SharkBait Posted May 14, 2007 Share Posted May 14, 2007 I can't do this can I? <?php $value = !empty($_POST['value']) ? $_POST['value'] : $message .= "You forgot something"; ?> Quote Link to comment Share on other sites More sharing options...
Fergusfer Posted May 14, 2007 Share Posted May 14, 2007 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. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted May 14, 2007 Share Posted May 14, 2007 Instead of asking here, try it. I tried it and it assigned the value "You forgot something" to both $value and $message; Ken Quote Link to comment Share on other sites More sharing options...
SharkBait Posted May 14, 2007 Author Share Posted May 14, 2007 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 Quote Link to comment Share on other sites More sharing options...
SharkBait Posted May 14, 2007 Author Share Posted May 14, 2007 But I guess its erroring because like you mention how it sets the first variable to whatever is being assessed? so $_SESSION['rmbrNotes'] Cannot Be set to unset() right? Quote Link to comment Share on other sites More sharing options...
SharkBait Posted May 14, 2007 Author Share Posted May 14, 2007 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?? Quote Link to comment Share on other sites More sharing options...
Fergusfer Posted May 14, 2007 Share Posted May 14, 2007 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']); } ?> Quote Link to comment Share on other sites More sharing options...
btherl Posted May 15, 2007 Share Posted May 15, 2007 The reason for the error is that unset is not a function, it's a php construct. It can't be used in some contexts (like inside a ternary operator). Quote Link to comment Share on other sites More sharing options...
SharkBait Posted May 15, 2007 Author Share Posted May 15, 2007 Thanks for the answers and explanations Quote Link to comment Share on other sites More sharing options...
nolochemical Posted February 28, 2008 Share Posted February 28, 2008 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 ---------------------------------------------------------------------------- Quote Link to comment Share on other sites More sharing options...
aschk Posted February 28, 2008 Share Posted February 28, 2008 Again you're using a construct (echo), and you're not applying the ternary operator to a value. You're using it like it's an if statement. What makes you think ternary operators are "more optimised" over if statements? Quote Link to comment Share on other sites More sharing options...
nolochemical Posted February 28, 2008 Share Posted February 28, 2008 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 Quote Link to comment Share on other sites More sharing options...
btherl Posted February 29, 2008 Share Posted February 29, 2008 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.