I'm working on a personal project, I do it as a hobby, I like the world of programming but I'm not very good. So I would like to understand more about the issue I am in.
I was wondering if it was possible to put one variable inside another. Let me explain better with some examples.
I know I can do this:
$var1 = $item->get_something();
$var2 = $var1->get_anything();
echo '<div > '. $var2 .' </div>
But I can't do something like that:
$var2 = ( $var1 = $item->get_something() ?? $var1->get_anything() );
echo '<div > '. $var2 .' </div>
Is it possible to define $ var1 inside $ var2? So that $ var2 contains both $ var1 something and $ var1 anything. I understand this is a horror, pardon my ignorance. But is something like this possible ?
CodePudding user response:
Is it possible to define $ var1 inside $ var2? So that $ var2 contains both $ var1 something and $ var1 anything.
Basically you are trying to keep the values returned from both of the functions in one variable using php, you can achieve this by using php dictionaries, an example is following.
$temp = $item->get_something();
//store the values of both something and anything in a PHP associative array
$var= [
'something' => $temp,
'anything' => $temp.get_anything()
];
echo $var['something']; //prints the value of something
echo $var['anything']; //prints the value of anything
CodePudding user response:
Thanks to Andrea Olivato.
Not 100% sure I understand what you mean but it seems you're looking to do everything in one instruction and you could do $item->get_something()->get_anything() – Andrea Olivato
After several tests I managed to get a result. I understood that I can also do as written below.
$var2 = $var1 = $item->get_something()->get_anything();
echo '<div > '. $var2 .' </div>