Alternative way to write string literals in PHP? (without ' or ") -
what use in php in place of normal ' , " symbols around something?
example:
echo("hello world!") thanks!
there 4 ways encapsulate strings, single quotes ', double quotes ", heredoc , nowdoc.
read full php.net article here.
heredoc
a third way delimit strings heredoc syntax: <<<. after operator, identifier provided, newline. string follows, , same identifier again close quotation.
http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
$str = <<<eod example of string spanning multiple lines using heredoc syntax. eod; nowdoc
nowdocs single-quoted strings heredocs double-quoted strings. nowdoc specified heredoc, no parsing done inside nowdoc. construct ideal embedding php code or other large blocks of text without need escaping. shares features in common sgml construct, in declares block of text not parsing.
a nowdoc identified same <<< sequence used heredocs, identifier follows enclosed in single quotes, e.g. <<<'eot'. rules heredoc identifiers apply nowdoc identifiers, regarding appearance of closing identifier.
http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc
$str = <<<'eod' example of string spanning multiple lines using nowdoc syntax. eod; escaping
if want use literal single or double quotes within single or double quoted strings, have escape them:
$str = '\''; // single quote $str = "\""; // double quote as herbert noted, don't have escape single quotes within double quoted strings , don't have escape double quotes within single quoted string.
if have add quotes on large scale, use addslashes() function:
$str = "is name o'reilly?"; echo addslashes($str); // name o\'reilly?
Comments
Post a Comment