Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.
Not a member yet? Click here to register.
Forgot Password?

The Ternary Operator

Asked Modified Viewed 4,371 times
C
Craig
C
Craig 14
  • Fusioneer, joined since
  • Contributed 4,462 posts on the community forums.
  • Started 212 threads in the forums
  • Started this discussions
asked
Fusioneer

---------------------------------------------------------------------------
The Ternary Operator
---------------------------------------------------------------------------

The ternary operator takes three operands...

[ulist=disc]A Condition
A Result For True
A Result For False
[/ulist]


Sounds like an if statement right?
You are right but the ternary operator is a short hand method of doing if statements. Less code is nice yes?


Here's an example:

 <?php

 $member_check = (iMEMBER) ? "You Are a Member" : "You Are A Guest";
 
 echo $member_check;

?>



First there is a condition (iMEMBER), then a question mark (?), and then a true result (You Are a Member), followed by a colon (:), and a false result (You Are A Guest). If the user is a member (iMEMBER) $member_check will be set to 'You Are a Member', else it will be set to 'You Are A Guest'.


That ternary statement would be like this in a normal if statement:


 if (iMEMBER) {
       $member_check = "You Are a Member";
    } else {
       $member_check = "You Are A Guest";
    }

echo $member_check;



So, my point?
Using ternary operator's allows you to compact five lines of code into one, less code, less mess, more understandability amongst coders.
0 replies

8 posts

C
Chan
C
Chan 0
Lead Developer of PHP-Fusion
  • Super Admin, joined since
  • Contributed 3,841 posts on the community forums.
  • Started 232 threads in the forums
  • Answered 6 questions
answered
Super Admin

:) since I'm off duty.. Just a bit contribution.

To preview on post and blank on load.

$data['news_news'] = (isset($_POST['news_news']) && (!empty($_POST['news_news'])) ? addslashes(stripinput($_POST['news_news']) : '';


To load plural based on array count.


echo " ".((count($rows) >1) ? " ".(count($rows))." Fishes" : " ".(count($rows))."Fish";


These were classics in our code base.
0 replies
C
Craig
C
Craig 14
  • Fusioneer, joined since
  • Contributed 4,462 posts on the community forums.
  • Started 212 threads in the forums
  • Started this discussions
answered
Fusioneer

Hi,

Or like so...

echo " ".((count($rows) >1) ? " ".(count($rows))." ><(((º> ><(((º> ><(((º>" : " ".(count($rows))."><(((º>";


Better?

Regards

0 replies
C
Chan
C
Chan 0
Lead Developer of PHP-Fusion
  • Super Admin, joined since
  • Contributed 3,841 posts on the community forums.
  • Started 232 threads in the forums
  • Answered 6 questions
answered
Super Admin

Hahahahah ...
0 replies
C
Craig
C
Craig 14
  • Fusioneer, joined since
  • Contributed 4,462 posts on the community forums.
  • Started 212 threads in the forums
  • Started this discussions
answered
Fusioneer

Of course sometimes we still need to use if statements, sometimes we have conditions that would only work using an if statement. Example say you want to do something with a big block of code or if it has more than one condition inside it...

if (FUSION_SELF ="news.php") {

echo"All my code and that<br />";

echo"Moar code";

if($userdata['user_id'] ="49320") {

echo" Hej Jib";

}

}


Of course that can still be used in a ternary but listen, let's be practical.

Am I rite?
0 replies
P
PolarFox
P
  • Veteran Member, joined since
  • Contributed 1,633 posts on the community forums.
  • Started 29 threads in the forums
answered
Veteran Member

btw
$userdata['user_id'] =[bcolor=#ff6600]=[/bcolor] "49320"

very sneaky error

also
($userdata['user_id'] ="49320"wink
== always true or "49320"
0 replies
C
Chan
C
Chan 0
Lead Developer of PHP-Fusion
  • Super Admin, joined since
  • Contributed 3,841 posts on the community forums.
  • Started 232 threads in the forums
  • Answered 6 questions
answered
Super Admin

also this, but point taken from Jib.


FUSION_SELF =="news.php"


-------------

I wish to share also this... sneaky ***

See, sometimes you need to select stuff based on integer, 0,1,2,3,4..


$current_value = $data['current_value'] (i.e. is 0 from db).
$option = array('0'=>'Me', '1'=>'You', '2'=>'We');
foreach ($option as $arr=>$val) {
$selected = ($current_value == '0' ) ? "selected" : '';
echo "<option value='$arr' $selected>$val</option>\n";
}


There is an error above code ($arr=='0') // this is 0 the integer.
($arr == 0) // this is NULL!

If the DB returns 0 as selection, the select will not get selected unless, you do this:


if ($input_value || $input_value == '0') {
/* I.e. NOT NULL, OR Zero integer. */

                            $input_value = stripinput($input_value);
                            $select = (isset($input_value) && $input_value == $arr) ? "selected" : "";
                        }


Turning 0 to text via stripinput() doesn't help because stripinput() thinks it's a NULL too.

The 0 is the sneakiest among all errors. I hate it most. lol
0 replies
S
skpacman
S
My PHP-Fusion site: https://skpacman.live
  • Member, joined since
  • Contributed 150 posts on the community forums.
  • Started 25 threads in the forums
answered
Member

Quote

hien wrote:
Turning 0 to text via stripinput() doesn't help because stripinput() thinks it's a NULL too.

The 0 is the sneakiest among all errors. I hate it most. lol


That's why there is the "Is identical to" (===) operator along with double quotations (") instead of single quotations ('). It sees the 0 as a string in double quotes, then you test if a variable is identical to it.

if ($input === "0"){
//The input is identical to the number 0
}


(correct me if I'm wrong. This is what I understand of it)
Edited by skpacman on 04-04-2014 16:54,
0 replies
— 2 years later —
C
Chan
C
Chan 0
Lead Developer of PHP-Fusion
  • Super Admin, joined since
  • Contributed 3,841 posts on the community forums.
  • Started 232 threads in the forums
  • Answered 6 questions
answered
Super Admin

An information to share regarding the ternary statement.

$a = $a ? : 0;

is NOT

$a = $a ?: 0;

The earlier will treat you mean you want a whitespace and therefore returns a " ". The later will return $a.
Do not put space between ? and :
0 replies

Labels

None yet

Statistics

  • Views 0 views
  • Posts 8 posts
  • Votes 0 votes
  • Topic users 4 members

4 participants

C
C
Craig 14
  • Fusioneer, joined since
  • Contributed 4,462 posts on the community forums.
  • Started 212 threads in the forums
  • Started this discussions
C
C
Chan 0
Lead Developer of PHP-Fusion
  • Super Admin, joined since
  • Contributed 3,841 posts on the community forums.
  • Started 232 threads in the forums
  • Answered 6 questions
P
P
  • Veteran Member, joined since
  • Contributed 1,633 posts on the community forums.
  • Started 29 threads in the forums
S
S
My PHP-Fusion site: https://skpacman.live
  • Member, joined since
  • Contributed 150 posts on the community forums.
  • Started 25 threads in the forums

Notifications

Track thread

You are not receiving notifications from this thread.

Related Questions

Not yet