What utility has this way of accessing variable values

Asked

Viewed 161 times

3

<?php      
      $Bar = "a";
      $Foo = "Bar";
      $World = "Foo";
      $Hello = "World";
      $a = "Hello";

      $a; 
      $$a; 
      $$$a; 
      $$$$a; 
      $$$$$a; 

?>

In what sense can I take advantage of this way of accessing php variables value in this way, in practice where it would be useful?

  • 1

    It reminds me a lot of the pointers of the C language, when you want to access the address of the address of a variable ***v;

  • Related: https://answall.com/questions/171153/variav%C3%A9l-Vari%C3%A1vel-em-php

  • 3

    And as Diego said downstairs: In practice, don’t use this.

  • 1

    @bfavaretto I took the liberty of adding your link in the reply, I found it interesting ok?

  • Okay @Diego, no problem.

2 answers

6


Variables, dynamic variables or variables created during PHP execution.

It is a feature that allows us to create a variable through the content of another variable.

To create a variable you use one variable to act as an identifier for another that is created. The symbol of $, that is, we should use $$. See in the following example.

<?php

// Declarando o valor da variável $a

$a = 'nome';

/**

 * Criamos $$a, que como possui dois $

 * também pode ser chamada pelo valor da

 * variável $a ou seja "nome"

 */

$$a = 'Mauro';

// Exibo $a e $nome que foi criada dinâmicamente

echo $a . ' : ' . $nome;

?>

As you can see in the example we can create the variable from the value of another variable. The opposite is also valid instead of creating the variable, in our case, $nome dynamically set its value normally however when we display its result on screen we can access its value through $$a, reminding that for this to work $a must have the same value as the variable that will be created in our "name" case. In this other example, using your question, see how the output would look:

  $Bar = "a";
  $Foo = "Bar";
  $World = "Foo";
  $Hello = "World";
  $a = "Hello";

  $a; //Retorna Hello
  $$a; //Retorna World
  $$$a; //Retorna Foo
  $$$$a; //Retorna Bar
  $$$$$a; //Retorna a

  $$$$$$a; //Retorna Hello
  $$$$$$$a; //Retorna World

In my opinion, in everyday life this is of no commercial use, but rather for studies, to learn how variables created in execution work.

Source 1 - Source 2 and comments

PS: See @bfavaretto’s answer to that question here, will have give an idea of real use.

0

Another opinion

Dynamic variables have a use yes as can be seen in the following example.

In sending a extensive form instead of typing hundreds times in the code, for example, $nome = $_POT['nome']

making use of dynamic variables the thing becomes very simplified, see how

foreach ( $_POST as $chave => $valor ) {
  // $$chave cria as variáveis com os names dos elementos do formulário
  $$chave = trim( strip_tags( $valor ) );
 }

I will quote another example of the use of these Variáveis variáveis, variáveis dinâmicas ou ainda variáveis criadas durante a execução no PHP

I have a hundred options as exemplified below:

echo ("<option value=\"youtube.php?n=s5&-----aaED7ZVkg0\"".$s5.">15/07/2016</option>\n");
echo ("<option value=\"youtube.php?n=s4&-----K_nny4LPEw\"".$s4.">15/07/2016</option>\n");
echo ("<option value=\"youtube.php?n=s3&-----u92sZyvhGw\"".$s3.">08/07/2016</option>\n");
echo ("<option value=\"youtube.php?n=s2&-----4BmVYRHypo\"".$s2.">08/07/2016</option>\n");
echo ("<option value=\"youtube.php?n=s1&-----_pM6kaYjc0\"".$s1.">08/07/2016</option>\n");

In this case I would like the user to click on an option and when the video is displayed, the option selected would be SELECTED

Note that I am passing a parameter in the URL n=s1 n=s2 etc.....

How could one leave the option selected SELECTED when directed to the video page?

An exhaustive and immense way would be through:

if ($_GET["n"]=="s1"){ $s1="" selected""; }elseif($_GET["n"]=="s2"){ $s2="" selected""; } ................ ..................

elseifa hundred times

Here comes the variable usage

$n = $_GET["n"];
$$n=" selected";

To $n=s1; we shall have $s1=" selected";

Did I help you? Hover your mouse in the yellowish area below to find out

Very useful, saved hundreds of ifs

Variables, dynamic variables or variables created during PHP execution Credits

Regardless of the name you find around in books or even on the Internet, it is a resource that allows us to create a variable through the content of another variable.

To create a variable you use one variable to act as an identifier for another that is created. For this we use twice the $symbol, that is, we must use $$.

  • Had never thought about it xD, would use an array

  • @Amadeuantunes the tb array would be huge no? I needed only two lines

  • truth, now got me thinking how I would solve this problem otherwise

  • 2

    I do not disagree that there are solutions that use variable variables and can be more elegant. What catches me is that the code becomes less readable and potentially confusing. So I say around to avoid as a rule. But every rule has exceptions :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.