scriptygoddess RSS Feed
 
 
 
 

PHP: simple logic statement

I'm tired of searching for the correct syntax everytime I wan to use this line… if you're doing a simple one line "if" statement, you can use a "ternary conditional operator" - for example:

echo $variable == 'value' ? 'print if true' : 'print if false';

3 Responses to “PHP: simple logic statement”

  1. 1
    Jonathan Johnson:

    Ah, the ternary operator. Just remember this. The question mark comes after the item in question — Is __ true ? Yes : No;

    It's the only way I can remember it ;)

  2. 2
    Michael Moncur:

    Don't you need parentheses around the condition? Or they optional in PHP?

  3. 3
    Mischievous Ramblings » PHP: Ternary Operator:
    [...] 2004.11.19 PHP: Ternary Operator 9:54 pm in Programming  | scriptygoddess provides a quick note reminding us how the ternary o [...]

Bookmarks

WordPress Resources

Meta

Random Stuff