user3021621
user3021621

Reputation: 341

How to write a variable in a double quoted string to be treated as a variable by eval()

Here is a code where I don't understand why the PHP code where the output is:

This is a $string with my $name in it. This is a cup with my coffee in it.

My code:

<?php
$string = 'cup';
$name = 'coffee';

$str = 'This is a $string with my $name in it.';

// will not echo the value of the strings variable because there in ' '
echo $str. "\n";

// this function is like writing the php code outside of it
// it gets a string with php statments (;) 
// because the code is written in a string
// if it is written it double quotes you have to escape $ and " 
// and if it is written in single quotes you have to escape '

eval("\$str = \"$str\";");

//it is not like this, why?????
//eval('$str = "$str";');

// and not like this, why???????
//$str = "$str" ;

echo $str. "\n";
?>

Why doesn't the statement: eval('$str = "$str";'); or the statement: $str = "$str"; do the same thing as the statement: eval("\$str = \"$str\";"); in this code?

Upvotes: 0

Views: 3500

Answers (4)

Why would you need eval in this context ?

Variables inside single quotes will not be interpreted , Instead put it under double quotes.

$str = "This is a $string with my $name in it."; //<--- Replaced single quotes to double quotes.

Secondly.. If you are really worried about escaping why don't you make use of a HEREDOC Syntax

<?php
$string = 'cup';
$name = 'coffee';

$cont=<<<ANYCONTENT
This is a $string with my $name in it. This text can contain single quotes like this ' and also double quotes " too.

ANYCONTENT;

echo $cont;

OUTPUT :

This is a cup with my coffee in it. This text can contain single quotes like this ' and also double quotes " too. 

Upvotes: 0

manishi
manishi

Reputation: 1

In the first eval statement:

eval("\$str = \"$str\";");

As second $ is not escaped, and you are using double quotes over the entire arguement, so second $str's value is passed to the eval, and the argument of eval becomes:

eval("\$str = \"This is a $string with my $name in it.\";");

which when evaluated, becomes:

$str = "This is a $string with my $name in it.";

Which assigns 'This is a cup with my coffee in it.' to $str.

In the second eval:

eval('$str = "$str";');

the statement evaluated is:

$str = "$str";

Which is same as your third statement. When this statement is executed, it converts non-strings to strings. In this case, $str is already a string, so this statement has no effect on the value of $str.

Hope this helps. :)

Upvotes: 0

Pheonix
Pheonix

Reputation: 6052

A Double quoted string evaluates all the variables inside it. A Single Quoted String does not.

Now to this statement

eval("\$str = \"$str\";");

first \$str -> the $ is escaped, so its a literal, and not the $str variable

second $str -> the $ is not escaped and the whole string is in double quotes, so this will become

$str = "This is a $string with my $name in it."

Now this PHP code is evaluated, which assigns the string on right to the variable on left. Hence $str becomes what This is a cup with my coffee in it.

Eval should be avoided.

Upvotes: 2

Conkreet
Conkreet

Reputation: 1

//it is not like this, why?????
//eval('$str = "$str";');

Because the input string might contain single quotes, so you can't use them to start and end the string.

// and not like this, why???????
//$str = "$str" ;

Because you want to evaluate a string, and the above is no string.

I don't see the point of this example, just use double quotes:

<?php
$string = 'cup';
$name = 'coffee';

$str = "This is a $string with my $name in it.";

echo $str. "\n";
?>

Upvotes: 0

Related Questions