Share
A "variable variable" is a PHP variable whose name is contained in another variable. Sounds tricky, right?
 ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌

ALEX WEB DEVELOP
Alex

About PHP "Variable Variables"

variable variables

Hey,
I'm Alex, from Alex Web Develop. I hope you are having a fantastic week.

Let's talk about variable variables in PHP.

A variable variable is a PHP variable whose name is contained in another variable.
For example, let's first define a standard variable containing the string "city":

$var = "city";


Now, you want to define a new variable named $city.
But instead of defining it the normal way, you want to use the string inside $var, "city", as the name for the new variable.
To do that, you need to add two $ symbols before the variable that contains the name.
Like this:

$$var = "London";

/*
It's the same as:
$city = "London";
*/


In $$var, $var is replaced with its content (the string "city"), and the extra $ turns "city" into $city.
Therefore, writing $$var is the same as writing $city:

$var = "city";
$$var = "London";
echo $city; // Output: "London"



The next question is: how do you use variable variables in your apps?


Variable variables work much like associative arrays.

Variable variables let you make a variable's name parametric and use it dynamically.
For example:

$title = 'my book title';
$author = 'myself';
$pages = 200;

$var = $_GET['var'];

if (isset($$var)) {
  echo $$var;
}


The above code echoes the variable defined in the "var" request element, if set.
For example, if you call the script with the "var=title" request string, it will echo: "my book title".

However, you can see how this solution is poorly readable and, in some cases, it can also have security issues.

In my opinion, it is better to use associative arrays instead:

$vars = [
  'title' => 'my book title',
  'a
uthor' => 'myself',
  '
pages' => 200
];


$var = $_GET['var'];

if (isset($vars[$var])) {
  echo $vars[$var];
}


What do you think?
Here are some links to read more about variable variables:

the cool bit

THE COOL BIT


You can actually add as many $-levels as you want.
Each $ makes PHP use the following variable as the name for the current one.

This is better shown with an example (try it yourself):

$var = "how";
$how = "are";
$are = "you";
$you = "today";
$today = "?";

echo $var . " "; // Output: "how ";
echo $$var . " "; // Output: "are ";
echo $$$var . " "; // Output: "you ";
echo $$$$var; // Output: "today";
echo $$$$$var; // Output: "?";

tip of the week

TIP OF THE WEEK


As of PHP 8.1.0, you can mark class properties as readonly.
You can initialize a readonly property only once (ex. from the class' constructor). After that, it behaves like a constant and you can't change it anymore.
This only works for typed properties.

For example:

class Car {
  public readonly string $model;
  
  public function __construct(string $model) {
    $this->model = $model;
  }
}

$car = new Car("Civic");
echo $car->model; // "Civic";

// Error!
$car->model = "new model";

the challenge

WEEKLY CHALLENGE


Here's a PHP code challenge for you.
You take a flight from France (or any other place you like) to Peru (or any other place you like).
The flight takes 10 hours, 15 minutes and 7 seconds.

Write a PHP function that takes in input the local departure datetime and prints the local (at the arrival's place) landing datetime.

function landingDatetime(string $departureDatetime) {
  $flightHours = 10;
  $flightMinutes = 15;
  $flightSeconds = 7;
  //...
}


Let me know if you can solve it or if you need a hint :-)

next steps

NEXT STEPS


Want to take your PHP skills to the next level?
Take a look at my professional PHP courses:


If you have any questions just reply to this email. I'm happy to help.


Before you go:
  • If you enjoyed this email, click here to share it with your friends.
  • Add my address (alex@alexwebdevelop.com) to your Contacts to make sure my next emails will reach you.
  • Send me a reply and let me know what you think. I read all the emails.

All the best,
Alex



Thanks to Freepik for the images used in this email.



You are receiving this newsletter because you subscribed to Alex Web Develop.

If you unsubscribe, you will not get any more emails from me.

Alessandro Castellano, P.IVA (VAT ID): 07012140484, via Luigi Morandi 32, 50141 Firenze FI, Italy

Email Marketing by ActiveCampaign