Re: Dynamic constant names

From: Date: Tue, 27 Mar 2001 16:45:51 +0000
Subject: Re: Dynamic constant names
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-45739@lists.php.net to get a copy of this message
In article <164314669380.20010327141859@britishgoldsmiths.com>, geoff@productivity.co.uk (Geoff Caplan) wrote: > I am trying to create a constant name dynamically and then get at the > value. > > // We set up the constant > define( CONSTANT_1 , "Some value" ) ; > > // Later we try to use it dynamically ... > $constant_number = 1 ; > $constant_name = ( "CONSTANT_" . $constant_number ) ; > > // We try to assign the constant value to a variable... > $constant_value = $constant_name ; > > But we find that $constant value still contains the NAME of the > constant, and not the VALUE. > > Am I misunderstanding something? Is there any way that this can be done? Your error_reporting level must not be all the way up to E_ALL, because when I tried your code PHP reported errors that might have helped you solve the problem. Try this: // We set up the constant define("CONSTANT_1", "Some value" ) ; //constant name must be in quotes // Later we try to use it dynamically ... $constant_number = 1 ; eval( "echo CONSTANT_$constant_number;" ) ;//tell PHP to treat the name as code not as a text string -- CC

« previous php.general (#45739) next »