3
Intro to PHP – Page 13 of 43CSCI 2910 – Client/Server-Side Programming
print Examples
print "Hello, World";
print 123; // Outputs "123"
$outputString = "Hello, World!";
print $outputString;
Intro to PHP – Page 14 of 43CSCI 2910 – Client/Server-Side Programming
echo Statement
• The echo statement similar to print
• echo, however, can take on a sequence of
arguments separated by commas.
• Example:
$outputString = "The answer is ";
echo $outputString, 42, "!";
• This outputs "The answer is 42!"
• print cannot combine elements like this.
Intro to PHP – Page 15 of 43CSCI 2910 – Client/Server-Side Programming
Escape Characters
• Because of the number of reserved characters in
PHP, escaping is necessary.
• Characters are escaped by preceding them with a
backslash (\).
• Characters that need escaped include ', ", \, $, and ?.
• Whitespace including carriage returns are allowed as
part of a string, but they are then output as part of the
string. Of course, in HTML, carriage returns are
considered whitespace and are ignored.
• As in JavaScript, single quotes can be used without
escaping within double quoted strings and vice versa.
Intro to PHP – Page 16 of 43CSCI 2910 – Client/Server-Side Programming
Printing Characters Not Available on Keyboard
• Escaping can also be used to display
ISO-8859-1 characters that are not present on the
keyboard.
• This is done by taking the ISO-8859-1 hex value and
placing it after "\x".
• The ISO-8859-1 hex values can be found using the
Character Map application found in Accessories Æ
System Tools.
• For example, the character "¼" has the hexadecimal
ISO-8859-1 value bc
16
. This can be represented with
\xbc.
• print "\xbc tsp" prints the string "¼ tsp"
Intro to PHP – Page 17 of 43CSCI 2910 – Client/Server-Side Programming
In-Class Exercise
• Earlier it was stated that the PHP script engine does
not like the tag
<?xml version="1.0" encoding="ISO-8859-1"?>.
• How might we still incorporate this tag in the file we
send to the browser without causing problems for
the PHP engine?
Intro to PHP – Page 18 of 43CSCI 2910 – Client/Server-Side Programming
Variable Declarations
• PHP interprets the dollar sign ($) followed by a
letter or an underscore (_) to be a variable name.
• Variables do not need to be declared before you
use them.
• Example: $var1 = 25;
• To help set off a variable identifier within a string,
you can surround it with curly brackets.
• This will become helpful when we start discussing
arrays and objects.
• Example: echo "The value is {$var1}." will display
"The value is 25."