DEV Community

Cover image for Backend Operations in PHP from 0 to Hero(Pt. 1 simple operations)

Posted on • Updated on

Backend Operations in PHP from 0 to Hero(Pt. 1 simple operations)

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>
    <meta charset="UTF-8">
    <title>Simple operations</title>
  $output = exec("python3");
  echo("99 divided by 6 is ");
Enter fullscreen mode Exit fullscreen mode

As you can see, the PHP script only does 3 things:

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

print(99 / 6)
Enter fullscreen mode Exit fullscreen mode

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 script and returned it's output as intended:
Simple backend operation

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


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!


Top comments (0)