Ubuntu Unleashed 2019 Edition: Covering 18.04, 18.10, 19.04

(singke) #1
        echo    $i  +   $j;
echo "\n";
echo $i + $k;
echo "\n";
?>

This time PHP prints a new line after each of the numbers, making it obvious
that the output is 20 and 10 rather than 2010 . Note that the escape
sequences must be used in double quotation marks because they will not work
in single quotation marks.


Three common escape sequences are \, which means “ignore the backslash”;
", which means “ignore the double quote”; and ', which means “ignore the
single quote.” This is important when strings include quotation marks inside
them. If we had a string such as “Are you really Conan
O’Brien?”, which has a single quotation mark in it, this code would not
work:


Click here to view code image
<?php
echo 'Are you really Conan O'Brien?';
?>


PHP would see the opening quotation mark, read all the way up to the O in
O’Brien, and then see the quotation mark following the O as being the end of
the string. The Brien? 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 '. The
escaping route looks like this:


Click here to view code image
echo 'Are you really Conan O\'Brien?';


Although escape sequences are a clean solution for small text strings, be
careful not to overuse them. HTML is particularly full of quotation marks,
and escaping them can get messy, as you can see in this example:


Click here to view code image
$mystring = "<img src=\"foo.png\" alt=\"My picture\"
width=\"100\" height=\"200\" />";


In such a 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

Free download pdf