All on your own


#1

What's wrong with my code?
It says"Parse error: syntax error, unexpected T_STRING on line 12"

<?php
    switch(1):
    case 1:
        echo "1";
        break;
    case 2:  //This is line 12.
        echo "2";
        break;
    case 3:
        echo "3";
        break;
    default:
        echo "don't know";
        break;
    endswitch;
    ?>

#2

Please, which one is line 12, and could you post a link to this exercise? Thank you.


#3

A switch is an identity function, in a sense. How can we match a primitive 1 to a primitive 2. It's not logical and there is a wire crossed here that throws a Parse error.

What I mean by identity function is this:

y = x

Programmatically,

f(x) = x

or

function identity(x){
    return x;
}
y = identity(42);
// 42

So that covers the act of making two objects identical. Now let's compare them and see the other side of identity.

a == b

is an equality, if a and b are the same in most respects. Eg. 1 == "1" or 1 == 1.0.

a === b

is an identity if a and b are the same, exactly. Eg. 1 === 1. Further reading and testing needed on 1 === 1.0 in PHP. Languages differ on this.

Now consider that primitve values are literal and as we've seen cannot be directly related, only indirectly. We don't sort by comparing letters, we do it by comparing ordinals. In a switch it is operands in an equality (I think it is safe to say, but may be an identity, will need to test)...

    switch (a):
    case 1: break;
    case 2: break;
    case 3: break;
    default: // no break
    endswitch;

This switch is looking at one or the other of,

a === 1 || a == 1

We need to test this further which one, exactly.

What happens when we reverse operands, which is perfectly legitimate in an identity function?

x = f(x)

In other words,

if,
y === x
then
x === y

Let's consider a Boolean expression:

    switch (true){
    case a % 3 === 0: break;
    case a % 5 === 0: break;
    default: echo a;
    }

This will only allow numbers that are not divisible by 3 or 5 to display.