This article covers basic PHP syntax, including variable usage, variable types, and several ways of printing variables to the web browser.
Embedded code blocks
PHP is an embedded web development language with many similarities to commercial packages such as Microsoft's Active Server Pages (ASP) or Cold Fusion. One of the similarities between PHP and these packages (especially ASP) is the ability to jump between PHP and HTML code quickly and easily. The basic syntax to jump in and out of PHP follows:
<html> <head> <title>My first PHP page</title> </head> <body> This is normal HTML code <?php // PHP code goes here ?> Back into normal HTML </body> </html>
In this example, we see that PHP code is signified by the use of the
<?php to begin the PHP block and
?> to signify the end of the code block. Although this a completely acceptable method of encapsulating your PHP code there are many other ways that are all, as far as syntax is concerned, correct. Here are some other ways to mark PHP code blocks.
|Valid syntax to indicate a PHP code block|
||Standard PHP syntax|
||ASP-style PHP syntax|
||Standard script syntax|
So, for example, let's say that you are a web developer who uses a third-party software package to do the actual layout of your web pages. Under normal conditions, PHP's standard syntax would cause unpredictable results in your layout software. So, to remedy this you could use the HTML script standard syntax instead of the PHP standard:
Also in PHP Foundations:
<html> <head> <title>My first PHP page</title> </head> <body> This is normal HTML code <script language="PHP"> // PHP code goes here // This code block syntax won't break // graphical web layout software </script> Back into normal HTML </body> </html>
Before we discuss variables, we should begin with some general PHP syntax rules. First, all single-line statements must conclude with a semicolon. In addition, statements that exceed a single line (such as most conditionals) must be surrounded by
} characters. Finally, the double forward slash (
//) represents a comment and everything past those characters until the end of the line will be ignored by PHP. Now, on to the variables in PHP!
Variables in PHP
PHP denotes all of its variables by using the
$ operator followed by any combination of characters as long as these rules are followed:
- The variable name starts with a letter or an underscore (
- It is followed by any combination of letters, numbers, and underscores
Note: A letter is defined as the lowercase and uppercase characters "a" through "z" as well as any character with an ASCII value between 127 and 255
To define a variable, you can either define it with a value or by using the
var operator. Example:
<?php $myvar = "foo"; // The variable contains nothing var $my_second_var; ?>
In this example, the variable
$myvar is assigned the string value "foo" while the second variable we defined
$my_second_var is empty and contains nothing. For a comparison, we will now give you some examples of invalid PHP variables:
<?php // Incorrect: Must have a '$' sign in front myvar = "bar"; // Incorrect: Variable name starts with a number $2myvar = "cat"; // Incorrect: Variable name has invalid characters $my(third)var = "dog"; // Correct syntax $_myvarnumber4 = "mouse"; ?>
Now that we have learned proper syntax for our variables, we can move on the variable types and functions.
Types of variables
PHP is what is known as a "loosely typed" language. What that means is any given variable can be an integer, floating-point number, string, object, or an array. In this article, we will only be discussing the first three types.
Type 1: The integer
An integer is the basic mathematical datatype and represents any whole number and usually can be any value from minus 2 billion to 2 billion. When assigning an integer value, three different types of notation can be used: decimal (regular base 10), hexadecimal (base 16), or octal (base 8). Usually, only the normal decimal notation is used but there are special cases when a hexadecimal or octal notation makes life easier for the developer.
<?php // All of the following are numerically equivalent $myint = 83; // Normal decimal notation $myint = O123; // Octal notation for the # 83 $myint = 0x53; // Hexadecimal notation for # 83 ?>
Type 2: The floating-point number
Floating-point numbers are the second mathematical datatype PHP provides. A floating-point number represents any value that contains a decimal point. Floating-point numbers are somewhat unreliable in the sense that the value stored in them is not always the exact value the developer expects, but for now we will ignore that. Instead, we will focus on the notation used to assign a variable a floating-point value. Floating-point numbers can be expressed in two different types of notation: decimal and scientific.
<?php // Both of the following are equivalent to 1.234 $myfloat = 1.234; // Standard decimal notation $myfloat = .001234e3; // Scientific notation ?>
Type 3: The string
A string is a datatype we first used in our original examples to assign values to variables. A string can be any combination of letters, numbers, or special symbols as long as consideration is given to characters that have functions in PHP. Before we consider special cases, let's first discuss the difference between the two string notations: the single and double quote. In every case where you may want to assign a string value to a variable, the value itself must begin and end with a pair of either single (
' ') or double (
" ") quotes.
<?php // This string begins and ends with single quotes $mystring = 'single quoted string'; // This string begins and ends with double quotes $mystring = "double quoted string"; ?>
In this example, both variables would simply be assigned a value within the single or double quotes. However, when double quotes are used, PHP will first look inside the string for any references to variables that may exist. If any references are found, they are replaced with values before being assigned to the designated variable. Conversely, when dealing with single-quoted strings, PHP simply takes the string as-is and assigns it to the designated variable.
<?php $myint = 10; // Assign the variable myint to 10 $string_one = 'The value of myint is $myint'; $string_two = "The value of myint is $myint"; ?>
Consider the above example. In the first line, we simply assign the integer value "10" to the variable
$myint. Then, we assign two more variables
$string_two. These are identical except
$string_one is stored using single quotes and
$string_two is stored using double quotes. In this example, the values within the two strings are as follows:
- $string_one = The value of myint is $myint
- $string_two = The value of myint is 10
Notice that, when the value of
$string_two is displayed, the variable
$myint was replaced with the value "10". In the single-quoted string, however, the actual string
$myint was stored.
Next, we will discuss "special" characters. Here's a common scenario: You are developing a web site and find yourself needing to store the double-quote character itself (
") as a string within another variable. You can't simply place the double quote within a set of double quotes because it will cause an error in PHP. To overcome this dilemma, a method called an "escape" is used to allow developers to store this special character along with others in strings. To escape a character, the character is simply prefixed by the backslash character (
\). So, to store the double-quote character in a string, we would instead tell PHP to store the string
\" instead. Here is an example:
<?php // The following will cause an error in PHP $mybadstring = "Do you know what an "escape" is?"; // The same string properly escaped $mystring = "Do you know what an \"escape\" is?"; ?>
The first attempt is incorrect due to the use of "un-escaped" double quotes within the string itself. The proper syntax for storing this string is illustrated in the second example where the quotes surrounding the word "escape" are properly coded. Below is a table of other special characters that require a backslash.
Note: Although attempting to store a un-escaped double-quote within a string will cause an error, storing a single-quote within a double-quoted string will not throw an error and is completely acceptable.
|Valid back-slashed characters|
||linefeed (LF or 0x0A in ASCII)|
||carriage return (CR or 0x0D in ASCII)|
||horizontal tab (HT or 0x09 in ASCII)|
||the sequence of characters matching the regular expression is a character in octal notation|
||the sequence of characters matching the regular expression is a character in hexadecimal notation|
John Coggeshall is a a PHP consultant and author who started losing sleep over PHP around five years ago.
Read more PHP Foundations columns.
Discuss this article in the O'Reilly Network PHP Forum.
Return to the PHP DevCenter.