This content originally appeared on DEV Community and was authored by Code_Jedi
If you're trying to build something like a SaaS, Micro-SaaS or just a website with some functionalities, then you're going to need to learn how to Perform backend operations with PHP.
That's why, in this tutorial, I'll be showing you how to perform backend operations with PHP
First of all, why would you need to perform backend operations in PHP?
Say you're trying to build a web application and you want it to perform tasks such as Machine Learning, Web Scraping and Language Translation, then you'd probably want to perform them in the backend, there are 4 main reasons for this:
- Your frontend language may not be able to perform the task well enough or doesn't fulfill all the desired functionalities.
An example of this is machine learning, it's a lot easier and more functional in python then it is in Javascript or PHP, that's why when trying to incorporate Machine Learning into your web application, it's a good idea to perform your machine learning scripts as backend operations with PHP.
- The task is too memory intensive and/or executes for too long
If your task is something that requires a lot of memory and/or executes for too long, it's not a good idea to make such a task running in the frontend, in turn slowing down your webpage and web performance.
- The task simply can't be performed in the frontend
Some tasks like web automation can't be performed in the frontend because it requires opening browsers and performing tasks in those browsers, needless to say, this can't be performed on a webpage.
- Security and private information Sometimes, if your task involves sensitive information, or if you just don't want people to see your task's inner-workings through frontend languages like javascript, then you should perform such a task in the backend.
Let's get started with simple backend operations in PHP!
Let's create our index.php
file and add the following contents:
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Simple operations</title>
</head>
<body>
<?php
$output = exec("python3 op1.py");
echo("99 divided by 6 is ");
echo("<h1>$output</h1>");
?>
</body>
</html>
As you can see, the PHP script only does 3 things:
- It executes
op1.py
and stores it's output as "$output". - It echos the simple math operation we want
op1.py
to perform. - Finally, it echos the output of the python3
op1.py
operation.
op1.py
print(99 / 6)
Read more about exec
Now if you run php -S localhost:8000
in your project's terminal and navigate to that address, you should see that your index.php
script has executed the op1.py
script and returned it's output as intended:
The way this works is simple, our webpage executes a script, requests its output and displays it on our webpage.
My beautiful diagram
That's it for simple backend operations with PHP, stick around for part 2 and part 3 to learn about more complex and demanding backend operations!
Byeeee?
This content originally appeared on DEV Community and was authored by Code_Jedi
Code_Jedi | Sciencx (2021-08-29T13:00:58+00:00) Backend Operations in PHP from 0 to Hero(Pt. 1 simple operations). Retrieved from https://www.scien.cx/2021/08/29/backend-operations-in-php-from-0-to-heropt-1-simple-operations/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.