Essential PHP
- Creating Your First PHP Script
- Getting PHP
- Running PHP on the Command Line
- Mixing PHP in Some HTML
- Creating PHP Variables

If php is in your path, you can run this from the command line like so (where % is a generic, cross-platform command-line prompt, and this example assumes you’re in the same directory as echo.php):
%php echo.php
If this works, you’ll see:
Hello from PHP.
If it doesn’t work, you can specify the exact location of php, which might look like this in Unix or Linux:
$/usr/local/bin/php echo.php
And something like this in Windows:
C:\>C:\php\php echo.php
The CLI has many command-line options, which you can use to customize its operation. In fact, php can tell you all about its options if you enter php -h to get the full list:
%php -h Usage: php [options] [-f] <file> [--] [args...] php [options] -r <code> [--] [args...] php [options] [-B <begin_code>] -R <code> [-E <end_code>] [--] [args...] php [options] [-B <begin_code>] -F <file> [-E <end_code>] [--] [args...] php [options] — [args...]
-a Run interactively -c <path>|<file> Look for php.ini file in this directory -n No php.ini file will be used -d foo[=bar] Define INI entry foo with value ‘bar’ -e Generate extended information for debugger/profiler -f <file> Parse <file>. -h This help -i PHP information -l Syntax check only (lint) -m Show compiled in modules -r <code> Run PHP <code> without using script tags <?..?> -B <begin_code> Run PHP <begin_code> before processing input lines -R <code> Run PHP <code> for every input line -F <file> Parse and execute <file> for every input line -E <end_code> Run PHP <end_code> after processing all input lines -H Hide any passed arguments from external tools. -s Display colour syntax highlighted source. -v Version number -w Display source with stripped comments and whitespace. -z <file> Load Zend extension <file>.
For example, if you want to get a simple text version of the information the phpinfo function prints out, use the -i option this way: %php -i. And you already know you can get the version of PHP with the -v option (note the (cli) in the output, indicating that we’re using the CLI):
%php -v
PHP 5.0.0 (cli) (built: Jul 13 2004 21:39:58)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v2.0.0, Copyright (c) 1998-2004 Zend Technologies
In Linux and Unix, you can run PHP scripts simply by typing the name of the script on the command line if you indicate where to find PHP with a line that begins with #! (and give the script execution permission):
#! /usr/bin/php
<?php
echo "Hello from PHP.";
?>
Commenting Your Scripts
The lines in a PHP web page so far have been either HTML or PHP scripts, which are meant to be read by computers. But there’s also a component that’s meant to be read only by peoplecomments.
Comments are annotations you add to your PHP pages to tell the story of what’s going on to a human. This is important because when you come back to a long and complex script years from now, you probably won’t remember what was happening in it. Or you might pass your script around for others to use. That’s where comments come in. With comments, you can describe exactly how a script behaves so that you can pick it up instantly later on.
There are three types of comments in PHP. The first kind lets you write multi-line comments, beginning with /* and ending with */ like this:
<?php /* Start by displaying a message to the user */
echo "Hello from PHP."; ?>
You can surround each line with /* and */ to make the comment look like a block, which will attract more attention:
<?php /* Start by displaying a */ /* message to the user */
echo "Hello from PHP."; ?>
But one thing that will make PHP choke is nesting comments inside each other, so don’t do this:
<?php /* Start by /* displaying a */ message to the user */
echo "Hello from PHP."; ?>
This won’t work because PHP looks for */ to end a comment, and when it sees that, it assumes the comment is endedwhich is a problem because the comment isn’t actually ended, but PHP will suddenly see what it thinks is plain text where there should be PHP statements.
The other types of comments are one-line comments, designed to hold text that will fit on a single line. You can use either // to start these comments, or a #:
<?php // Start by displaying a # message to the user
echo "Hello from PHP."; ?>
These kinds of comments are also useful because they can be placed on lines that also contain code. In that case, PHP will ignore anything after the # or //, like this:
<?php
echo "Hello from PHP."; //Display a message
echo "Hello from PHP again!"; #Display another message
?>
You can also use these comments to make blocks:
<?php // Start by displaying a // message to the user
echo "Hello from PHP."; ?>
Or, to make something that will really stand out, try this:
<?php ########################## # Start by displaying a # #/ message to the user # ##########################
echo "Hello from PHP."; ?>
These days, single-line comments, which are easier to write because you don’t have to keep track of where they end, are prevailing. The multi-line comments still have their uses, though, and are often used at the beginning of programs to form a comment block that explains what the program is all about. They’re also sometimes used when you define your own functions so that a comment-style block can indicate what the function does and how you use it.
It’s a good idea to use as many comments as needed to clarify what a script is doing. Using too many comments can cloud the issue, but not using enough is worse.
Assigning Values to Variables
In PHP, when you want to create a variable, you assign data to it using an assignment operator. We’ll see all the PHP assignment operators in next Chapter, “Gaining Control With Operators and Flow Control,” but for now, the most common one is the equals sign, =. Here’s an example that uses the equals operator to assign values to new variables (after this runs, $temperature will hold 69, $pi will hold 3.1415926535, and so on):
$temperature = 69;
$number_of_earths = 1;
$pi = 3.1415926535;
$reassurance = "No worries.";
Note that we’re assigning numbers to some variables and text to other variables here. In some languages, you need to specify the variable type, such as string or integer, but not in PHP, which makes it much easier.

