Hello PHP world!
In the previous article, you briefly saw how a simple PHP script may look like, now it's time to take a closer look at how PHP scripts work.
Before you start
- you need to have PHP installed
- create a directory called
php-tutorialin your web root to be used for storing example scripts - all examples assume the web root is
C:\mywebstuff\htdocsand the server is accessible ashttp://localhost/
Simplest script - no PHP code
You can create a file called C:\mywebstuff\htdocs\hello.html with the following content:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Hello world</title>
</head>
<body>
Hello World!
</body>
</html>
Now load the page as:
http://localhost/php-tutorial/hello.html
You'll see the words "Hello World!" printed in the browser. Nothing really special here.
Now rename the file to hello.php and request it in your browser as http://localhost/php-tutorial/hello.php The result will be exactly the same. This means that a PHP script can simply be an HTML document. But a PHP script can be much more once you unleash the power of the PHP tag!
PHP tags
Let's change the body section of the script above to:
<body>
<?php
echo 'Hello World! ';
echo 'Today is ';
echo date('Y-m-d');
?>
</body>
If you reload the page in your browser, you'll see something like:
Hello World! Today is 2007-09-05
Nice, we printed the current date! And if you visit the page tomorrow, it will display tomorrow's date. But how did this happen?
The PHP code is inserted in the normal HTML code by using the start tag <?php and the end tag ?>. Anything between these tags is processed by the PHP engine. If the PHP code between those tags uses echo (or print, or printf), it prints something to the page which looks like a normal HTML to the browser. Actually, if you view the source of this page you'll see that the body section contains:
<body> Hello World! Today is 2007-09-05 </body>
As you can see, there's no PHP code, it was already processed and its result was printed to the page. The PHP code is hidden from the end user of the page (the browser), so you can do all the top secret stuff you want and you select what to echo to the browser.
Echoing HTML
In the previous example we used PHP to print simple text to the browser, but we can also print HTML (or JavaScript, or CSS) if we want to. Basically PHP produces output that you would otherwise type in a static HTML. Here's a modified version of the example above:
<body>
<?php
echo 'Hello World! ';
echo 'Today is ';
echo '<strong>', date('Y-m-d'), '</strong>';
?>
</body>
The result of this script will have the date in bold:
Hello World! Today is 2007-09-05
The commas between the different parts printed on the last line separate the different chunks you want to output. This line can basically be read as "echo an opening strong tag, then echo the date, then echo a closing strong tag". Another way (more common, but less performant) to do the same is:
echo '<strong>' . date('Y-m-d') . '</strong>';
The dots mean that the different parts needs to be merged together. You can read this line as: "Take an opening strong tag, glue the date to it, then glue the closing strong tag to the result of the previous operation, and then echo the final result".
Using dots to glue pieces of text together is also known as "string concatenation" and is slightly slower than simply echoing strings, so a good PHP programming habit is to get used to the comma way, although most of the code you'll see out there uses string concatenation.
Double or single quotes
The following two lines will give you the same result:
echo "Hey there"; echo 'Hey there';
The second way (using single quotes) is preferred for two main reasons:
- it's more convenient when printing HTML with attributes, for example
echo '<a href="hello.php">Say hi!</a>'; - it's faster
Single quotes are faster, because they cannot contain PHP variables, so the PHP engine has an easier job when it comes to understanding what's in there between the quotes. Double quotes can contain PHP variables and some special characters which makes them more convenient in some cases, but most of the time it's better to use single quotes. (You'll learn about PHP variables in the following articles.)
Echoing quotes
OK, so quotes surround whatever you want to print to the browser. But what if you want to actually print quotes. The solution is simple - using the \ before the quote. The backslash is called also "escape character".
So <?php echo 'I don't know'; ?> is a syntax error, but <?php echo 'I don\'t know'; ?> will work as expected. If you want to escape \, just add another \, like \\.
Another way is to use the "other" quotes, for example the following lines are all valid:
<?php echo "I don't know"; ?><?php echo 'He said "Hi!" and then left.'; ?><?php echo "He said \"Hi!\" and then left."; ?>
Now you can see why single quotes are easier to use then printing HTML attribute. In this example:
echo '<a href="hello.php" title="Hi link" >Say hi!</a>';
if you want to use double quotes, you'll have to escape them, like:
echo "<a href=\"hello.php\" title=\"Hi link\">Say hi!</a>";
The second line is definitely harder to read and write.
The semi-colon
Maybe you've noticed already, but let's make this clear - all PHP statements end with a semi-colon. Usually one statement takes one line, but you can have two statements (or more) on one line if you wish.
Valid code:
echo 'hi!'; echo 'ho!'
Also valid:
echo 'hi!'; echo 'ho!'
An error (missing semi-colon):
echo 'hi!' echo 'ho!'