PHP strings can be specified in four ways.
1) Single quoted strings:
- This type of strings will display things completely "as is."
- Variables and escape sequences will not be interpreted. The exception is that to display a literal single quote, you can escape it with a back slash \', and to display a back slash, you can escape it with another backslash \\
- So yes, even single quoted strings are parsed.
2) Double quote strings:
- This type of strings will display a host of escaped characters (including some regexes), and variables in the strings will be evaluated.
- An important point here is that you can use curly braces to isolate the name of the variable.
- For example let's say we have the variable $abc and we want to echo "The $abc are", That will look for the variable $abc.
To get around this use echo "The {$abc} is" You can put the brace before or after the dollar sign.
Let us discuss an example for this,
$abc="test";
echo "test$abc"; // output = testtest
echo "test$abcaa"; // output = test
As $ is in Double quote, system will try to find remaining string as a variable. Like in first example $abc = "test"; and we request for "test@abc" so it will result as testtest. In second example system will try to find $abcaa variable and its not declared so it will consider as an empty string and result as test only.
So if we need to write test$abc as a string we need to use,
$abc="test";
echo 'test$abc'; // output = test$abc
echo 'test$abcaa'; // output = test$abcaa
Note: Single quotes being faster than double quotes in many situations.
3) Heredoc:
- String syntax works like double quoted strings.
- It starts with <<<. After this operator, an identifier is provided, then a newline.
- The string itself follows, and then the same identifier again to close the quotation. You don't need to escape quotes in this syntax.
<?php
var_dump(array(<<<EOD
foobar!
EOD
)); exit;
?>
4) Nowdoc:
- PHP 5.3.0+
- String syntax works essentially like single quoted strings.
- The difference is that not even single quotes or backslashes have to be escaped. A nowdoc is identified with the same <<< sequence used for heredocs, but the identifier which follows is enclosed in single quotes, e.g. <<<'EOT'. No parsing is done in nowdoc.
<?php
class foo {
public $bar = <<<'EOT'
bar
EOT;
}
?>
Visit http://php.net/manual/en/language.types.string.php for more information..
1) Single quoted strings:
- This type of strings will display things completely "as is."
- Variables and escape sequences will not be interpreted. The exception is that to display a literal single quote, you can escape it with a back slash \', and to display a back slash, you can escape it with another backslash \\
- So yes, even single quoted strings are parsed.
2) Double quote strings:
- This type of strings will display a host of escaped characters (including some regexes), and variables in the strings will be evaluated.
- An important point here is that you can use curly braces to isolate the name of the variable.
- For example let's say we have the variable $abc and we want to echo "The $abc are", That will look for the variable $abc.
To get around this use echo "The {$abc} is" You can put the brace before or after the dollar sign.
Let us discuss an example for this,
$abc="test";
echo "test$abc"; // output = testtest
echo "test$abcaa"; // output = test
As $ is in Double quote, system will try to find remaining string as a variable. Like in first example $abc = "test"; and we request for "test@abc" so it will result as testtest. In second example system will try to find $abcaa variable and its not declared so it will consider as an empty string and result as test only.
So if we need to write test$abc as a string we need to use,
$abc="test";
echo 'test$abc'; // output = test$abc
echo 'test$abcaa'; // output = test$abcaa
Note: Single quotes being faster than double quotes in many situations.
3) Heredoc:
- String syntax works like double quoted strings.
- It starts with <<<. After this operator, an identifier is provided, then a newline.
- The string itself follows, and then the same identifier again to close the quotation. You don't need to escape quotes in this syntax.
<?php
var_dump(array(<<<EOD
foobar!
EOD
)); exit;
?>
4) Nowdoc:
- PHP 5.3.0+
- String syntax works essentially like single quoted strings.
- The difference is that not even single quotes or backslashes have to be escaped. A nowdoc is identified with the same <<< sequence used for heredocs, but the identifier which follows is enclosed in single quotes, e.g. <<<'EOT'. No parsing is done in nowdoc.
<?php
class foo {
public $bar = <<<'EOT'
bar
EOT;
}
?>
Visit http://php.net/manual/en/language.types.string.php for more information..
No comments:
Post a Comment