Grow your CSS skills. Land your dream job.

How can I write an if statement if one value is null?

  • # July 14, 2013 at 6:15 pm

    Code:

    if(isset($trakt->movie->title)) {
    $title = $trakt->movie->title;
    } elseif(isset($trakt->show->title)) {
    $title = $trakt->show->title;
    } else {
    $title = “None”;
    }

    How could I write it to something like this:

    If `$trakt->movie->title` or `$trakt->show->title` isset(), $title = $result?

    # July 14, 2013 at 6:57 pm

    I think I might need to use `empty()` instead of `isset()`

    # July 14, 2013 at 7:07 pm

    May I recommend the following resource:

    [http://www.php.net/manual/en/types.comparisons.php](http://www.php.net/manual/en/types.comparisons.php “http://www.php.net/manual/en/types.comparisons.php”)

    # July 14, 2013 at 7:07 pm

    You can use `$title = $trakt->movie->title ?: $trakt->show->title ?: “None”; `, not sure if that answers your question.

    # July 14, 2013 at 7:18 pm

    Thanks for the resource, @BenWalker

    @CrocoDillon Any documentation on `?:`. Can’t seem to find it.

    # July 14, 2013 at 7:19 pm

    It’s the ternary operator @chrisburton.

    http://davidwalsh.name/php-shorthand-if-else-ternary-operators

    Also @CrocoDillon, shouldn’t it be $title = $trakt->movie->title ? $trakt->movie->title : $trakt->show->title ? $trakt->show->title : "None"; Not sure if that’s right though. I never saw the operator with the ? and the : right next to eachother.

    # July 14, 2013 at 7:25 pm

    [PHP manual](http://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary)

    (condition) ? (expression if true) : (expression if false);

    Because you can omit a part of it you can create shortcuts to `if .. else` statements.

    EDIT: @srig99, same result :)

    > Since PHP 5.3, it is possible to leave out the middle part of the ternary operator. Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise.

    # July 14, 2013 at 7:32 pm

    Oh, ok then @CrocoDillon. Thanks for the tip!

    # July 14, 2013 at 7:33 pm

    @CrocoDillon

    I had also been unaware of that. Thanks!

    # July 14, 2013 at 7:38 pm

    Now I’m confused.

    I can just do this?:

    $title = $trakt->movie->title ? $trakt->show->title : “None”;

    # July 14, 2013 at 7:41 pm

    No. If `$trakt->movie->title` evaluates to true, `$title` will equal `$trakt->show->title`.

    The `?:` operator is simply a shorthand:

    ( expression one ) ?: ( expression two )

    Is the same as:

    ( expression one ) ? ( expression one ) : ( expression two )

    # July 14, 2013 at 7:47 pm

    Sorry for the double post, but to clarify:

    $title = $trakt->movie->title ?: $trakt->show->title ?: “None”;

    [The code CrocoDillon posted above] is equal to the following longhand:

    $title = ( $trakt->movie->title ) ? ( $trakt->movie->title ) : ( ( $trakt->show->title ) ? ( $trakt->show->title ) : ( “None” ) );

    # July 14, 2013 at 7:47 pm

    Ok, then it appears that’s not what I’m looking for.

    If `$trakt->movie->title` evaluates to a string and `$trakt->show->title` is empty, the `$title` should equal `$trakt->movie->title`, not `$trakt->show->title` and vice versa.

    ####Simplified:

    if $trakt->movie->title` returns a string and `$trakt->show->title` is empty

    $title = `$trakt->movie->title`

    if $trakt->movie->title` is empty but `$trakt->show->title` returns a string

    $title = `$trakt->show->title`

    # July 14, 2013 at 7:54 pm

    What if `$trakt->movie->title` evaluates to a string and `$trakt->show->title` is NOT empty? In the code you posted in the first post, `$title` would still equal `$trakt->movie->title`.

    CrocoDillon’s code above follows a cascade of `$trakt->movie->title`, `$trakt->show->title`, `”None”`, taking whichever first evaluates to true. The difference between his code and yours in the first post is that values of `””` and `”0″` will evaluate to false, while your original code would have allowed `$title` to equal that.

    # July 14, 2013 at 7:59 pm

    @srig99, @BenWalker, no problem :)

    @ChrisBurton, what do you want to happen if both are empty? Or both not empty? If that never happens I see no reason using `?:` wouldn’t work, unless like @BenWalker said you want `””` and `”0″` to be valid titles.

Viewing 15 posts - 1 through 15 (of 31 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".