DJ Mike's Tutorials: PHP


< ^ >

Basic Syntax Pt. 2

Here is a simple PHP script embeded in HTML:

<html>
<body>
<?
echo "Hello world.";
?>
</body>
</html>


and here is what the results look like: helloworld_01.php. Pretty boring on the face of it but there are several important points to this. For one thing, use a source code viewer on helloworld_01.php and you'll notice that you don't see the PHP code. Server side language, remember? The server looks for the <?, processes the PHP code till it gets to the ?> then wraps it all up and sends it tho the browser. The browser never sees the PHP code that generated the text.

Your first script.

Simple statements: A simple statement is a statement that is executed without conditions and with out looping. Your first example of a PHP script is:

<?
echo "Hello world.";
?>


The script consists of an echo statement, which sends output to the browser, containing a string (a series of charactors) enclosed in quotes ending in a simicolon.

Don't forget that simicolon!

If you forget it, the PHP engine won't know that you wanted to end the function and keep running on until it finds one. Since webtv will parse just about any HTML no matter how sloppy, you may have gotten into bad habits. If you leave out one tiny simicolon, the whole thing will fail. PHP will generate an error message but it is so cryptic that it is almost useless. (List of error messages) Not only are they useless for determining what you did wrong, they may tell you that the error is on a line when it is really five lines up from the line it says it is on! 90% of my errors are from missing simiquotes so that is the first thing I look for.

Notes On Quotes And Special Characters

There are special characters and combinations of characters that have meaning to PHP. If you use single quotes instead of double quotes, these characters will be literal instead of having meaning. For example, in the "hello world" script, the $ tells PHP that the following string of characters is the name of a variable. If you use single quotes instead of double quotes, special characters will have no meaning. The next biggest source of bugs is missing quotes and quotes where they don't belong. Under different circumstances you may use double quotes, single quotes or no quotes.

Numbers
If the output is a number, use no quotes:
<?
echo 5# outputs the number 5
?>
Strings
For strings, use either single quotes or double quotes:
<?
echo 'Hello world'
# outputs Hello world
?>

or
<?
echo "Hello world"
# outputs Hello world
?>
Strings containing quotes
Escape the inner quotes by escaping it with a backslash
<?
echo "He said, \"Hello world\""
# outputs He said, "Hello world"
?>

Or mix single quotes and double quotes:
<?
echo 'He said, "Hello world" '
# outputs He said, "Hello world"
?>

You can use either single or double quotes for the outer qoutes as long as the inner quotes are not the same and they are properly nested
Strings containing variable names
Variable names start with the special character, $. If you use a variable name within single quotes, the $ will be outputed as a literal $ instead of it indicating a variable. If you use it within double quotes and if it is escaped with a backslash, it will be outputed as a literal $. If it is within double quotes and is not escaped, it will be treated as a variable name.
<?
$var 
"Ford";
echo 
'Bill drives a $var<br>';
# outputs Bill drives a $var<br />
echo "Bill drives a \$var<br>";
# outputs Bill drives a $var<br />
echo "Bill drives a $var<br>";
# outputs Bill drives a Ford<br />
?>

Since you generate HTML with echo the same way you generate text, always remember to escape the quotes in HTML attributes:

<?
echo "<img src=\"logo.gif\" width=\"500\" height=\"200\" />";
?>

The same applies to the special character $ (used to name variables) and the special character \ (used to escape other special characters). With that in mind, what is wrong with the following lines?

<?
echo "Beans cost $1.29/lb<br />"
echo "Rice costs $0.67/lb";
?>

Answer

An example of a special combination character is new line which is \n. When is used in an echo statement that is double quoted, PHP will add a line break to the HTML source code. The viewer won't see it in your web page since it does not add a <br /> but having line breaks in your HTML source code is very useful for debuging. Similar combinations are \r (carriage returm) and \t (tab). In regular expressions there are many more combinations.

HEREDOC Syntax

Some times you have long strings that span several lines and have lot of quotes that need to be escaped. For those times, PHP has an alternative syntax that doesn't require that quotes be escaped. Here is a short example:

<?
$string 
= <<<HEREDOC
<font color="red">A lot of text with "quotes".</font>
HEREDOC;
?>

You can use any string that you want to enclose the text instead of HEREDOC as long as you use the same string at the end as at the begining. Double quotes and single quotes will not be seen as having meaning to PHP so you don't need to escape them but $ will and will need to be escaped. The first occurrence of the delimiting string, HEREDOC in this example, must be at the end of the line. Nothing must follow it on that line, not even a space. The final occurrence must be at the beginning of a line and followed by a simicolon and nothing else.


< ^ >



Created by DJ Mike from Santa Barbara

DJ Mike


Dance Away Santa Barbara's Home Page
<a href="http://www.statcounter.com/" target="_blank"> <img src="http://c5.statcounter.com/counter.php?sc_project=1321035&java=0&security=da2193dc" alt="counter free hit invisible" border="0" /></a>