Internally, however, the computer does use various data types for storage, so in some cases you should know what issues can arise because of data typing. For more on data types, see “Handling Data Types” in next chapter.
Take a look at an example, phpvariables.php, which appears in Example 1-4. In this case, we’re assigning a value of 1 to a variable named $apples and displaying the value stored in that variable:
echo "Setting number of apples to 1.<BR>";
$apples = 1;
echo "Number of apples: ", $apples, "<BR>";
.
.
.
Next, say we want to increase the number of apples we have by three. You can do that by assigning $apples the current value in $apples plus 3 and displaying the new result:
echo "Setting number of apples to 1.<BR>";
$apples = 1;
echo "Number of apples: ", $apples, "<BR>";
echo "Adding 3 more apples.<BR>";
$apples = $apples + 3;
echo "Number of apples now: ", $apples, "<BR>";
Example 1-4. Assigning values to variables
<HTML> <HEAD> <TITLE> Assigning values to variables </TITLE> </HEAD> <BODY> <H1> Assigning values to variables </H1> <?php echo "Setting number of apples to 1.<BR>";
$apples = 1;
echo "Number of apples: ", $apples, "<BR>";
echo "Adding 3 more apples.<BR>";
$apples = $apples + 3;
echo "Number of apples now: ", $apples, "<BR>"; ?> </BODY> </HTML>
You can see the results in Figure 1-8. Now you can do math at run time, storing data in variables and manipulating that data as needed.

Figure 1-8. Assigning values to variables.
Interpolating Variables in Strings
You can display the values of variables like this:
$apples = 1;
echo "Number of apples: ", $apples, ".";
However, there’s a shortcut that you can use to make this easier. The values in variables can be interpolated if you put them into double-quoted (not single-quoted) strings, which means that their values are inserted directly into the string. This technique enables you to convert the example from the previous chunk:
$apples = 1;
echo "Number of apples: $apples.";
This example prints out Number of apples: 1. You can see the complete previous example, which displays variable values after assignment, converted to use variable interpolation in Example 1-5, phpinterpolation.php.
Example 1-5. Expanding variables in strings
<HTML> <HEAD> <TITLE> Interpolating variables </TITLE> </HEAD> <BODY> <H1> Interpolating variables </H1> <?php echo "Setting number of apples to 1.<BR>";
$apples = 1; echo "Number of apples: $apples <BR>"; echo "Adding 3 more apples.<BR>"; $apples = $apples + 3; echo "Number of apples now: $apples <BR>"; ?> </BODY> </HTML>
The results of this example appear in Figure 1-9.

Figure 1-9. Interpolating variable values.
Interpolation gives you a quick way of displaying the contents of a variable, but there’s an issue to watch here. For example, what if the variable $text held the text “news“, and you wanted to put the word “newspaper” into the output? You might try this:
<?php $text = "news";
echo "Where's the $textpaper <BR>"; ?>
But PHP isn’t going to understand that because it looks like you’re using a variable named $textpaper. Here’s the error you get:
PHP Notice: Undefined variable: textpaper in C:\php\t.php on line 4
The correct way to handle this situation is to enclose the variable you’re interpolating, $text, in curly braces, { and }, as in Example 1-6.
Example 1-6. Expanding variables in strings
<HTML> <HEAD> <TITLE> Interpolating variables </TITLE> </HEAD> <BODY> <H1> Interpolating variables </H1> <?php $text = "news";
echo "Where's the {$text}paper.";
?>
</BODY>
</HTML>

