Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP eval $a="$a"?

Tags:

php

eval

I was looking through some code for work, and came across this line:

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

I'm really confused as to why any PHP developer would've written this line. What purpose does this serve, besides setting a variable to itself?

Luckily, the function this line is in is never called.

like image 727
Rocket Hazmat Avatar asked Mar 17 '11 14:03

Rocket Hazmat


2 Answers

<?php
$string = 'cup';
$name = 'coffee';
$str = 'This is a $string with my $name in it.';
echo $str. "\n";
eval("\$str = \"$str\";");
echo $str. "\n";
?> 

The above example will output:

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

http://php.net/manual/en/function.eval.php

I do just ctl+c ctrl+v :-)

like image 199
zod Avatar answered Sep 22 '22 06:09

zod


It converts the value of the variable to a string, but I wouldn't recommend using it.

Use the function strval() instead. Have a look at the manual.

like image 29
Michiel Pater Avatar answered Sep 21 '22 06:09

Michiel Pater