Page 36 - Open Soource Technologies 304.indd
P. 36
Web Technologies-I
Notes 2.2.4 Special Characters
Anything within quotes is part of a string, even numbers. Thus, “123” is a string, not a number.
But there are special characters which cannot be included directly in a quote. For instance, you
cannot have double quotes inside a double-quoted string, and you cannot have single quotes
inside a singe-quoted string. To include these special characters, you need to escape them so that
the parser knows that they are literal characters and not string delimiters.
In PHP, you escape a character by preceding it with a backslash. There are two types of escaped
characters in PHP. There are those that are a special character in the language and need to be
escaped to get the parser to treat them as a literal. For instance, \” says that the quote after the
backslash is a literal quote, and \\ is used to include a literal backslash in a string. There are
also those that are normal characters that serve as special characters when they are escaped. For
instance, \n inserts a line break in the string at that location, and \r inserts a carriage return. Yes,
they are the same thing to us, but they are different characters as far as the computer is concerned.
Some of the more common escape characters are:
\” embeds a literal double quote in a string
\’ embeds a literal single quote or apostrophe in a string
\\ embeds a literal backslash in a string
\$ embeds a literal dollar sign in a string
\ { embeds a literal left brace in a string
\} embeds a literal right brace in a string
\[ embeds a literal left bracket in a string
\] embeds a literal right bracket in a string
\n embeds a new line character in a string
\r embeds a carriage return character in a string
\0 through \777 represents and embeds any valid ASCII character value in octal format.
\x0 through \xff represents and embeds any valid ASCII character value in hexadecimal format.
\x0 through \xffff represents and embeds any valid Unicode character value in hexadecimal
format.
Strings in single quotes are not technically parsed, but you can use \’ and \\ to escape single
quotes and backslashes in unparsed strings. No other escaped characters will work in single-
quoted string literals. They will be treated as literal text.
One important thing you should not have in a string is the sub-string?>, which ends that PHP
script, even if it does occur inside a string. In fact, even ? \> is a bad idea. It is recommended
that you use the numeric value for the question mark and write it \x3f>. 3f is the hex value for ?.
You can use the is_string() function to test whether something is a string. If it is a string, it will
return true.
PHP includes many string related functions, since much of what it does is string processing.
Special characters can either be a single character preceded by a backslash
or a numeric value in either octal or hexadecimal preceded by a backslash.
30 LOVELY PROFESSIONAL UNIVERSITY