John Davidson

php - Please Explain $$v++ Why its returning 1 or true

0 comments
Message:


<?php

function firstDuplicate($a) {
foreach ($a as $v)
echo $$v++;
return -1;
}
firstDuplicate([2, 1, 3, 5, 3, 2]);

I'm unable to understand why echo $$v++ is returning 1. Please help.



My Answer:
In PHP, `$$v` is a variable variable. It means that the value of the variable `$v` is used as the name of another variable whose value is then returned.

For example, if `$v` is equal to "x" and `$x` is equal to 1, then `$$v` will return the value of `$x`, which is 1.

If `$v` is equal to "y" and `$y` is equal to true, then `$$v` will return the value of `$y`, which is true.

So, `$$v` will return the value of the variable whose name is stored in the variable `$v`.

Rate this post

4 of 5 based on 7105 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us