Name a variable, the value of another variable

-1

I would like to know how to create a variable whose value is $ test2

$prueba = "Hola";
$prueba2 = &$prueba;
$$prueba2; // Debería valer $Hola pero me da un error.

Thank you.

    
asked by Antonio 26.10.2018 в 18:41
source

5 answers

3

You want to assign a variable $Hola from a string Hola that is in a variable. For that you can do:

$prueba  = 'Hola';
$$prueba = 'Funcioné';

var_dump($Hola);

And that prints "I worked".

What you are doing now

$prueba = "Hola";
$prueba2 = &$prueba;
$$prueba2;

It breaks down into:

// declaro la variable $prueba con valor 'Hola'
$prueba = "Hola";

// declaro la variable $prueba2 como una referencia a $prueba
$prueba2 = &$prueba;

// Escribo $$prueba2 que se interpreta como $Hola
$$prueba2;

And that below throws an error:

 E_NOTICE  Undefined variable: Hola

Bonus Track

Now, in case anyone is curious as to what effect the use of & has in declaring $prueba2 defining it as a reference implies that:

$prueba  = 'Hola';
$prueba2 = &$prueba;
$prueba  = 'Chao';
var_dump($prueba2);

Print 'Chao'. By redefining $prueba by reference, $prueba2 is redefined. If you do not pass it as a reference:

$prueba  = 'Hola';
$prueba2 = $prueba;
$prueba  = 'Chao';
var_dump($prueba2);

Print 'Hello', because $prueba2 was declared as the value of $prueba and if you redefine this one, the interpreter does not matter. $prueba2 has already been evaluated.

This behavior applies to scalars. However, objects (including instances of a class, which are objects) are always assigned by reference:

// esto crea un objeto en memoria, y $prueba es una referencia al objeto
$prueba = (object) ['saludo' => 'hola', 'despedida' => 'chao'];

// $prueba2 no es el valor del objeto, sino una referencia al objeto
$prueba2 = $prueba;

// se modifica el objeto referenciado
$prueba->despedida = 'adiós';

// todas las variables que lo referencian apuntan a su nuevo contenido
var_dump($prueba2);

Will print an object where despedida is adiós instead of chao .

Reassigning the value of $prueba would not touch the object, which is still in memory.

// esto crea un objeto en memoria, y $prueba es una referencia al objeto
$prueba = (object) ['saludo' => 'hola', 'despedida' => 'chao'];

// $prueba2 no es el valor del objeto, sino una referencia al objeto
$prueba2 = $prueba;

// se redeclara $prueba, la cual pierde la referencia al objeto
$prueba = 'cualquier cosa';

// $prueba2 sigue siendo una referencia al objeto en memoria
var_dump($prueba2);

Therefore $prueba2 is not worth cualquier cosa but returns the content of the referenced object.

    
answered by 26.10.2018 / 19:20
source
0

Your code should be like this:

<?php

$prueba = "hola";
$prueba2 = & $prueba;

echo $prueba2; //Muestra hola

?>

You can see it working here .

    
answered by 26.10.2018 в 19:50
0

What I think is happening is that you are trying to make a reference and for that you must assign the ampersand (&) sign to the beginning of the variable whose value is being assigned.

$nombre = 'Juan';          // Asigna el valor 'Juan' a $nombre
$saludo = &$nombre;        // Referenciar $nombre vía $saludo.
$saludo = "Hola $saludo";  // Modifica $nombre...
echo $saludo;
echo $nombre;  // $nombre también se modifica.

// imprime Hola Juan y Hola Juan

Another example that they also published is var_dump that by definition shows information about a variable such as:

$cadenadetexto = 'abc';
var_dump($cadenadetexto);
// que dará como resultado string(3) "abc"

Which I do not think you're looking for that.

Greetings

    
answered by 26.10.2018 в 19:47
-1

I have corrected I hope this helps you Eliminate the & of your code and leave it like this. The & is used more for concatenation of variables not to pass the value as you tried.

 <?php
 $prueba="Hola";
 $prueba2="prueba";
 echo $$prueba2;
 ?>
    
answered by 26.10.2018 в 18:57
-1

Double $ and & amp; are unnecessary in your case.

   <?php 
$variable = "hola"; 
$variable2 = $variable; 
echo $variable2; 
?>
    
answered by 26.10.2018 в 19:00