Variable variables

When working in PHP and trying to reference an array with a variable variable such that…

[php]$mailfax = array(“mailing address”, “fax number”,“payment”);
$lTempString = “mailfax”;
echo ${$lTempString}[0];[/php]

When I run it i get ‘Array[0]’ - but expecting “mailing address” - how to reference the array content using a variable variable?

It works for me, hmm ?? [php]echo ${$lTempString}[0];[/php]

Do a
[php]var_dump($$lTempString);[/php]

You might find a clue in why it don’t work for you? I’m stumped.

Thanks… your reply suggested I’m close what I wanted to do was possible… and I was able to figure out the answer… apparently with this build of PHP i have to use the following format…

echo {${$lTempString}[0]};

it works fine with the curly brackets around the whole thing… not sure why… but when all else failed i used random trial and error of formatting and found this result…

So … when i did the code in “EasyPHP” webserver, it required this formatting… when I switched over to “Abyss PHP” webserver it allowed the curly brackets to be removed… who would have guessed that?!? ok… moving on…

Why are you using Variable variables? I have never needed it and dont know of any good reason to use it. It just makes the code harder to follow.

Anyone have a good reason it should ever be used where there is not a better php alternative?

I kind of disagree :

[php]<?php
class Seasons {
/* 2014 to 2020 for more go to http://www.neoprogrammics.com/sun/Solstices_and_Equinoxes_1600_to_2400.php */
protected static $bookingDayFormat;
protected $season = [‘Spring Starts’ => [‘2014-03-20’, ‘2015-03-20’, ‘2016-03-20’, ‘2017-03-20’, ‘2018-03-20’, ‘2019-03-20’, ‘2020-03-20’],
‘Summer Starts’ => [‘2014-06-20’, ‘2015-06-21’, ‘2016-06-20’, ‘2017-06-21’, ‘2018-06-21’, ‘2019-06-21’, ‘2020-06-20’],
‘Fall Starts’ => [‘2014-09-23’, ‘2015-09-23’, ‘2016-09-22’, ‘2017-09-22’, ‘2018-09-23’, ‘2019-09-23’, ‘2020-09-22’],
‘Winter Starts’ => [‘2014-12-21’, ‘2015-12-22’, ‘2016-12-21’, ‘2017-12-21’, ‘2018-12-21’, ‘2019-12-22’, ‘2020-12-21’]];
protected $date;
public $retrieveSeason;

public function __construct($checkSeason, $date = NULL) {
$this->retrieveSeason = $this->getSeason($checkSeason, $date);
}

public static function bookingDate($date) {
return self::$bookingDayFormat = date(“Y-m-d”, strtotime($date));
}

protected function getSeason($checkSeason, $date = NULL) {
$this->date = self::bookingDate($date);
$this->date = is_null($this->date) ? date(‘Y-m-d’) : $this->date;
foreach ($this->$checkSeason as $key => $value) {
if (in_array($this->date, $value)) {
return $key;
}
}
return false;
}

}

$season = new Seasons(‘season’, “2014-12-21”);
echo $season->retrieveSeason;
[/php]

Now I modified this script, but if you needed both hemispheres it would be more practical to use variable variables. :wink:

Not sure what your saying. I dont see it being used in the code you posted.

[php] foreach ($this->$checkSeason as $key => $value) {
if (in_array($this->date, $value)) {
return $key;
}[/php]

You normally only have $this->checkSeason with a regular variable.

The script before I modified (from a weather script I found on the net to fit my needs) was protected $northern_hemisphere = array(//data) and there was a second array called protected $southern_hemisphere = array(//data);

Then to call one of them, all you would had to do is:
[php] $season = new Seasons(‘northern_hemisphere’, “2014-12-21”);
echo $season->retrieveSeason;[/php]

This obviously is the Object-Oriented way of doing variable variables.

There are many ways to skin a cat… my example was very simplistic as I was aware my issue was one of formatting and I wanted to make the code very simple as not to get hung up on other details. Variable variables are for the most part an older programming technique and frequently there are security implications to its use, it should therefore be used sparing and carefully. Most of my co-workers did not even know of its existence… thus why i reached out to this community… thanks for your help BTW.

Sponsor our Newsletter | Privacy Policy | Terms of Service