1

i'm just confused about assigning variable values on php to another one , how does it work?! for Example:

 <?PHP
    $var1=mysqli_fetch_array($query);
    while($var2=$var1)
     {
       echo $var2[$key];   /** this wont't work Correctly however $var1 it's 
       value = to mysqli_fetch_array**/
     }

    while($var1=mysqli_fetch_array($query))
     {
      echo $var1[$key];   /** this will work , why ! **/
     }

 ?>
8
  • you have a parse error on line 2. Commented Dec 13, 2019 at 17:15
  • @FunkFortyNiner please could u point to it ? Commented Dec 13, 2019 at 17:21
  • The semi-colon is missing for $var1=mysqli_fetch_array($query). That should read as $var1=mysqli_fetch_array($query); Commented Dec 13, 2019 at 17:24
  • @FunkFortyNiner ok fixed but if u know what i mean is to know how assigning in php works? Commented Dec 13, 2019 at 17:27
  • 1
    Because the second example calls the function for each iteration and thereby exhausts the result set. The first just keep reassigning the first row over and over again. Commented Dec 13, 2019 at 17:36

1 Answer 1

2

A peculiarity in PHP's assignment behaviour, is that it also returns the result of the assigned value. This allows for statements such as:

$a = $b = $c = 3; // All of a, b and c will equal 3

And:

while ($variable = call_a_function()) {
  do_something_with($variable);
}

In the latter example, variable gets assigned to the output of call_a_function() at the beginning of the loop iteration; as soon as call_a_function() returns a value that evaluates to false, the loop ends. If the returned value does not evaluate to false, variable will contain whatever value was returned, until it gets overwritten again.

Your examples use a similar behaviour. The crucial difference between

$var1=mysqli_fetch_array($query);
while($var2=$var1)
{
  echo $var2[$key];
}

And:

while($var1=mysqli_fetch_array($query))
{
  echo $var1[$key];
}

...is that in the first example, $var1 is only assigned to the return value of mysqli_fetch_array($query) before the loop starts, while in the second example, $var1 is assigned to the return value of mysqli_fetch_array($query) in every iteration of the loop.

What makes the two pieces of code crucially different, in the end, is the fact that mysqli_fetch_array($query) returns different results, depending on circumstances.

Combining the code snippets into an example that works as intended, yet uses $var2, yields:

while($var2=$var1=mysqli_fetch_array($query))
{
  echo $var2[$key];
}

or

$var1=mysqli_fetch_array($query); // ask first time
while($var2=$var1)
{
  echo $var2[$key];
  $var1=mysqli_fetch_array($query); // ask again, because the answer changed
}

TL;DR: The first example asks a question once, the second asks a question many times. In this case, the intended behaviour of your code requires the question to be asked multiple times, because the answer changes over time.

Sign up to request clarification or add additional context in comments.

1 Comment

Notice that in the end, this was not related to assigning variables per se, but about mysqli_fetch_array not being idempotent. en.wikipedia.org/wiki/Idempotence

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.