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
Related
I want to insert a URL all into single quotes. I have tried using both single and double quotes, but it is not working. I checked this question - Escaping single quote in URL link, but it did not help:
<?php
echo '\'' base_url().'\'index.php?admin/product/delete/1\'';
echo '\''; echo base_url().index.php?admin/product/delete/1\'';
echo '\'. base_url().index.php?admin/product/delete/1\'';
echo "\'"; echo base_url().'index.php?admin/product/delete/1\'";
?>
I am trying to achieve this:
'http://localhost/my_site/index.php?admin/product/delete/1'
The URL in single quotes.
If you really want to use only single quotes, try this:
echo '\'' . base_url() . 'index.php?admin/product/delete/1\'';
Just simple:
echo "'" . base_url() . "index.php?admin/product/delete/1'";
You can use double-quotes to pass variables into strings in PHP. On top of enhancing readability, this also saves you the trouble of escaping single quotes.
So for example:
<?php
$foo = 'bar';
echo 'single-quoted string: $foo <br />';
echo "double-quoted string: $foo <br />"; // "$foo" works, "{$foo}" is better
echo "double-quoted string: \$foo <br />"; // Backslash escapes
?>
Prints (emphasis mine):
single-quoted string: $foo
double-quoted string: bar
double-quoted string: $foo
So to update your example:
<?php
/* ... */
$baseurl = base_url();
echo "'{$baseurl}index.php?admin/product/delete/1'";
?>
Or how about we put it all into a variable and gain a little readability:
<?php
/* ... */
$baseurl = base_url();
$fullurl = $baseurl . 'index.php?admin/product/delete/1';
echo "'{$fullurl}'";
?>
echo "'" . base_url() . "index.php?admin/product/delete/1'";
I am trying to output the results from a field that is read from mysql.
the field has this in it.
< ? echo "yes"; ? >
however when i try to print the field... it is null.
do i have to escape it on output?
i dont want to execute the code.. i want to output what is in the field as text
You have to escape with htmlspecialchars()
$val = htmlspecialchars ('<?php echo "yes"; ?>');
echo $val;
I guess htmlspecialchars is that what you want:
<?php
$str = "<?php echo 'lol'; ?>";
echo htmlspecialchars($str);
?>
it outputs <?php echo 'lol'; ?>
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
I cant get this to work and its driving me mad, pls help
echo "<a href='#' onclick='javascript:$.jGrowl
(\"".$_SESSION['product_description'][$i]."\")' >?</a>";
the problem comes down to the 'product_description' - those single ' marks are breaking it, what should i do sigh
EDIT: if i replace the .$_SESSION['product_description'][$i]. with a bunch of charecters it works, its not a problem with anything but PHP and those ''
Chances are the real problem lies with the $.jGrowl. Within double quotes, PHP tries to parse found variables. e.g.
$foo = 'foo';
echo "This is foo: $foo"; // output: This is foo: foo
So, to avoid this you need to escape the $ using \$ within the string...
echo "...\$.jGrowl..."
See this demo.
Keep this here for reference:
Escape them with a backslash, like you would with double quotes. e.g.
// which ever quote is used to encapsulate the string
// must be escaped within the output.
echo 'Hello, \'world!\''; // output: Hello, 'world!'
echo "Hello, \"world!\""; // output: Hello, "world!"
// but, if you use the opposite quote, it does not need
// to be escaped for output.
echo 'Hello, "world!"'; // output: Hello, "world!"
echo "Hello, 'world!'"; // output: Hello, 'world!'
See the PHP Docs on strings for more information and what characters need escaping.
Are you missing your opening "?
echo "<a href='#' onclick='javascript:$.jGrowl(\"".$_SESSION['product_description'][$i]."\")' >?</a>";
Make it
echo "<a href='#' onclick='javascript:$.jGrowl(\"".$_SESSION['product_description'][$i]."\")' >?</a>";
Try:
echo "<a href='#' onclick='javascript:\$.jGrowl(\"".str_replace('"','\"',$_SESSION['product_description'][$i])."\");' >?</a>";
You need to ensure you are escaping any double quotes within the value...
This one works for me:
echo "<a href=\"#\" onclick=\"javascript:$.jGrowl('".$_SESSION['product_description'][$i]."')\" >?</a>";
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