Jump to content

Ternary Operator - This is wrong right?


SharkBait

Recommended Posts

I did but i guess i should of shown what I was doing that was erroring on me:

 

<?php
$_SESSION['rmbrNotes'] = isset($_POST['chkNotes']) : $notes ? unset($_SESSION['rmbrNotes']);
?>

 

I get this error: Parse error: parse error, unexpected T_UNSET in /home/tingram/public_html/testsite/test.php on line 236

 

So i thought it it was I couldn't run functions after it, so I posted a more simpler version of what I wanted to do :)

Ah solved it by making a function that returns a value ie:

 

<?php

function ClearSession($value) {
  if(isset($_SESSION[$value])) unset($_SESSION[$value])

  return 0;
}

$_SESSION['rmbrNotes'] = isset($_POST['chkNotes']) ? $notes : ClearSession("rmbrNotes");

?>

 

Though as I am looking at the above code.. it still isn't quite right is it? If I return 0 will it not set the $_SESSION variable??

 

The ternary operator will set the value of the first variable to the result of either the first statement or the second statement depending on whether the condition evaluates true.  It makes sense to use the ternary operator where you wish to assign one value or another depending on the condition.  If you only want to make an assignment in one case or the other, it doesn't make sense.  I would re-write your example as so:

 

<?php

if (isset($_POST['chkNotes'])) {
  $_SESSION['rmbrNotes'] = $notes;
} elseif (isset($_SESSION['rmbrNotes']) {
  unset($_SESSION['rmbrNotes']);
}

?>

  • 9 months later...

Greetings,

 

I was trying to minimize/optimize a few blocks of code when I arrived at this error. I have tried to find documentation about the prototype for ternary operators, but was unsuccessful.

Any ideas or hints as to why this returns an error or were I can find more documentation about ternary operator usage limits. Thanks in advanced. In the meanwhile I am going to continue my search.

 

Reproduce Code:

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

function foo($name){

 

($name == "") ? return 0 : echo $name."<br>" ;

 

}

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

Expected Result:

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

exit the current function

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

I understand your point, I have never tried to use ternary operators in this way before and when you mentioned "you're not applying the ternary operator to a value" is when I realized or remember why. Thanks for the second pair of eyes.

 

I am currently using if statements, there is nothing wrong with them. "I was trying to minimize/optimize a few blocks of code...". I find using a "minimal" amount of code is good way to "optimize" my applications.

 

thanks again,

 

~nolo

If by "optimize" you mean fewer lines of code or neater appearance, then that may be true, depending on your point of view.  You can also write an "if" in one line, though I don't think it looks as nice as the ternary operator.

 

I would not expect it to be any faster to execute.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.