1

In this [I-believe] famous loop condition ($row = $result->fetch()) (in which $result is a PDOStatement object) what is the type of the $row? Is it an "array" or an "array container"? Is there a difference between an array and a variable that holds an array?!

I ask this specially because if there wasn't any difference, then I should have been able to have my rows the whole in the $row variable after the loop is done, which is not the case, and I need another array to hold each row for me(the syntax is also different and I need empty brackets in front of the name of that array to add elements)

(According to my tutorial, fetch() is a method of the PDOStatement object that returns the next row of my table as an array).

5
  • 1
    $row is a run-time array variable which is created each time when loop runs so previous values will not be available. that's why you want to save it's data to another array variable Commented Feb 6, 2018 at 6:34
  • @AlivetoDie Ah, thank you. But isn't it possible to have a non-run-time array variable in the loop condition? like ($row[] = $result->fetch()) Commented Feb 6, 2018 at 6:40
  • A better alternative is use fetchAll() Commented Feb 6, 2018 at 6:42
  • What do you mean by "array container"? Commented Feb 6, 2018 at 6:59
  • On error, PDOStatement::fetch() returns FALSE. On success it returns TRUE, an array or an object, depending on the value of its first argument ($fetch_style). If your tutorial doesn't say this then you have to find a better one. And, no matter what tutorial do you use, always search for the answers in the documentation first. Commented Feb 6, 2018 at 7:02

1 Answer 1

2

Actually

$row is a run-time array variable which is re-created each time when loop (while loop) runs.So previous values will not be available. That's why you have to save it's data to another array variable which is created statically.

Is this recreation an intrinsic quality of the while loop?

Simon.B no it's not. You can try

$row = []; 
while($row[] = $result->fetch()){};
print_r($row); 

and check.

Better alternative is to use fetchAll();

But why this above apporach is not used often:-

Because above approach will load the whole array again and again into Memory when loop runs and then assign array to that.

While when you are using run-time variable and doing assignment then the whole array is not loaded each time, only assignment will done.

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

6 Comments

Is this recreation an intrinsic quality of the while loop? (I ask this because in the for loop for example, we can have a counter variable and it's NOT recreated).
@Simon.B no it's not. You can try $row = []; while($row[] = $result->fetch()){};print_r($row); and check.Better alternative is to use fetchAll();
Wow this comment solved my problem! We can't create a variable inside a while loop condition, and that's why we can't just use $row[] before it's been created! Thanks a ton "Alive to Help" :)
@Simon.B the approach what i gave is little bit heaviour in the sense that it will load the whole array again and again when loop runs and then assign array to that.While when you are using run-time variable and doing assignment then the array is not loaded each time.
Your answer is thorough and complete, but I have a final question too. Is a "run-time array" simply a variable that holds a single array "temporarily"?
|

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.