HTML and CSS Reference
In-Depth Information
You can include the array value using string concatenation:
$html_paragraph = “<p>” . $paragraph['intro'] . “</p>”;
You can also use array references within strings if you enclose them within curly braces,
like this:
$html_paragraph = “<p>{$paragraph['intro']}</p>”;
One final note on defining strings is escaping. As you know, quotation marks are com-
monly used in HTML as well as in PHP, especially when it comes to defining attributes
in tags. There are two ways to use quotation marks within strings in PHP. The first is to
use the opposite quotation marks to define the string that you're using within another
string. Here's an example:
$tag = '<p class=”important”>';
I can use the double quotes within the string because I defined it using single quotes.
This particular definition won't work, though, if I want to specify the class using a vari-
able. If that's the case, I have two other options:
$tag = “<p class=\”$class\”>”;
$tag = '<p class=”' . $class . '“>';
In the first option, I use the backslash character to “escape” the double quotes that occur
within the string. The backslash indicates that the character that follows is part of the
string and does not terminate it. The other option is to use single quotes and use the
string concatenation operator to include the value of $class in the string.
Conditional Statements
Conditional statements and loops are the bones of any programming language. PHP is no
different. The basic conditional statement in PHP is the if statement. Here's how it
if ($var == 0) {
echo “Variable set to 0.”;
The code inside the brackets will be executed if the expression in the if statement is
true. In this case, if $var is set to anything other than 0, the code inside the brackets will
not be executed. PHP also supports else blocks, which are executed if the expression in
the if statement is false. They look like this:
if ($var == 0) {
echo “Variable set to 0.”;
} else {
Search WWH ::

Custom Search