Argv command line help

i got this pice of code out of a script and i’m just wondering if someone can tell me what this line of code does. i think i kinda got an idea…

(isset($argv[1]) - checks if argv[1] is set

the ? i’m not sure except maybe it’s checking if argv[1] is = to safe

the rest of the line im not sure what it’s setting ? true : false) : false);

here is the full line of code…

$safeMode = (isset($argv[1]) ? ($argv[1] == "safe" ? true : false) : false);

can someone explain plz what it does

Thanks Snoop

i did some searching the ? is the conditional operator

$x ? $y : $z

would means "if $x is true, then use $x; otherwise use $z".

so now i understand it a little bit more

$safeMode = (isset($argv[1]) ? ($argv[1] == "safe" ? true : false) : false);

so if argv[1] is set its true so use safe - now i don’t quite understand the ending ? true : false) : false); i’m still looking lol…

thanks Snoop

This is the Ternary Operator. Can be found on the following page the documentation - https://www.php.net/manual/en/language.operators.comparison.php

There are two different Ternary expressions in the code. The main one is -

$safeMode = isset($argv[1]) ? $y : false; // note: the initial and final () around this are unnecessary

Where $y is the second Ternary expression, which is testing if $argv[1] == “safe”, use true, else use false.

So, overall, if $argv[1] isset and it is equal to ‘safe’ assign a true to $safeMode. If it is not set or it is set but it is not equal to ‘safe’ assign a false to $safeMode.

Thanks phdr been looking around found acouple sites but gonna check the one you said… the end of it i think was confusing me lol…

? true : false) : false);

thanks so much that helped me more then you know lol

still gotta alot to lean too : )

Thanks Snoop

Sponsor our Newsletter | Privacy Policy | Terms of Service