Issues while using Quotes in PHP - 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';
?>

Related

echo php variable and html in Wordpress

I am playing around with wordpress and wondering, why this line of code works:
echo "<a href='....'>$name</a>";
I learned it this way:
echo "<a href='....'>".$name."</a>";
Is there something special defined in WP to make this work?
In PHP, there are two types of String.
The first type uses the single quotes, as follows.
$val = 'this is a simple string';
The second type is as follows:
$val = "This is a not so simple string";
With the latter type, any variables included in the string will be resolved to their values, so:
$val = "Hello there";
$message = 'Dave says $val';
// Literally equals: Dave says $val
$message2 = "Dave says $val";
// Literally equals: Dave says Hello there
There are lots of other differences, which you can read about here.

How to escape backslashes in files

I am trying to work on a script but I am stuck in one place.
Eg. To get a php output I have used..
str_php = """
<?php
echo "Hello World!";
?>"""
php_file = open("index.php", "w")
php_file.write(str_php)
php_file.close()
Ok, so I get the output as it is....
<?php
echo "Hello World!";
?>
So my php code is running. all good till here. But, the problem starts from when I try using "\" and "\n" and "\r"
str_php = """
<?php
echo "Hello World!"; \n echo "How are you"; \n echo "God bless you";
?>"""
php_file = open("index.php", "w")
php_file.write(str_php)
php_file.close()
But here I dont get the output as it is.
<?php
echo "Hello World!";
echo "How are you";
echo "God bless you";
?>
And the "\" it just vanishes... at an output.
Eg. I want an output of a php hyperlink something like...
str_php = """<?php
print("$dirArray[$index]");
?>"""
php_file = open("index.php", "w")
php_file.write(str_php)
php_file.close()
and the output I get is...
<?php
print("$dirArray[$index]");
?>
The "\" is missing and the php does not run creating error.
print("$dirArray[$index]") - Original
print("$dirArray[$index]") - python output
Can any one help me out with "\", "\n", "\r" ??
Just use "\" to escape the "\" character.
Since it is common to want to have long strings containing several "\", Python also allows one
to prefix the string opening quotes if ar r (for "raw") - inside such
a string, no escaping of "\n" to chr(10) or "\t" to chr(9) happens:
>>> print (r"Hello \n world!")
Hello \n world!
You need to escape your "\" with another backslash writting it as "\\".
If you use "\n" it will be parsed and make a newline. Try to use '\n', strings enclosed in '\n' are not parsed and it should print out as you want it to.

PHP string inception manipulation

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>";

What is the difference between double and single quotes in PHP? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Difference between single quote and double quote string in php
I am new to PHP and in programming i have seen use of both "" and ' '.
What is the difference between "" and ' '?
And in declaring a link i have used the following ,but it does not seem to work there must be something wrong in quotation:
$abc_output .='' Back to Main Menu'';
echo $abc_output;
What might be the error here?
You want to keep the text inside of your string:
$abc_output .='Back to Main Menu';
The difference between ' and " is that you can embed variables inside of a double quoted string.
For example:
$name = 'John';
$sentence = "$name just left"; // John just left
If you were to use single quotes, then you'd have to concatenate:
$name = 'John';
$sentence = $name.' just left'; // John just left
PS: Don't forget that you always have to escape your quotes. The following 2 are the same:
$double = "I'm going out"; // I'm going out
$single = 'I\'m going out'; // I'm going out
Same applies the other way around:
$single = 'I said "Get out!!"'; // I said "Get out!!"
$double = "I said \"Get out!!\""; // I said "Get out!!"
Double quotes allow additional expressions, such as "$variable \n", which single quotes don't. Compare:
$variable = 42;
echo "double: $variable,\n 43\n";
echo 'single: $variable,\n 43';
This outputs:
double: 42,
43
single: $variable,\n 43
For more information, refer to the official documentation.
Text in double quotes are parsed.
For example:
$test = 'something';
print('This is $test');
print("This is $something");
would result in:
This is $test
This is something
If you don't need the string to be parsed you should use single quotes since it's better performance wise.
In your case you need to do:
$abc_output .='Back to Main Menu';
echo $abc_output;
Or you will get an error.
The Back to Main Menu wasn't in the string.

How to echo an echo in php?

I have some basic PHP code:
$raceramps56["short"] = "My Test Product";
$leftMenu =
'<div class="leftMenuProductButton"><?PHP echo $raceramps56["short"] ?></div>';
Won't echo the PHP code, only the element. I've tried things like
<div class="leftMenuProductButton">' . <?PHP echo $raceramps56["short"] ?>. '</div>';
Which just returns Parse error: parse error, unexpected '<'
So my question is, how do I either get this to work, or take another approach?
try this
$raceramps56["short"] = "My Test Product";
$leftMenu ='<div class="leftMenuProductButton">'.$raceramps56["short"].'</div>';
I thought I'd provide some extra information just so you understand.
In your code:
$raceramps56["short"] = "My Test Product";
$leftMenu =
'<div class="leftMenuProductButton"><?PHP echo $raceramps56["short"] ?></div>';
You are including literally.
Take a read of this. http://php.net/manual/en/language.types.string.php
When I was first learning, I did not understand the different between literal ' and double quotes and it especially caused problems when I was trying to echo things.
Take a look at this:
<?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';
?>
If you were to use " " instead of ' you would not get the same output because " will interpret everything rather then take it literally.
I hope this has been of additional help, even though you have had your question answered already.
You can run php in a '. You can only echo it like this if you know what I mean.
$leftMenu ='<div class="leftMenuProductButton">.$raceramps56["short"].</div>';
echo $leftMenu;
Use this:
$leftMenu ='<div class="leftMenuProductButton">'.$raceramps56["short"].'</div>';
Or without the need of escaping double quotes:
$leftMenu = '<div class="leftMenuProductButton">' . $raceramps56["short"] . '</div>';
$raceramps56["short"] = "My Test Product";
$leftMenu = '<div class="leftMenuProductButton">' . $raceramps56["short"] . '</div>';
echo $leftMenu;

Categories