PHP: using single quotes and double quotes in one line

How can I use the string as shown below.

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

The above code is just an example. I need to use a large html template that contains single and double quotes. I tried the Addslashes php method, but when I use one and two quotes in this function, I get a syntax error. Please help me.

Note: my real-time use is json data as below.

  $string = " <html> ... <b:if cond='data:blog.pageType == "item"'> .. "; $string = '{"method":"template","params":{"1":"'.$string.'"},"token":"12345"}'; 
+4
source share
3 answers

You can use heredoc for this:

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

If you also want to prevent variable interpolation, you can use nowdoc (starting with 5.3):

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

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

+7
source

You can use heredoc as follows:

 $str = <<< EOF 'Is yo"ur name O'reil"ly?' EOF; 
+1
source

I used heredoc to do the same thing as

 $string = <<< EOF 'Is yo"ur name O'reil"ly?' EOF; 
+1
source

Source: https://habr.com/ru/post/1483165/


All Articles