When you open this file from a web browser, the output will look like this:

hello exit from the world

As you can see, the PHP script executed on the server and simply provided output of the “echo” text you specified. This is output as the static HTML text “Hello World!”.

Things get a lot more interesting when you start using more advanced PHP functions (see below).

How does PHP work?

Unlike HTML, you can’t just create a PHP file on your computer and then open the file with your browser. This is because your computer would need PHP installed in order for it to process requests for executing PHP scripts.

The way that pages containing PHP scripts work normally is if your browser opens the page stored on a remote web server somewhere on the Internet. This web server has PHP installed and can handle all PHP script requests that are invoked on this page.

php process

The order in which it works is as follows.

  1. When you visit a web page that contains PHP scripts, the web server processes the page and any PHP scripts called when the page loads for the first time.
  2. The static HTML code for the page and all PHP script output is passed to your web browser.
  3. When you interact with PHP elements on the page, a new request is sent to the web server.
  4. The server processes any new user-triggered PHP scripts, then updates the page displayed on the browser with the new output.

This process means that all script processing takes place on the server rather than in your browser. The only demand on your computer’s processor is to load the static HTML content into the browser. All PHP script processing takes place on the web server rather than your computer. This makes web pages and interactions with the page more efficient and faster.

This is more efficient than browser-side scripting languages ​​like JavaScript processed by the browser and which consume your CPU resources to operate.

Example PHP script

Let’s look at a typical real-world example of a PHP script on a modern web page.

For WordPress sites, most web pages have PHP scripts that look up the settings in the WordPress site setup and format the page accordingly. PHP scripts can use conditional statements like “IF” to vary the output of the page according to these input conditions.

In the script below, the PHP script checks the WordPress “socialFooter” configuration. If the user has it enabled, the PHP script will display the social footer section. Otherwise, it will not display anything.

In this PHP IF statement, you’ll also notice individual IF statements that check which social media icons have been enabled or disabled in settings. The PHP script will only display social media icons that have been enabled in settings.

The output of this PHP script (the HTML output of the PHP script that the browser sees) looks like this:

php example

This example shows how PHP can look at different input conditions and then tailor the output of the web page the user sees accordingly.

Use PHP functions

PHP is like other programming languages ​​in that you can name functions that you can then call later in the script. This is useful when you are writing a longer PHP script that does the same thing multiple times.

Here is a basic example:

function StringOut () {
echo “Hello everyone!”;

StringOut ();

Functions are always defined at the start. They contain an open parenthesis and a closed parenthesis to contain the function. The last part of the PHP script contains most of the function and all the function calls.

If you are looking to learn all the existing PHP functions that you can call (the ones you don’t have to write yourself), php.net is the best online resource for finding all of the available functions to use in PHP.

Even if you don’t plan on becoming a PHP expert, it’s good to know the basics if your website or blog is experiencing PHP errors. Understanding the basics can help with troubleshooting. Or you could learn enough to do something like customize the WordPress login page or logo.

Source link