1

The first time I paste this little test for loop in PHP interactive shell mode (php -a from the command line), it runs perfectly.

for($weekday == 0;  $weekday<7; $weekday++) { echo $weekday; }

Then I try pasting it and running it again a few times, it never returns any output. Why does it work the first time and do nothing after that?

Output:

enter image description here

And php -v returns PHP 7.1.1 (cli) (built: Jan 23 2017 15:09:57) ( NTS )

UPDATE: yes, I am stupid and used the comparison operator instead of the assignment operator. Dur. :-) But still, my original question is valid: WITH the comparison operator (as seen above), even though it is not exactly doing what I want it to do, why does it only execute once?

1 Answer 1

3

Change the == to = to reset $weekday back to 0 each time.

for($weekday = 0;  $weekday<7; $weekday++) { echo $weekday; }

Otherwise, the second time it runs $weekday is already 7 and the loop condition fails immediately.

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

Comments

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.