Unexpected output


#1

When I run the following code, the output is nothing like I expect. My expectation was that when case 1: executes that $roll = 1, when case 2: executes, $roll = 2, etc. I assumed that to true because the break executes when $roll = 0 This doesn't appear to be true though. Further, when $rollCount is printed on the last output line, it is not the same as the number of output cases, for instance, I'll have 5 case outputs to a value of 3 in $rollCount, another time I'll have 10 case outputs to the same value of 3 in $rollCount.

So what is going on here to explain the observed results?

<?php
	$roll = true; 
	
    $rollCount = 0; 
    do {
        $roll = rand (0, 6); 
        $rollCount++;
        switch ($roll) {
            case 0:
                break;
            case 1:
            	echo "a $roll \n";
            case 2: 
            	echo "b $roll \n";
            case 3:
            	echo "c $roll \n";
            case 4: 
            	echo "d $roll \n";
            case 5: 
            	echo "e $roll \n";
            case 6: 
            	echo "f $roll \n";
        }
        }while($roll);
        $verb = "were";
        $last = "rolls"; 
        if ($rollCount == 1) {
            $verb = "was";
            $last = "roll"; 
        }
        print "<p> There {$verb} {$rollCount} {$last}! $roll</p>";
        $roll = false;
        ?>

For convenience, I'm running the code at http://sandbox.onlinephpfunctions.com/


#2

Recall that in PHP we are generating HTML.

echo "<p>a $roll</p>";

That will give you your linebreak since P is a block level element. As you've already found, the double quotes allow string interpolation of embedded variables.

If no line spacing is allowed in the response (P has margins top and bottom of 1em) then add a <br> to your string.

echo "a $roll<br>";

#3

I placed $roll in the last output just to confirm that the break was executing when I expected it too. I guess what I don't understand is what causes $roll = x not to correspond to the case of the same number, except in case 0 where the break executes. Additionally, I don't understand why the number of case echo outputs does not correspond to $rollCount.


#4

For starters, you need a break on every case.

Aside

I don't like spilling out all the errors in one post, and usually pick a scenario that lets the learner spot the others. Sorry for being vague. See what that fix does, and we can review further if the problem still persists.


#5

I appreciate that. I'm so far from the woods at this point, it's hard to make out the trees. :slight_smile:
Funny: I'm looking at the php manual for the third or fourth time and see the break; in each case just now! Now the case output is just offset by one from the output $rollCount which can be attributed to case 0 being a break.

I did figure that there was something missing (this is a modified code snippet from another's post), I just couldn't figure out what. I still wonder why I got the output I did - how does php iterate through the incorrect
code? My brain sure is liking this!


#6

The thing that I am having trouble wrapping my head around is the switch. Why, if there are no function calls, have a switch at all?

<?php 
    $rollCount = 0;
    $rolls = array('A', 'B', 'C', 'D', 'E', 'F');
    do {
        $roll = rand (0, 5); 
        $rollCount++;
        echo "$rollCount: $rolls[$roll]\n";
    } while ($roll);
    
    $verb = "were";
    $last = "rolls"; 
    if ($rollCount == 1) {
        $verb = "was";
        $last = "roll"; 
    }
    print "There {$verb} {$rollCount} {$last}!";
 ?>

1: F
2: E
3: E
4: B
5: E
6: E
7: F
8: E
9: C
10: B
11: A
There were 11 rolls!

Edit

If we're at the point where improvement is the concern,

<?php 
    $rollCount = 0;
    $rolls = array('A', 'B', 'C', 'D', 'E', 'F');
    do {
        $roll = rand (0, 5); 
        $rollCount++;
        echo "$rollCount: $rolls[$roll]\n";
    } while ($roll);
    
    $verb = $rollCount > 1 ? "were" : "was";
    $last = $rollCount > 1 ? "rolls" : "roll"; 
    print "There $verb $rollCount $last!";
 ?>

Ternary expressions save a whoe lot of code when used to their advantage. It came to us here.

1: F
2: B
3: E
4: B
5: D
6: A
There were 6 rolls!

#7

Way cleaner this way. Now to grok the order of operations and flow!

From line 4, thus far, I see do x while z.

do pick a number between 0 and 5 and call it $roll
do add one to previously declared as "0" $rollCount
do output some stuff where $roll maps to a previously declared point in array $rolls

while a random number between 0 & 5 is valid, assign certain words based on context and output accordingly.

I'm understanding the elements, but what made it stop?


#8

Efficiency abounds! Good to see that there are iterations as such.


#9

while ($roll);

Once the concept of truthy is known, as well as falsy, of course, the world is our oyster and we may save code steps amundo.

0 is falsy. See the picture?


#10

So, while evaluates as true or false! 0 or null being false, anything else is true. Now I have to figure out the $verb and $last variable declarations of your last post...


#11

A ternary expression is like and if-else. In the above it is assigned to a variable, so called a ternary statement.

variable = state or conditional ? outcome if true : outcome if false

One or the other of the outcomes is assigned to variable.


#12

Cool! I was looking in the manual - I'll have to write a few before I really get it. I like efficiency, it improves readability too.


#13

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.