Re: variable string names

From: Date: Sun, 09 Mar 2003 10:31:50 +0000
Subject: Re: variable string names
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-138847@lists.php.net to get a copy of this message
John's solution is the better one. But if you really wish to stick to your own, try and use eval() to complile $where$i. Edwin John W. Holmes wrote:
I have: if ($where1 != '') { $whereArray = array_push($whereArray, $where1); } and I want to repeat for $where1 up to $where8 but rather than write it out 8 times, I'd rather use a loop for ($i=1; $i<=8 i++) { if ($where1 != '') { $whereArray = array_push($whereArray, $where1); } } but how can I change $where1 to $where2, $where3 etc using $i. nothing I try seems to work. Do I use eval?
Yes, you can use variable variables, like others have suggested, but why not just use an array? Almost every implementation of variable-variables seems to be a work around that's used instead of arrays. Where are all of your $where variables coming from? If they are from a form, just name them as "where[]", then simply loop through them using foreach($_POST['where']) and check them all. This makes your code more dynamic as you can add/subtract "where[]" elements and your "processing" code doesn't change. With a variable-variable solution, you have to change the number of loops you run each time to change the number of "where" elements you have. ---John Holmes...


« previous php.general (#138847) next »