Jump to content

Ternary Operator - This is wrong right?


SharkBait

Recommended Posts

I can't do this can I?

 

<?php

$value = !empty($_POST['value']) ? $_POST['value'] : $message .= "You forgot something";

?>

 

You sure can.  If $_POST['value'] is empty, both $message and $value will equal "You forgot something", though.

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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??

 

Link to comment
Share on other sites

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']);
}

?>

Link to comment
Share on other sites

  • 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

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

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.