In php, If I have
$ABC = "love";
echo $abc;
-----
Display: love
That's so okay. BUT if I have
$ABC = "$love"; //Have $ in the string.
echo $abc;
-----
Display: Unkown variable love
I want:
$ABC = "$love"; //Have $ in the string.
echo $abc;
-----
Display: $love //$love are all text character
How can I change $ become character, It will not be $ as the variable. Please help me.
Use \ to escape it, or just use single quote.
$ABC = "\$love";
// or
$ABC = '$love';
<?php
$la='$love';
echo $la;
echo '<br>';
$lb="\$lover";
echo $lb;
?>
outputs
$love
$lover
Learn the differences between single and double quotes when defining a string.!
Related
I have a PHP function that in echoing out a string, is losing some of the characters passed in it. Any reason why this would be happening?
I pass: $vhkvdov#jqlydk#p*_L#1qrlws|ufqh#KWLZ#1hwdgsX
It returns: #jqlydk#p*_L#1qrlws|ufqh#KWLZ#1hwdgsX
This is my PHP code:
<?php
function display($str) {
echo $str;
}
display("$vhkvdov#jqlydk#p*_L#1qrlws|ufqh#KWLZ#1hwdgsX");
?>
If you have $ in you string use single quotes:
display('$vhkvdov#jqlydk#p*_L#1qrlws|ufqh#KWLZ#1hwdgsX');
Otherwise PHP would try to use $vhkvdov as a variable name and replace it by its contents, what will be nothing as I expect that the variable is not set
PHP reference on strings
Note: During development I would set error_reporting in php.ini to
error_reporting=E_ALL | E_NOTICE;
and
display_errors=On
or
log_errors=On
error_log=PATH/TO/YOUR/LOG.file ; make sure this is writable by the web server
To see an error message like :
PHP Notice: Undefined variable: vhkvdov in
The problem is that you are using double quotes, which make PHP interpret the inner string for PHP variables.
Consider the following example:
$friend = "foo";
echo "hello $friend";
// Output: hello foo
Use single quotes instead if you do not want PHP to interpret the string contents.
$friend = "foo";
echo 'hello $friend';
// Output: hello $friend
Take a read of this to help clarify why this is happening:
http://php.net/manual/en/language.types.string.php
Specifically:
When a string is specified in double quotes or with heredoc, variables
are parsed within it.
This is whats happening with your code:
If a dollar sign ($) is encountered, the parser will greedily take as
many tokens as possible to form a valid variable name. Enclose the
variable name in curly braces to explicitly specify the end of the
name.
Here is an example:
<?php
$juice = "apple";
echo "He drank some $juice juice.".PHP_EOL;
// Invalid. "s" is a valid character for a variable name, but the variable is $juice.
echo "He drank some juice made of $juices.";
?>
The above example will output:
He drank some apple juice.
He drank some juice made of .
When you use single quotes, you will avoid this issue as the string taken litterally:
// 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';
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
I am trying to print a variable between curly braces as
Product_number{product_version}
I tried
echo "$product_number{$product_version}";
But that does not work. I don't understand why :(
try using double braces:
echo "$product_number{{$product_version}}";
You can also do:
echo "$product_number{".$product_version."}";
{ followed by $ is treated specially. It is mainly used when you want to append a string immediately at the end of a variable's value:
$v = 'hack';
echo "I {$v}ed it";
echo $product_number . "{" . $product_version . "}";
Escape the "{":
echo "$product_number\{$product_version}";
Can I store a special character inside a variable?
I mean something like:
<?php
$variable=' '
Then can i use something like echo $variable?
Yes, but don't take my word for it:
#!/path/to/php
<?
$variable = ' ';
echo $variable;
?>