How-To

What Is PHP and How to Use It?

PHP is an acronym for “PHP: Hypertext Preprocessor”. Learn about PHP and the basics you need to know to get started.

PHP is an acronym for “PHP: Hypertext Preprocessor.” It’s a scripting language that lets web developers run processes from a web page. Instead of those scripts running on the browser, they run on the webserver and then deliver the script’s output to the web page.

The benefit to using PHP is that it lets developers display far more interactive and dynamic web page content without bogging down the browser or consuming the user’s CPU. These scripts are embedded directly into the web page HTML that produces that static content of the page.

How to Embed PHP Into a Web Page

Embedding PHP functions into an HTML web page is very easy. Just preface the script with <?PHP and then place ?> at the end of the script.

Everything in between these tags will run on the server as a PHP script. The output of that script will be displayed on the page where you embedded it.

One of the simplest PHP functions is the “echo” function, which returns plain text. Embedding a simple PHP function like this works as follows:

<!DOCTYPE html>
<html>
<head>
<title>My PHP Echo Example</title>
</head>
<body>

<?php
echo “Hello World!”;
?>

</body>
</html>

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

hello world output

As you can see, the PHP script ran on the server and simply provided an output of the “echo” text you specified. This outputs in the form of the HTML static text “Hello World!”.

Things get much 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 so that it can process requests to execute PHP scripts.

The way pages with PHP scripts normally work is that your browser will open the page that’s stored on a remote web server somewhere on the internet. That web server has PHP installed and can process any PHP script requests that are called on that page.

php process

The order that this works is as follows.

  1. When you visit a web page on the web that contains PHP scripts, the webserver processes the page and all PHP scripts that are called the first time the page loads.
  2. The static HTML on the page and all PHP script outputs are delivered to your web browser.
  3. When you interact with any PHP elements on the page, a new request goes to the webserver.
  4. The server processes any new PHP scripts triggered by the user and then updates the page displayed on the browser with the new output.

This process involves all script processing taking place on the server rather than in your browser. The only demand put on your computer’s CPU is loading the static HTML content in the browser. All PHP script processing happens 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 consuming your CPU resources to work.

PHP Script Example

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 settings in the WordPress site configuration and format the page accordingly. PHP scripts can use conditional statements like “IF” to vary page output depending on those input conditions.

In the script below, the PHP script checks the “socialFooter” WordPress configuration. If the user has enabled this, then the PHP script displays the social footer section. Otherwise, it won’t display anything.

<?php if($socialFooter==”on”) { ?>
<div class=”footer-bottom”>

<?php if($vimeo) { ?><a href=”<?php echo $vimeo;?>” target=”_blank” class=”soc-button icon-text”>&#62215;</a><?php } ?>
<?php if($twitter) { ?><a href=”<?php echo $twitter;?>” target=”_blank” class=”soc-button icon-text”>&#62218;</a><?php } ?>
<?php if($facebook) { ?><a href=”<?php echo $facebook;?>” target=”_blank” class=”soc-button icon-text”>&#62221;</a><?php } ?>
<?php if($googlepluss) { ?><a href=”<?php echo $googlepluss;?>” target=”_blank” class=”soc-button icon-text”>&#62224;</a><?php } ?>
<?php if($pinterest) { ?><a href=”<?php echo $pinterest;?>” target=”_blank” class=”soc-button icon-text”>&#62227;</a><?php } ?>
<?php if($tumblr) { ?><a href=”<?php echo $tumblr;?>” target=”_blank” class=”soc-button icon-text”>&#62230;</a><?php } ?>
<?php if($linkedin) { ?><a href=”<?php echo $linkedin;?>” target=”_blank” class=”soc-button icon-text”>&#62233;</a><?php } ?>
<?php if($dribbble) { ?><a href=”<?php echo $dribbble;?>” target=”_blank” class=”soc-button icon-text”>&#62236;</a><?php } ?>
</div>
<?php } ?>

Inside this PHP IF statement, you’ll also notice individual IF statements that check which social network icons were enabled or disabled in the settings. The PHP script will only output the social media icons that were enabled in settings.

The output of this PHP script (the HTML output from 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 web page output that the user sees accordingly.

Using 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’re writing a longer PHP script that does the same thing multiple times.

A basic example of this is as follows:

<?php
function OutputString() {
echo “Hello world!”;
}

OutputString(); 
?>

Functions are always defined a the start. They contain an open bracket and close bracket to contain the function. The latter part of the PHP script contains the bulk of the function and all of the function calls.

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

Even if you’re not planning to become a PHP expert, it’s great to know the basics if your website or blog experiences PHP errors. Understanding the basics can help with troubleshooting. Or you could learn enough to do something like customizing the WordPress login page or logo.

Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

 

To Top