Fueling Your Coding Mojo

Buckle up, fellow PHP enthusiast! We're loading up the rocket fuel for your coding adventures...

Popular Searches:
69
Q:

migration - Variable variables handling order: changes in PHP 7

Hi everyone,

I have recently started working with PHP 7 and I am facing some difficulties with variable variables handling order. I would appreciate it if someone could shed some light on this topic for me.

To provide some context, I am currently working on a migration project where I am upgrading an existing PHP application from an older version to PHP 7. In the previous version, I had some code that utilized variable variables, but it seems that the handling order may have changed in PHP 7.

From what I understand, variable variables allow me to use the value of a variable as the name of another variable. For example:

```php
$foo = 'bar';
$$foo = 'baz';

echo $bar; // Output: baz
```

However, in PHP 7, I have noticed that the order of operations may have changed when it comes to variable variables. This has caused some unexpected behavior in my code. I would like to better understand this change and how it affects the handling of variable variables.

Can someone please explain to me the changes in variable variables handling order in PHP 7? What is the recommended approach to ensure compatibility with PHP 7 while using variable variables?

Any help or insights on this matter would be greatly appreciated.

Thank you in advance!

All Replies

mzulauf

Hey everyone,

I also faced a similar challenge when transitioning to PHP 7 and dealing with variable variables. The changes in handling order did cause some unexpected behavior in my code, and it took me a while to figure out what was going on.

In PHP 7, the order of variable variables handling has indeed changed. Previously, the evaluation was performed from left to right, but in PHP 7, it is done from right to left. This means that the rightmost variable is resolved first and then used to determine the leftmost variable.

For instance, consider the following code snippet:

php
$foo = 'bar';
$bar = 'baz';

echo $$foo; // Output: baz


In PHP 7, the order of evaluation has shifted. The rightmost variable `$foo` is resolved first, yielding `'bar'`. Then, the resolved variable `$bar` is used to fetch the value, resulting in the output of `baz`.

To ensure compatibility with PHP 7 and make your code work as expected, you'll need to make some adjustments. One way to handle this is by using the `variable_variable` function within backticks (`). For instance:

php
$foo = 'bar';
$bar = 'baz';

echo `$$foo`; // Output: baz


By enclosing the variable variable `$$foo` within backticks, you retain the desired order of evaluation in PHP 7.

I hope this provides some clarity and helps you address the changes in variable variables handling in PHP 7. If you have any further questions, feel free to ask!

Best regards!

moen.tia

Hey there,

I encountered a similar situation when I migrated my PHP application to PHP 7 and had to deal with the changes in variable variables handling order. It was a bit perplexing at first, but I managed to adapt my code to accommodate the modifications in PHP 7.

In PHP 7, the handling order of variable variables has indeed been altered. Previously, PHP evaluated them from left to right, but now the evaluation occurs from right to left. This means that the rightmost variable is resolved first, and its value is then used to determine the leftmost variable.

Let's look at an example to understand this better:

php
$foo = 'bar';
$bar = 'baz';

echo $$foo; // Output: baz


With PHP 7, the evaluation order has transformed. The rightmost variable `$foo` is resolved to `'bar'` first, which is then used to access the variable `$bar`, resulting in the output of `baz`.

To ensure your code works seamlessly with PHP 7, it's crucial to review and make appropriate changes. One approach is to use the `${$foo}` syntax, which clearly specifies the variable variables. Here's an example:

php
$foo = 'bar';
${$foo} = 'baz';

echo $bar; // Output: baz


By enclosing the variable variable `${$foo}` in curly braces, you ensure the correct evaluation order is maintained in PHP 7.

I hope this explanation sheds some light on the changes in variable variables handling order in PHP 7. If you have any further queries, feel free to ask!

Best regards.

lina10

Hey there,

I recently went through a similar experience while migrating my PHP application to PHP 7. The changes in variable variables handling were indeed a bit tricky to handle, but I managed to figure it out through some testing and research.

In PHP 7, the handling order of variable variables has indeed changed. Previously, PHP would evaluate variable variables from left to right, but in PHP 7, they are evaluated from right to left. This means that the rightmost variable is resolved first, and then the resolved variable is used to determine the leftmost variable.

For example, consider the following code:

php
$foo = 'bar';
$bar = 'baz';

echo $$foo; // Output: baz


In PHP 7, the order of evaluation has changed. The rightmost variable `foo` is resolved first, which becomes `'bar'`. Then, the resolved variable `$bar` is used, resulting in the output `baz`.

To ensure compatibility with PHP 7, it's important to review your code and modify it accordingly. One approach is to use curly braces to explicitly indicate the variable variables. For example:

php
$foo = 'bar';
${$foo} = 'baz';

echo $bar; // Output: baz


By enclosing the variable variable `${$foo}` in curly braces, you ensure that the correct order of evaluation is maintained in PHP 7.

I hope this clears up any confusion and helps you navigate through the changes in variable variables handling order in PHP 7. Let me know if you have any further questions!

Cheers!

New to LearnPHP.org Community?

Join the community