note 98263 added to function.eval

From: Date: Fri, 04 Jun 2010 18:59:19 +0000
Subject: note 98263 added to function.eval
Groups: php.notes 
Request: Send a blank email to php-notes+get-169896@lists.php.net to get a copy of this message
Reply to john dot risken at gmail dot com: This is NOT the way to do this. Eval is far too risky for this purpose and there are much better ways to do what you are doing: The easiest is using $$theVarStem: <?php $emailName="me@example.com"; $theVarStem="emailName"; $theVarValue=$$theVarStem; echo $theVarValue; // me@example.com ?> This does exactly what you did without using eval. Note that this only works with $$var, not $foo$var. So to do things like $foo$var, you need to set $var='foo'.$var first and then use $$var. I think in nearly any circumstance, using an array would be even better, especially if you're doing this for more than one variable: <? $myarray['emailName']="me@example.com"; $theVarStem="emailName"; $theVarValue=$myarray[$theVarStem]; echo $theVarValue; // me@example.com ?> ---- Server IP: 212.124.37.9 Probable Submitter: 79.212.22.11 ---- Manual Page -- http://www.php.net/manual/en/function.eval.php Edit -- https://master.php.net/note/edit/98263 Del: integrated -- https://master.php.net/note/delete/98263/integrated Del: useless -- https://master.php.net/note/delete/98263/useless Del: bad code -- https://master.php.net/note/delete/98263/bad+code Del: spam -- https://master.php.net/note/delete/98263/spam Del: non-english -- https://master.php.net/note/delete/98263/non-english Del: in docs -- https://master.php.net/note/delete/98263/in+docs Del: other reasons-- https://master.php.net/note/delete/98263 Reject -- https://master.php.net/note/reject/98263 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#169896) next »