How can I store PHP code inside a 'heredoc' variable? - php

I would like to store the following code inside a HEREDOC variable:
<?php
$var = 'test';
echo $var;
?>
like this:
$hered = <<<HERED
<?php
$var = 'test';
echo $var;
?>
HERED;
The problem is that HEREDOC works like double quotes, "" - that means each dollar sign ($) has to be replaced with \$...
Is there a way to use HEREDOC without performing such an operation?

Yes, there is. Check out the nowdoc syntax:
$hello = 'hey';
$echo <<<'EOS'
$hello world!
EOS;
//Output: $hello world

It can be done using \:
echo <<<HEREDOC
<?php
\$var = 'test';
echo \$var;
?>
HEREDOC;
I know there's now doc, but for example my current use case needs heredoc, because some dollars need to be escaped and some not:
$variableNotToEscape = 'this should be output';
echo <<<HEREDOC
$variableNotToEscape
\$variableNotToEscape
HEREDOC;
Which returns
this should be output
$variableNotToEscape

Related

Escaping $ in HEREDOC string with PHP

I have something like this
$var = <<<HEREDOC
..."request": ${"hello"}...
HEREDOC;
Of course PHP thinks ${"hello"} is a variable and it fails to load.
How to escape the $?
The same way you escape almost anything in a string
$var = <<<HEREDOC
..."request": \${"hello"}...
HEREDOC;
echo $var;
RESULT
..."request": ${"hello"}...
Is that what you want?
$var = <<<HEREDOC
..."request": \${"hello"}...
HEREDOC;

PHP : Using single quotes and double quotes in single string

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"}';
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.
I used heredoc to do the same like
$string = <<< EOF
'Is yo"ur name O'reil"ly?'
EOF;
You can use heredoc like this:
$str = <<< EOF
'Is yo"ur name O'reil"ly?'
EOF;

Issues while using Quotes in PHP

I have Learnt that Quotes doesn't matter in PHP.
But in the following code, if I try to use single quotes in eval(); I get error, on the other hand code works fine with Double Quotes.
$a = '2';
$b = '3';
$c = '$a+$b';
echo $c.'<br/>';
eval("\$c = \"$c\";");
//eval('\$c = \'$c\';'); //Parse error: syntax error, unexpected T_VARIABLE, expecting T_STRING
echo $c;
PHP.net says that escape sequences are not expanded when using single quotes.
Quotes do matter ;-)
<?php
$color = "red";
echo "My car is $color"; // Outputs "My car is red"
echo 'My car is $color'; // Outputs "My car is $color"
?>
Unlike double quotes, PHP does not parse variables in single quotes.
Example:
$name = 'John';
echo 'hello $name'; // hello $name
echo "hello $name"; // hello John
More Information
FYI, it isn't good idea to use eval in production environment for security reasons.
Using eval is a bad idea but if you are doing this for learning purpose then the correct way is
eval("\$c = \$c;");
.
don't use eval and update your string-quoting skills here:
The following example was lifted from: The PHP Manual
<?php
echo 'this is a simple string';
echo 'You can also have embedded newlines in
strings this way as it is
okay to do';
// Outputs: Arnold once said: "I'll be back"
echo 'Arnold once said: "I\'ll be back"';
// Outputs: You deleted C:\*.*?
echo 'You deleted C:\\*.*?';
// Outputs: You deleted C:\*.*?
echo 'You deleted C:\*.*?';
// Outputs: This will not expand: \n a newline
echo 'This will not expand: \n a newline';
// Outputs: Variables do not $expand $either
echo 'Variables do not $expand $either';
?>

Echoing variables in PHP without calling them

Let's say I have something like this:
echo "This is a $variable";
echo "<?php echo $var; ?>";
How can I make it simply output:
This is a $variable
<?php echo $var; ?>
Instead of trying to parse the variables?
Use single-quotes rather than double-quotes:
$var = 'This is a $variable';
echo $var;
Output:
This is a $variable
Use single quotes
echo 'This is a $variable';
echo '<?php echo $var; ?>';
Use single-quotes instead of double-qoutes:
echo '<?php echo $var; ?>';
If you want to show it in a web browser, use htmlentities so it's not interpreted as a html tag:
echo htmlentities('<?php echo $var; ?>');
Use single quotes
echo 'my vars = $a, $b, $c';
or if you need double quotes escape $ sign with slash in front of it like:
echo "my vars = \$a, \$b, \$c";
or use chr(36) to replace $ sign (my fav :) ), or.... etc

How to handle newlines in Javascript? (from PHP)

I have code like this:
<?php
echo '<script type="text/javascript">';
echo 'var out="'.$txt.'";';
echo '</script>';
?>
Where $txt is a PHP variable that can contain newlines like this:
line1
line2 hello world
Which would end up like this:
var out="line1
line2 hello world";
Which will cause a Javascript error, of course.
What is the best way to handle this? The out variable will be used in a HTML textarea, so I don't think it can be parsed into <br>
$txt = str_replace( array( "\n", "\r" ), array( "\\n", "\\r" ), $txt );
should replace newlines. Don't do it this way.
This is a naïve implementation of string escaping for JavaScript. As you're actually trying to format a string for use in JavaScript, a much better solution would be to use json_encode:
$txt = json_encode($txt);
echo "<script>var out={$txt};</script>";
json_encode will correctly escape special characters in strings, such as quotes, tabs, form feeds, and other special unicode characters. It will also perform all the correct escaping for converting objects, arrays, numbers, and booleans.
you can add a \ at the end of a line to create a multi line String
var out="line1 \
line2 hello world";
Most of these don't work for me.
Normally, I'd use json_encode like
<?php
$MyVar = json_encode($MyVar);
?>
<javascript language='javascript'>
MyVar = <?php echo $MyVar; ?>
But for a quick fix you can just break a line like this:
Pay attention to the double quotes.
<?php
$MyVar = "line one here
then line two here
finally line five here";
//** OR
$MyVar = $MyVarA .
"
"
. $MyVarB;
?>
<HTML>
<HEAD>
<javascript language='javascript'>
Myvar = "<?php echo $MyVar; ?>";
. . .
You can use str_replace to convert line breaks into a different character (in this case, perhaps a space, but it depends how you want the output to show up)
$out = str_replace("\n", '\n', $in);
$content = str_replace( "\\n", "\\\\\\n", $content );
Result:
var a = "Hello \
World"
I tried this and it worked well.
<?php
echo '<script type="text/javascript">';
$txt = "line1 \\n line2 hello world";
echo 'var out="'.$txt.'";';
echo '</script>';
?>
I am using PHP 5.3

Categories