Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP : Using single quotes and double quotes in single string

Tags:

php

How can i use string like below code.

$str = 'Is yo"ur name O'reil"ly?';

The above code is just an example..I need to use big html template which contains single and double quotes.I tried Addslashes php method but when i use single and double quote string in that function i get syntax error.Please help me.

Note : my realtime usage is json data like below.

    $string = "

    <html>
    ...
    <b:if cond='data:blog.pageType == "item"'>
    ..

    ";
   $string = '{"method":"template","params":{"1":"'.$string.'"},"token":"12345"}';
like image 970
user2425700 Avatar asked May 28 '13 13:05

user2425700


3 Answers

You can use a heredoc for that:

$string = <<<EOM
    <html>
    ...
    <b:if cond='data:blog.pageType == "item"'>
    ..
EOM;

If you wish to prevent variable interpolation as well you can use nowdoc (since 5.3):

$string = <<<'EOM'
    <html>
    ...
    <b:if cond='data:blog.pageType == "item"'>
    ..
EOM;

Both heredoc and nowdoc have specific formatting requirements, so be sure to read the manual properly.

like image 182
Ja͢ck Avatar answered Oct 21 '22 05:10

Ja͢ck


I used heredoc to do the same like

$string = <<< EOF
'Is yo"ur name O'reil"ly?'
EOF;
like image 34
a4arpan Avatar answered Oct 21 '22 06:10

a4arpan


You can use heredoc like this:

$str = <<< EOF
'Is yo"ur name O'reil"ly?'
EOF;
like image 37
anubhava Avatar answered Oct 21 '22 05:10

anubhava