Backend Operations in PHP from 0 to Hero(Pt. 1 simple operations)
Tags: php, devops, programming, webdev
Published: 2021-08-29 13:00:58

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:
```php
<!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:**

1. It executes `op1.py` and stores it's output as `$output`.
2. It echos the simple math operation we want `op1.py` to perform.
3. Finally, it echos the output of the python3 `op1.py` operation.

**op1.py**
```python
print(99 / 6)
```
Read more about [exec](https://www.php.net/manual/en/function.exec.php)

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:
![Simple backend operation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zb5algjopkxym1wgjm0r.png)
**************

The way this works is simple, our webpage executes a script, requests its output and displays it on our webpage.

![Diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f0igemwsdbgjmmqekrzn.png)

*************
#### 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👋