Path // www.yourhtmlsource.comPHP Scripting → INTRODUCTION TO PHP

Introduction to PHP


PHP is a hugely useful server-side scripting language, “especially suited for web development.” PHP adds the possibility of dynamic content to your pages: you can pull data from a database, place different content on your site on different days automatically, and do things like user logins; all with some relatively easy scripting. This first tutorial will concern itself with familiarising us with the basic syntax of PHP and some of its basic constructs.

Clock This page was last updated on 2025-11-16


A Little Exposition...

First let’s get a handle on what exactly PHP is. The abbreviation itself is one of those obscure jokes that geeky programmers love so much. It’s called a recursive initialism, if you care, and involves the abbreviation itself turning up in the explanation. The full expansion of PHP is “PHP: Hypertext Preprocessor.”

That “preprocessor” bit is the keyword here. Client-side scripting languages, like JavaScript, are used to create effects for HTML pages once they have reached the viewer, inside their browser. Server-side scripting languages, like PHP, pre-process the pages on the server before sending them to your browser. What this means is that all the maths, all the calls to the database, and all the string manipulation is done, the page is assembled, and only when all of that is finished is the page finally sent to your browser.

So, the PHP script will decide which HTML to send to the user. The page that gets sent will not include any PHP code at all. This is why you never see PHP code when you press View Source in your browser.

The following are some of PHP’s core benefits. PHP is...

  • free
  • open source (meaning anyone can submit changes to the code)
  • widely-deployed (which means it’s installed on » millions of servers across the web and is probably available on your own)
  • cross-platform (you can develop on a Windows machine and upload to a Unix server and the same code will still work)

These properties have combined to make PHP the most successful server-side scripting language on the web. Other examples of successful scripting languages are Perl, Cold Fusion and ASP. Each have their benefits, drawbacks and idiosyncracies; but PHP is generally thought of as the easiest to learn, while being powerful enough to run full-scale web applications.

A terminology point: up until now you’ve been writing pretty much static webpages. With the introduction of PHP code, your pages can be made dynamic. A site with a lot of scripting behind it can be thought of as a “web application,” a program that takes input and gives back output.

Basic PHP Syntax

All you need to write PHP is a basic text editor. A PHP page is much like a normal HTML page, with the addition of some PHP code. We wrap PHP code within delimeters that look sort of like HTML tags. You can think of it as two “modes.” Normally you’re coding in HTML mode, but can then open a PHP tag and enter PHP coding mode until you close the PHP section and go back into HTML mode. Here’s a basic PHP page:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
      "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
<title>My First PHP Page</title>
</head>

<body>

<p>This is normal HTML content.</p>

<?php
 echo "<p>This text will be added to the page by PHP.</p>";
?>

<p>We’re back coding normally in HTML mode.</p>

</body>
</html>

As you can see, this is a very basic HTML page up until that <?php bit. That’s our opening PHP tag, and everything that comes after that will be evaluated by PHP before the page is served to the user. We go back to normal HTML mode by closing the PHP section with the PHP close tag, ?>.

All the PHP itself does is prints — “echoes” in programming terms — the text we give it to the outputted page. So when this page is viewed in a browser, the paragraph that was “added to the file by PHP” will turn up exactly where we wrote it. The page ends up like this. View the source of that page to see the output.

All you need to do is save this file as example.php and upload it to your server. If, like most, it is enabled to interpret PHP code, it will see the .php extension and pass the file to PHP for evaluation before it is sent to the user. If it doesn’t work, contact your server administrators and ask them to sort it out. If you’re testing on a local server, read the PHP manual’s section on » installing PHP.

The file extension of a PHP-enabled page must be .php. There are a few exotic variants you might see around the web (.php3, .phtml etc.), but these are all deprecated and should not be used.

sourcetip: There are a few » other ways of entering PHP mode. The examples in these tutorials always use the standard <?php ... ?> style, as this is guaranteed to work everywhere.

Testing Your Setup

PHP includes a handy diagnostic function that you can call to check if everything is set up correctly on your server and see what way PHP is configured. Simply save a file with this line of code:

<?php phpinfo(); ?>

Once you view this page you should see a long table with all of PHP’s configuration details. Keep this script handy, and run it whenever you have trouble with something.

Variables

Like all programming languages, PHP has variables. Variables are places in your script where you can store values like numbers and text (called “strings” in programming). You can then use these values later on in your program. PHP uses the dollar sign, $ to denote variables. Below we’ll define a variable, and then print it out.

<?php
 $name = "John";
 echo $name;
?>

So that’s how we define and print out string variables. Notice how each statement in PHP ends in a semicolon. We can also define numeric variables, and perform mathematical processing on them, like so:

<?php
 $firstnumber = 10;
 $secondnumber = 3;
 $thrice = $firstnumber * $secondnumber;
 echo "These numbers multiplied together equal " . $thrice;
?>

The out put of this code snippet will be

These numbers multiplied together equal 30

Two things to note, and note well, here. Firstly, when we define numeric variables like $firstnumber, you don’t put quote marks around the value. Quote marks would mean to treat the value as a string, and we don’t want that here. Secondly, in the final echo line, we’re concatenating (joining, in other words), a string value and a numeric value into one sentance. The operator to join two variables together into a string is the dot.

Arrays