Variables Variables

22. 08. 2019

> **Warning:** This article was written many years ago and some information may be outdated or incorrect. Please bear this in mind when reading.

Variables are not intended for common deployment (they solve problems that can be solved in other ways), they are mainly used to make writes more concise and memory accesses more complex.

Consider the following example:

$x = 25; // contains 25
$nacitana_promenna = 'x'; // contains "x"
$y = $$nacitana_promenna; // contains 25
echo $y; // prints 25

Note the two dollars following each other. In this case, the value of the variable $y will be loaded into the variable that has the name contained in the $nacitana_variable.

A little confusing, huh? That's why you'd better not use variables.

Note: Variable variables are a specialty of PHP because of the dollar sign. In other languages, the beginning of a variable name is not marked with any character, so you cannot use variable variables because it would be ambiguous when it is a classical variable and when it is not.

Jan Barášek   Více o autorovi

Autor článku pracuje jako seniorní vývojář a software architekt v Praze. Navrhuje a spravuje velké webové aplikace, které znáte a používáte. Od roku 2009 nabral bohaté zkušenosti, které tímto webem předává dál.

Rád vám pomůžu:

Související články