1

I am new to PHP and I am struggling to see why my code is not working when the echo displays 0. The code works for everything else however whenever the random operator chooses 0 it outputs 0 Negative, when I have it to display as Zero. I have no idea why it is doing this and I would appreciate any guidance.

$randomNumber = rand(-5, 5);
$var = $randomNumber;
echo $randomNumber;
echo $integerValue = (($var === 0) ? " Zero" : ($var <=-1) ? " Negative" : (($var >=1) ? " Positive" : " Not Positive or Zero") );
Rasclatt
  • 12,498
  • 3
  • 25
  • 33
madoreo
  • 33
  • 2
  • 9
  • 2
    Ternary operators are best used if there are only 2 outcomes. For readability and easier troubleshooting, I would think about using `if` / `else` on that. – Rasclatt Aug 08 '17 at 18:21

4 Answers4

4

The problem is due to PHP ternary operator precedence, which works backwards compared to most other languages: Understanding nested PHP ternary operator

Try this last line:

echo $integerValue = (($var === 0) ? " Zero" : (($var <=-1) 
                 ? " Negative" : (($var >=1) 
                 ? " Positive" : " Not Positive or Zero") ));
Kostas Stamos
  • 175
  • 1
  • 3
  • 16
0

You don't want to echo the assignment. You need to assign the value, then echo it. Try changing the last line to

$integerValue = (($var === 0) ? " Zero" : ($var <=-1) ? " Negative" : (($var >=1) ? " Positive" : " Not Positive or Zero") ); 

and then adding the line

echo $integerValue;
Ravinder Reddy
  • 3,869
  • 1
  • 13
  • 22
iavery
  • 554
  • 3
  • 9
  • I tried this and it didn't change the output for '0'. However thanks for the tip on changing the echo – madoreo Aug 08 '17 at 18:20
  • Oh, sorry, I missed the original issue you were having. Wouldn't you want anything between -1 and 1 to come back as zero? Like this: `$integerValue = (($var < 1 && $var > -1) ? " Zero" : ($var <=-1) ? " Negative" : (($var >=1) ? " Positive" : " Not Positive or Zero") );` – iavery Aug 08 '17 at 18:21
  • While not normal, the assignment will happen before the echo statement. So php will assign `$integerValue` and then echo'ing whatever value holds. Here is a quick demo: https://3v4l.org/CaemK. This is similar to how `while($row = mysqli_fetch...)` works in that row will be assigned before any expression is evaluated. – Jonathan Kuhn Aug 08 '17 at 18:23
  • I need the output to display: '0' Zero - anything below zero as: -1 negative - and anything above zero as: 2 positive, for example – madoreo Aug 08 '17 at 18:25
  • Try adding parens around the nested ternary operator like so. This will get you the outcome you are looking for: `$integerValue = (($var === 0) ? " Zero" : (($var <=-1) ? " Negative" : (($var >=1) ? " Positive" : " Not Positive or Zero")));` – iavery Aug 08 '17 at 18:31
0

You are missing a parentheses:

<?php
$randomNumber = rand(-5, 5);
$var = $randomNumber;
echo $randomNumber;
echo $integerValue = (($var === 0) ? " Zero" : (($var <=-1) ? " Negative" : (($var >=1) ? " Positive" : " Not Positive or Zero") ) );

Explanation:

(
    ($var === 0) ? " Zero" : (
        ($var <=-1) ? " Negative" : (
            ($var >=1) ? " Positive" : " Not Positive or Zero"
        )
    )
)
Jonathan
  • 6,507
  • 5
  • 37
  • 47
0

You already have another answer that explains why you weren't getting the outcome you expected, but just FYI, your multiple ternary expression can be simplified somewhat.

echo $var ? $var > 0 ? ' Positive' : ' Negative' : ' Zero';

$var will evaluate to false if it is zero so there's no need to explicitly compare it to zero, and the final "Not Positive or Zero" isn't really a possible outcome, as the result of rand will be an integer, which is either positive, negative, or zero by definition.

Parentheses aren't necessary, but they may make it more obvious what the expression is doing.

echo $var ? ($var > 0 ? ' Positive' : ' Negative') : ' Zero';
Don't Panic
  • 41,125
  • 10
  • 61
  • 80