Tuesday, June 26, 2012

PHP Learning - Working with Data(3)

Use special characters in strings:
php provides some special characters you can use in string: /n and /t
$string = “NBA \nPlayer”;
echo $string;
you will get the result :
NBA
Player

$string = “Line 1 \n\tLine 2”;
echo $string;
you will get the result:
Line 1
Line 2

These special character can only be used with double quotes, I will explain the differences between single-quoted string and double-quoted string
If you enclose a variable in double quotes, PHP uses the value of the variable.
However, if you enclose a variable in single quotes, PHP uses the literal variable. To illurstrate, see the following example:
$name ="Yao Ming";
$output1 = "$name";
$output2 = '$name';
echo $output1;
echo $output2;
you will get the result
Yao Ming
$name

Sometimes you want a character in a double-quoted string to be treated as a
literal, not as a special character, even though it has special meaning.
You can tell PHP to output characters, rather than use their special meaning, by preceding
the character with a backslash (\). This is called escaping the character.
For example, the following two strings produce the same output:
$var1="music";
$string = "The variable name is \$var1";
echo $string;
you will get the result: "The variable name is $var1" instead of "The variable name is music"


View the original article here

No comments:

Post a Comment