Выбрать главу

?>

PHP would see the opening quotation mark, read all the way up to the O in O'Reilly, and then see the quotation mark following the O as being the end of the string. The Reilly? part would appear to be a fragment of text and would cause an error. You have two options here: You can either surround the string in double quotation marks or escape the single quotation mark with \'.

If you choose the escaping route, it will look like this:

echo 'Are you really Bill O\'Reilly?';

Although they are a clean solution for small text strings, you should be careful with overusing escape sequences. HTML is particularly full of quotation marks, and escaping them can get messy:

$mystring = "<img src=\"foo.png\" alt=\"My picture\" width=\"100\" height=\"200\" />";

In that situation, you are better off using single quotation marks to surround the text simply because it is a great deal easier on the eye!

Variable Substitution

PHP allows you to use two methods to define strings: single quotation marks, double quotation marks, or heredoc notation, but the latter isn't often used. Single quotation marks and double quotation marks work identically, with one minor exception: variable substitution.

Consider the following code:

<?php

 $age = 25

 echo "You are ";

 echo $age;

?>

That is a particularly clumsy way to print a variable as part of a string. Fortunately, if you put a variable inside a string, PHP performs variable substitution, replacing the variable with its value. That means you can rewrite the code like so:

<?php

 $age = 25

 echo "You are $age";

?>

The output is the same. The difference between single quotation marks and double quotation marks is that single-quoted strings do not have their variables substituted. Here's an example:

<?php

 $age = 25

 echo "You are $age";

 echo 'You are $age';

?>

The first echo prints "You are 25", but the second one prints "You are $age".

Operators

Now that we have data values to work with, we need some operators to use, too. We have already used + to add variables together, but many others in PHP handle arithmetic, comparison, assignment, and other operators. Operator is just a fancy word for something that performs an operation, like addition or subtraction. However, operand might be new to you. Consider this operation:

$a = $b + c;

In this operation, = and + are operators and $a , $b , and $c are operands. Along with +, you also already know - (subtract), * (multiply), and / (divide), but Table 27.2 shows more.

TABLE 27.2 Operators in PHP

Operator What It Does
= Assigns the right operand to the left operand.
== Returns true if the left operand is equal to the right operand.
!= Returns true if the left operand is not equal to the right operand.
=== Returns true if the left operand is identical to the right operand. This is not the same as ==.
!== Returns true if the left operand is not identical to the right operand. This is not the same as !=.
< Returns true if the left operand is smaller than the right operand.
> Returns true if the left operand is greater than the right operand.
<= Returns true if the left operand is equal to or smaller than the right operand.
&& Returns true if both the left operand and the right operand are true.
|| Returns true if either the left operand or the right operand is true.
++ Increments the operand by one.
-- Decrements the operand by one.
+= Increments the left operand by the right operand.
-= Decrements the left operand by the right operand.
. Concatenates the left operand and the right operand (joins them).
% Divides the left operand by the right operand and returns the remainder.
| Performs a bitwise OR operation. It returns a number with bits that are set in either the left operand or the right operand.
& Performs a bitwise AND operation. It returns a number with bits that are set both in the left operand and the right operand.

There are at least 10 other operators not listed, but to be fair, you're unlikely to use them. Even some of the ones in this list are used infrequently — bitwise AND, for example. Having said that, the bitwise OR operator is used regularly because it allows you to combine values.

Here is a code example demonstrating some of the operators:

<?php

 $i = 100;

 $i++; // $i is now 101