Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

$name=null; if($name) { code } else { code }

what is meaning of if($name) represents here?

Hey Vinay , You are using if condition which execute when the condition is true or false. As in your code $name is set to null so $name will treated as false value and else code block will execute. if($name) will check whether the $name has value or not.If it has value condition is true otherwise false.

1 Answer

Codin - Codesmite
Codin - Codesmite
8,600 Points

It is shorthand for is true.

By setting $name to null it will return false.

NULL, 0 and FALSE all return as false and not true.

So:

<?php
    $name = null; 
    if($name) { 
        // code 
    }
?>

Is basicaly just a short way of declaring

<?php
    $name = null; 
    if($name == TRUE) { 
        // code 
    }
?>

You can also add ! to check for if false.

<?php
    $name = null; 
    if(!$name) { 
        // code 
    }
?>