PHP
Article
By Lami Adabonyan

Video: Shorthand if-else Conditionals with PHP

By Lami Adabonyan

In this screencast I’ll show you how to make your code more succinct by using the ternary operator to write shorthand if-else conditional statements in PHP.

Loading the player…

  • Julius Koronci

    I am sorry to say but this is one of the worst things I have seen here..I was expecting a discussion when to use the shorthand because it is not always readable and can cause issues to understand your code..of course there is the speed issue..is the shorthand version still slower in php 5.6 when using earlier versions of php and shorthand with huge arrays..you can run out of memory and it can cause big speed issues when used incorrectly..

    I mean if you just need the syntax its at php.net there is no need for a video like this..so I am kind of disapointed

    • Anónimo

      Sorry, can you explain or referer to speed issues in short conditionals? All my tests were always similar in memory usage or speed http://pastebin.com/49SgCeA8

    • mrLami

      I generally use the short form in cases I discussed in video where I’m initializing a variable based on a condition or if another value is set.

      I certainly won’t go crazy with it and do something like below because it starts to defeat the purpose of being readable.


      $result = condition ? 'result 1' :
      (secondCondition ? 'result 2' :
      (thirdCondition ? 'result 3' : 'result 4'));

  • dkuye

    Thanks for the video. You should have call it “Intro to PHP conditional statement”.

  • Anónimo

    Hello SitePoint, why my comment about questions to Juilus Koronci was deleted? Thanks.

    • Julius Koronci

      Hi..and what was your question..I think it took more than a day to show my comment..?

      • Anónimo

        My question was for you, about some place where explain speed and memory issues. All my tests say that short if and regular if are same in memory and speed http://pastebin.com/49SgCeA8 (shot if a little bit faster). It was made 4 days ago.

      • Anónimo

        Sorry, I can’t post my original comment (I don’t know why), always is deleted.

  • You kept getting banned because you’re not a registered user and you have links in your comment. I have approved you manually now.

  • You kept getting banned because you’re not a registered user and you have links in your comment. I have approved you manually now.

    • Anónimo

      mmm, “registered”? where? at Disqus or SitePoint? Thanks :)

      • At Disqus. Or if you’re registered, I guess it just flagged pastebin links as suspicious, I don’t know :( Anyway, should be OK now

    • Anónimo

      mmm, “registered”? where? at Disqus or SitePoint? Thanks :)

    • Anónimo

      mmm, “registered”? where? at Disqus or SitePoint? Thanks :)

    • Anónimo

      mmm, “registered”? where? at Disqus or SitePoint? Thanks :)

    • Anónimo

      mmm, “registered”? where? at Disqus or SitePoint? Thanks :)

  • You kept getting banned because you’re not a registered user and you have links in your comment. I have approved you manually now.

  • Hi, I have read a few good articles about it but unfortunately I don’t have the links..but I believe there is no difference in speed in PHP 5.6..but in previous versions there were use cases where it could become an issue..thats what I was actually looking for in this blog post :)

    • Anónimo

      I have made some tests more in my dev server with phpfarm.

      The results are very similar in speed and same memory usage.

      # /opt/phpfarm/inst/php-5.3.29/bin/php -v

      PHP 5.3.29 (cli) (built: Jul 15 2015 14:31:21) (DEBUG)
      Copyright (c) 1997-2014 The PHP Group
      Zend Engine v2.3.0, Copyright (c) 1998-2014 Zend Technologies

      # /opt/phpfarm/inst/php-5.3.29/bin/php if-vs-shortif.php

      SHORT: 4.9286470413208 – MEMORY: 650696 – MEMORY PEAK: 3585168
      LARGE: 4.9436099529266 – MEMORY: 650808 – MEMORY PEAK: 3586112

      # /opt/phpfarm/inst/php-5.3.29/bin/php if-vs-shortif.php

      LARGE: 4.8195848464966 – MEMORY: 650696 – MEMORY PEAK: 3585168
      SHORT: 4.918417930603 – MEMORY: 650808 – MEMORY PEAK: 3586112

      # /opt/phpfarm/inst/php-5.4.39/bin/php -v

      PHP 5.4.39 (cli) (built: Jul 27 2015 12:28:14) (DEBUG)
      Copyright (c) 1997-2014 The PHP Group
      Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies

      # /opt/phpfarm/inst/php-5.4.39/bin/php if-vs-shortif.php

      SHORT: 6.4380738735199 – MEMORY: 245504 – MEMORY PEAK: 3180712
      LARGE: 6.062686920166 – MEMORY: 245720 – MEMORY PEAK: 3182240

      # /opt/phpfarm/inst/php-5.4.39/bin/php if-vs-shortif.php

      LARGE: 6.6514749526978 – MEMORY: 245504 – MEMORY PEAK: 3180712
      SHORT: 6.9146828651428 – MEMORY: 245720 – MEMORY PEAK: 3182240

      I think that speed or memory usage isn’t a real problem to use short conditionals :)

      Regards!

      • Hi..check out this post from Fabien Potencier fabien.potencier.org/the-php-ternary-operator-fast-or-not.html

      • My reply is pendiing because of a link..just put this in google and read the article: fabien potencier the-php-ternary-operator-fast-or-not

  • var_dump(“123” == “123xyz”)
    welcome to php

Recommended
Sponsors
Get the latest in PHP, once a week, for free.