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

  • # July 14, 2013 at 8:25 pm

    @CrocoDillon I’m grabbing the last value in an array. So there will either be a `movie->title` or `show-title`. There can’t be both and neither one can be empty. One of them will return with a title string.

    I’m trying to simplify an if/elseif.

    ####Current code:

    $trakt = $data->activity[0];

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

    Basically do this (if it was valid):

    Let’s say the last thing I watched was a `movie`.

    IF $trakt->movie->title OR $trakt->show->title is empty then, $title = whichever isn’t empty

    The result would be the value of `$trakt->movie->title` since I watched a `movie` last. Not a `show`.

    __
    # July 14, 2013 at 8:45 pm

    This reply has been reported for inappropriate content.

    Beware PHP Sadness.

    Note that when “nesting” ternary operators (i.e., one ? inside another), you can quite easily end up with exactly what you didn’t want. Because PHP’s ternary operator is left-associative, it works on each condition in the opposite order you probably expect (“backwards”).

    As an example, you probably expect both of these to print “a”:

    print TRUE ? ‘a’: TRUE ? ‘b’: ‘c’;

    print FALSE ? FALSE ? ‘b’: ‘c’: ‘a’;

    In fact, you’re probably more certain about the first example printing “a” – but, in fact, it prints “b.” In math terms, the order-of-operations doesn’t work like this:

    (condition1 ? value1 : (condition2 ? value2 : value3;))

    but like this:

    ((condition1 ? value1 : condition2 )? value2 : value3;)

    Read more here if you’re interested. The long and short of it is, “don’t use nested ternary statements” if you’re not very sure of how they work.*

    * most of the time, stacking all of the conditions in the “if true” position, with all values at the end, will produce the expected results. most of the time.

    # July 14, 2013 at 8:54 pm

    This reply has been reported for inappropriate content.

    @traq yeah that’s pretty counter-intuitive but using it like ?: is pretty safe (though this wouldn’t work, thinking about it).

    expr1 ?: expr2 ?: expr3 is the same as (expr1 ?: expr2) ?: expr3.

    @ChrisBurton, maybe you should check the what the type of the last value is instead of checking it’s title:

    $trakt = $data->activity[0];

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

    see if that works.

    __
    # July 14, 2013 at 8:59 pm

    This reply has been reported for inappropriate content.

    There’s also

    $title = @$trakt->movie->title
    or $title = @$trakt->show->title
    or $title = “None”;

    A very unique use-case, where or is (usefully!) different than ||
    and @ is not completely evil. : )

    # July 14, 2013 at 9:03 pm

    @CrocoDillon It worked when the last thing I watched was a movie however, if the last thing I watched was a show, I get this:

    Notice: Undefined property: stdClass::$movie in trakt.php on line 10

    10. if($trakt->movie) {
    11. $title = $trakt->movie->title;
    12. } elseif($trakt->show) {
    13. $title = $trakt->show->title;
    14. } else {
    15. $title = “None”;
    16. }

    # July 14, 2013 at 9:06 pm

    @traq THAT WORKED!

    …doing some testing to make sure.

    # July 14, 2013 at 9:12 pm

    This reply has been reported for inappropriate content.

    Sometimes I wish PHP was as forgiving as JS :P

    # July 14, 2013 at 9:15 pm

    Thanks @CrocoDillon, @traq, @BenWalker and @srig99.

    Solution (by @traq):

    $title = @$trakt->movie->title
    or $title = @$trakt->show->title
    or $title = “None”;

    # July 14, 2013 at 9:31 pm

    This reply has been reported for inappropriate content.

    Can’t say I added much – what I posted was incorrect! (Sorry)

    @Traq – thanks for detailing so clearly how the ternary operator works. I can’t say I’ve ever stacked them before, but it’s certainly something I would readily have done without considering how they would be evaluated.

    # July 14, 2013 at 9:35 pm

    @BenWalker I still like to thank those who contribute their time.

    __
    # July 14, 2013 at 9:59 pm

    This reply has been reported for inappropriate content.

    @chrisburton, just to clarify, my

    $title = @$trakt->movie->title
    or $title = @$trakt->show->title
    or $title = “None”;

    works out exactly the same as @CrocoDillon‘s last suggestion (if you added @‘s):

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

    …which actually turns out to be about 15% faster to execute, according to quick-and-dirty benchmarks; plus is fancier-looking ;)

    # July 14, 2013 at 10:06 pm

    @traq, Hey buddy, the ? and none is comparable to sql or and null criteria?

    __
    # July 14, 2013 at 10:25 pm

    This reply has been reported for inappropriate content.

    @traq, Hey buddy, the ? and none is comparable to sql or and null criteria?

    umm… no?

    The ? in PHP is part of the ternary operator (I always think of it as the “is this true?” operator).
    In SQL, the ? is a placeholder for data in a prepared statement.

    In SQL, NULL (which is not the same as “None”, or even the idea of 0) means “unknown.”
    The "None" in my example above is simply the last (default) value in the assignment chain.

    But I’m not really sure I understand your question. If you meant something else, please explain further.

    # July 14, 2013 at 10:31 pm

    I have a good knowledge of DB Dev, as you prolly know, doing it for over 10 years (Access, MySql and SQL Server) but I am lately getting intrigued with PHP and how it works in conjunction with DB. Is it like a scripting language to retrieve attribute values from predefined Transact query and place it in html? Or can you actually query tbl with it?

    __
    # July 14, 2013 at 10:52 pm

    This reply has been reported for inappropriate content.

    As with HTML and everything else, when you get down to it, PHP only writes text. When you write an SQL statement in PHP (for example,
    $sql = "SELECT this FROM that WHERE other = '$myVar'";
    ), you’re really just making a string. PHP does nothing with it except send it, verbatum, to the database server.

    Of course, PHP has functions that wrap MySQL’s API libraries, so you can, more-or-less, do anything you can do with MySQL directly.

    Read up on MySQLi (my preference) or PDO. If you have more questions, however, you should probably start a new discussion instead of side-tracking @chrisburton‘s.

Viewing 15 posts - 16 through 30 (of 31 total)

You must be logged in to reply to this topic.

There's a whole bunch of content on CSS-Tricks.

Search for Stuff   •   Browse the Archives

Get the Newsletter ... or get the RSS feed