February 19 2014

PHP Convert a String to Variable

Tagged Under :

php
Have 2 methods to do it.

Method 1:
echo eval('return $'. $string . ';');

And another method as below:
$counter = 1;
$button1 = "Hello PHP";
$printbutton = ${'button' . $counter};

echo $printbutton
The Method 2 example will print out Hello PHP

Make a Comment

You must be logged in to post a comment.