How PHP is written

As explained already PHP code is embedded inside HTML, making a complete PHP script. By default, PHP operates with PHP mode turned off - all text read in a script is considered to be HTML unless PHP mode has been enabled.

This method of parsing means that the PHP elements of a script are "code islands"; standalone chunks of code that can work independently of the HTML "sea" around them. That is not to say that the PHP code cannot affect the HTML - far from it! We will get there soon.

PHP scripts are generally saved with the file extension .php to signify their type. Whenever your web server is asked to send a file ending with .php, it first passes it to the PHP interpreter, which executes any PHP code in the script before returning a generated file to the end user. Each line of PHP code is called a statement, and ends with a semi-colon to signify it is a complete statement.

Variables in PHP, that is, objects that store data, all begin with $ followed by a letter or an underscore, then any combination of letters, numbers, and the underscore character. This means you may not start a variable with a number, so be careful. Here is a list of valid and invalid variable names:

$myvar

Correct

$Name

Correct

$_Age

Correct

$___AGE___

Correct

$91

Incorrect - starts with a number

$1Name

Incorrect - starts with a number

$Name91

Correct; numbers are fine at the end and after the first character

$_Name91

Correct

$Name's

Incorrect no symbols other than _ are allowed, so apostrophes ' are bad.

Variables are case sensitive, which means that $Foo is not the same variable as $foo, $FOO, or $fOO.

Assigning variables is as simple as using the equals operator (=) on a variable, followed by the value you want to assign. Here is a basic script showing assigning and outputting data - note the semi-colons used to end each statement:

<?php
    $name
= "Paul";
    print
"Your name is $name\n";
    
$name2 = $name;
    
$age = 20;
    print
"Your name is $name2, and your age is $age\n";
    print
'Goodbye, $name!\n';
?>

As you can see, we set the $name variable to be the string "Paul", and PHP lets us print out that variable alongside "Your name is". Therefore, the output of the first print statement is "Your name is Paul", because PHP will substitute $name for its value whenever it finds it by itself, or inside a double-quoted string (that is, one starting and ending with "").

Author's Note: The technical term for "variable substitution" is "variable interpolation".

We then set $name2 to be $name, which effectively copies $name's information into $name2 entirely. $name2, therefore, is now also set to "Paul". We also set up the $age variable to be the integer 20. Our second print statement outputs both variables at once, as again PHP will substitute them inside the string.

The last print statement will not do what was expected, however. It will print:

Goodbye, $name!\n

The reason for this is that PHP will not perform variable substitution for variables inside single-quoted strings, and won't even replace the escape characters. Whereas in double-quoted strings, PHP will replace the variable $name with its value, in a single-quoted string, PHP will consider $name to mean that you actually want it to output the word "$name" just like that.

One final note before we go onto other topics. Occasionally you may run into the situation where you want to tack a value onto your variable inside your strings, but you find that PHP considers these characters to be part of the variable. Take a look at this code:

<?php
    $food
= "grapefruit";
    print
"These $foods aren't ripe yet.";
?>

Ideally what we wanted to be printed was "These grapefruits aren't ripe yet", but because we have added the S to the end of the variable name, we have changed it from trying to read $food to trying to read $foods. The variable $foods does not exist, so PHP will leave the space blank. How to solve this? Well, we need to make it clear to PHP where the variable starts and ends, and there are two ways to do this:

<?php
    $food
= "grapefruit";
    print
"These ${food}s aren't ripe yet.";
    print
"These {$food}s aren't ripe yet.";
?>

Both of those are valid and should work as expected. The braces, { and }, tell PHP where the variable ends. Note that code like this will work without modification:

<?php
    $food
= "grapefruit";
    print
"This $food's flavour is bad.";
?>

That will work fine because you are not allowed to use an apostrophe as part of your variable names. If you want to make your life easy you can just avoid the problem entirely by not getting PHP to substitute the variable:

<?php
    $food
= "grapefruit";
    print
"These " . $food . "s aren't ripe yet.";
?>

 

Next chapter: Whitespace >>

Previous chapter: Running PHP scripts

Jump to:

 

Home: Table of Contents

Follow us on Identi.ca or Twitter

Username:   Password:
Create Account | About TuxRadar