Author Topic: Different Characters  (Read 1654 times)

Occasionally, I'll be looking at code, and it will have characters in odd places, like so:

function toggleStuff()
{
    $Stuff = !$Stuff;
    echo("You have turned" @ ($Stuff ? "on" : "off") @ ".");
}

Which I'm assuming does the same thing as throwing a switch$ in there, but I also sometimes see the << and >> characters. What are they for, and are there any other things like this that I should know about?


    $Stuff = !$Stuff;
This will turn $Stuff to 1 if it were a string, blank, false, 0, or -1. It would turn $Stuff to 0 otherwise.

    echo("You have turned" @ ($Stuff ? "on" : "off") @ ".");
test ? true : false
If $Stuff were a string, blank, false, 0, or -1, it would show "off" in the echo. Otherwise "on".
Essentially the same as:
if(test){true}
else{false}, but also works in-line.
switch$
switch is an integer(float?) case handler, where switch$ is a string case handler. Not much different from if/else if/else if/else.
but I also sometimes see the << and >> characters.
Bit operators, along with | and &
x << y
if x were 0010 (2), and y were 2
the result would be 1000 (8)
>> does the opposite.
x | y
bitwise or operator, the result carries all true bits.
if x were 0011 (3) and y were 0110 (6)
the result would be 0111 (7)
x & y
bitwise and operator, any bits similar in x and y would remain in the result.
if x were 0011 (3) and y were 0110 (6)
the result would be 0010 (2)
x % y
division remainder.
example, if x were 10 and y were 3, the result would be 1. This is because if you subtract 3 from 10 till you cannot anymore, you would be left with 1.


Also for your reference, what you described in the OP is called a ternary operator
I only use it for very simple assignments such as what you posted, for anything even slightly more complex, I use if/else because I find it more readable