Forum Moderators: coopster

Message Too Old, No Replies

how to get value from variable

         

toplisek

1:07 pm on Oct 10, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I have set variables like:
$variables['myvariable1'] = "Value variable1";

How to retrieve this value in the same PHP file to be used also in another variable name like:
$variables['myvariable2'] = "My settings are: .'$variables['myvariable1']'";

Code is just sample with errors just to show meaning.

So, Value variable1 will be also inside another variable.

penders

1:47 pm on Oct 10, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



$variables['myvariable2'] = "My settings are: .'$variables['myvariable1']'";


You aren't simply retrieving the value, but making it part of another string value. In this case you are probably better off using string concatenation (. period). Which you seem to be trying to use above, but in the wrong place...

$variables['myvariable2'] = "My settings are: '" . $variables['myvariable1'] . "'";


(I've purposefully spaced out the string concatenation operator (period) in the code above for clarity, however, I tend not to do this normally.)

Or, if you want to use "variable parsing" then...

$variables['myvariable2'] = "My settings are: '$variables[myvariable1]'";


(Note I have removed the single quotes around the array index in the above example.)

Or, preferably, use "complex" curly brace syntax (also "variable parsing"):
$variables['myvariable2'] = "My settings are: '{$variables['myvariable1']}'";


(Note that variable parsing only works in double quoted strings, and HEREDOC/NOWDOC syntax.)