-1

I have written a ternary function in PHP and it seems to work, although I am not sure if it is correct, can someone take a look and tell me if it is right?

I have added the ternary and the if of what should be happening.

    //Foreach Loop
    foreach ($post as $item) {
        //If of what should occur
        if ($passed == true) {
            if (is_numeric($item)) {
                if ($item > 0) {
                    $passed = true;
                }
                else {
                    $passed = false;
                }
            }
            else {
                if ($item != "") {
                    $passed = true;
                }
                else {
                    $passed = false;
                }
            }
            //Ternary operator.
            $passed = (is_numeric($item) ? ($item > 0 ? true : false) : ($item != "" ? true : false));
        }
        else {
            return $passed;
        }
    }
Can O' Spam
  • 2,718
  • 4
  • 19
  • 45
  • Can down vote tell me on what grounds they did it? (Want to know how to improve!) :) – Can O' Spam Jun 16 '15 at 09:15
  • I'm not the one who downvoted but the reason might be _can someone take a look and tell me if it is right_ – Narendrasingh Sisodia Jun 16 '15 at 09:20
  • The `$passed` is declared before the `foreach` loop – Can O' Spam Jun 16 '15 at 09:21
  • @PEM do you have any documented evidence proving ternaries are slower? or is this personal opinion. To the OP there is a code review stack overflow for this http://codereview.stackexchange.com/ – Dave Jun 16 '15 at 09:22
  • @Dave, that is exactly what I am looking for, thank you for letting me know of this :) – Can O' Spam Jun 16 '15 at 09:23
  • @Dave While this may be a little old : http://fabien.potencier.org/the-php-ternary-operator-fast-or-not.html I also noted that on several tests on my own use-cases. But again, it's more readability if you ask me :) – PEM Jun 16 '15 at 09:25
  • Nested ternary operators are bad if you ask me : you save a few lines/characters of code while losing much in readibility. each time you or a colleague will have to look at this part of code, he will have to draw himself a mental image of the "if" you just posted, and that's losing time ! – Laurent S. Jun 16 '15 at 09:32
  • @PEM interesting read I've never seen that before ta for link. – Dave Jun 16 '15 at 09:57

2 Answers2

2

please have a look on corrected code

$passed = (is_numeric($item))?($item>0?true:false):($item !="" ? true:false);
kevin
  • 300
  • 2
  • 11
-1

Honestly I do not really understand why you do not use a

if (!empty($item)) { 
    $passed = true; 
} else {
    return false; 
} 

In any case, ternaries are less readable than if /elseif / else, they are also slower (note that it is not an universal truth but a more general use case thing http://fabien.potencier.org/the-php-ternary-operator-fast-or-not.html). I would recommend, if you really need all these if and elses to keep them rather than using ternaries (for readability's purpose).

PEM
  • 1,948
  • 14
  • 13
  • Some people have a habit of downvoting without saying why they did it... It's about time SO asks downvoters to explain why they do so. – PEM Jun 24 '15 at 13:05