Re: manipulating constant name as string
| From: | Al | Date: | Mon, 02 Jan 2006 15:25:05 +0000 |
| Subject: | Re: manipulating constant name as string | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-228087@lists.php.net to get a copy of this message | ||
Tom Rogers wrote:
Hi, Monday, January 2, 2006, 4:37:17 AM, you wrote: DG> Hello DG> I am trying to access the constant name as string and not the constant DG> contents. For example, DG> define('myconst', 'const text 1'); DG> $myArray = array() DG> myArray['myconst'] = "this is it" DG> etc. DG> $xmltext=""; DG> foreach ($myArray as $key => $val) {A suggestion.. Make it a habit to use all caps for your constants. It makes little "gotchas" easier to spot. e.g., define('MYCONST', 'const text 1'); Then your array would be obvious: myArray[MYCONST] = "this is it"; verses myArray['myconst'] = "this is it";DG> echo "$key = $val\n\n"; DG> $xmltext .= "$xmltext = $xmltext . "<$key> $val </$key>";DG> } DG> Unfortunately, $key within the string concatenation is translated into DG> the constant value. However, for the echo statement, then $key constant DG> name is displayed. Is there a way to keep the constant name rather its DG> value when manipulating it as a string? DG> thanks, DG> Daniel drop the single quotes to use the constant: myArray[myconst] = "this is it"