Jump to content

learning ternery operator


redarrow

Recommended Posts

Advance thank you as i have been learning php for a while i fort i get a new php 5 bible book

to see what i need to brush up on there are 45 chapters and i am on chapter 6

 

i need your help cheers.

 

I am learning the tenery operator from the below example but the result is always true why?

 

Can you clear this opertor up so i understand it properly.

 

 

// ? means if

 

// : means is true

 

<?php

$a=10;

$b=20;

if($a === $b ? $a : $b ){

echo " correct a is larger then b";
}else{
echo " not correct a is not larger then b";
}
?>

Link to comment
Share on other sites

The problem here is that you are using the triple equal sign, "===", which means the condition is true if the types of the arguments are the same.

 

The tenery operartor is a short way of doing an "if-else" block. The next two snippets of code do the same thing:

<?php
$a = 10;
$b = 20;
$result = ($a == $b)?$a:$b;
echo $result;
?>

 

<?php
$a = 10;
$b = 20;
if ($a == $b)
   $result = $a;
else
   $result = $b;
echo $result;
?>

 

Ken

Link to comment
Share on other sites

well okay, that's sort of true, but not entirely relevant here.. the problem redarrow is that yes, a ternary operator is a condition. So the problem here is that you have a condition nested inside a condition.  A condition will evaluate true or false.  Look at your code:

 

<?php

$a=10;

$b=20;

if($a === $b ? $a : $b ){

echo " correct a is larger then b";
}else{
echo " not correct a is not larger then b";
}
?>

You assign a value to $a and $b first.  Next you have an 'if' condition.  'if' what?  inside your 'if' statement you do a ternary operation.  if $a === $b return $a if it's true, $b if it's false.  So either way, it will return a value, since you assigned a value to both variables.  Therefore, your original 'if' statement will always look like one of the two:

 

if($a) { ...}

 

or

 

if($b) { ... }

 

since you assigned something to both of them in the first place, it will always return true.

 

edit:

 

the point of the ternary operator is, as ken stated: a shorthand if..else statement. 

 

if(true) {
  $blah = something;
else {
  $blah = something else;
}

 

can be shortened to a ternary operator like this:

$blah = (true) ? something : something else;

 

example:

 

$x = 1;
$blah = ($x == 1) ? "one" : "not one";

 

Link to comment
Share on other sites

The OP edited his original example while I was typing my response which is why it doesn't relate. His original example was

<?php
$a=10;

$b=20;

$result = $a === $b ? $a : $b;

if ($result) {
echo " correct a is larger then b";
}else{
echo " not correct a is not larger then b";
}
?>

Which is not correct either.

 

Ken

Link to comment
Share on other sites

i would like to no how this relates to programming the long way in speed or is all the ternary operator for programmers prefrence in style or is there a reel reason to use this style of programming?

 

i use it for small if statements. and to keep my code tidyer.

 

<?php
        $sql = "SELECT * FROM my_table";
        $query = mysql_query($sql);
        echo "<select name=\"drop_down\">\n";
        while($row = mysql_fetch_array($query)){
                echo "<option value=\"". $row['column1'] ."\"". (($row['column1'] == $_POST['drop_down']) ? (" SELECTED") : ("")) .">". $row['column2'] ."\n";
        }
        echo "</select>\n";

Link to comment
Share on other sites

if you write that as

<?php
$a=100;
$b=200;
$c=900;
$d=1000;
$result=((($a >$b)?$a:$b)?$c:$b)?$d:$a; 
?>

Here is what is happening:

($a>$b) is evaluated first and is "false", so the expression's result is "$b" or "200".

200 is evaluated as "true", so the next part's result is "$c" or 900.

900 is also evaluated as "true", so the final answer is "$d" or 1000.

 

Ken

 

Link to comment
Share on other sites

redarrow, it mostly just boils down to preference.  That's why it's called shorthand.  Like the others, I use it for short things.  Mostly if I have a variable that's going to be assigned something based on either one thing or the other, and that's all that needs to be done for that condition, I simply use the ternary operator. 

 

I wouldn't recommend nesting ternary operators like that.  If for no other reason, because it can easily become confusing to you, the programmer.  You want to strive for elegant coding, but part of elegance is clarity and ease of reading (by humans).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.