note 24534 modified in language.variables.variable by danbrown

From: Date: Wed, 24 Jun 2009 16:59:30 +0000
Subject: note 24534 modified in language.variables.variable by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-157412@lists.php.net to get a copy of this message
You can also use variable variables and the string concat operator to generate suffixed (or prefixed) variables based on a base name. For instance, if you wanted to dynamically generate this series of variables: base1_suffix1 base1_suffix2 base2_suffix1 base2_suffix2 base3_suffix1 base3_suffix2 You can do this: <?php $bases = array('base1', 'base2', 'base3'); $suffixes = array('suffix1', suffix2); foreach($bases as $base) { foreach($suffixes as $suffix) { ${$base.$suffix} = "whatever"; #...etc } } ?> --was-- You can also use variable variables and the string concat operator to generate suffixed (or prefixed) variables based on a base name. For instance, if you wanted to dynamically generate this series of variables: base1_suffix1 base1_suffix2 base2_suffix1 base2_suffix2 base3_suffix1 base3_suffix2 You can do this: $bases = array('base1', 'base2', 'base3'); $suffixes = array('suffix1', suffix2); foreach($bases as $base) { foreach($suffixes as $suffix) { ${$base.$suffix} = "whatever"; #...etc } } http://php.net/manual/en/language.variables.variable.php

« previous php.notes (#157412) next »