kucing Posted February 21, 2007 Share Posted February 21, 2007 Hi all.. I am having problem with proper syntax format. If i am not mistaken this syntax is right $myVar[$i]["var"] = ($a>1?$a=true:a=false); but when i try to make some change like this $myVar[$i]["var"] = (($a>1?$a=true:a=false)?$b=true:b=false); it doesn't seems to work As I want something like when the first parameter is true then the next parameter should work. Thanks I appreciate your help Link to comment https://forums.phpfreaks.com/topic/39518-solved-need-syntax-format/ Share on other sites More sharing options...
Jessica Posted February 21, 2007 Share Posted February 21, 2007 I don't think either is right. I'm not too familiar with the ternary, but I think it needs to be like this: $myVar[$i]["var"] = ($a>1?true:false); $a=true and $a=false will both return true, so in your first one it will always be true, because you get the value of that operation. Check out the example on this page: http://us2.php.net/ternary Example 15.3. Assigning a default value And this one: Note: Is is recommended that you avoid "stacking" ternary expressions. PHP's behaviour when using more than one ternary operator within a single statement is non-obvious: Example 15.4. Non-obvious Ternary Behaviour Link to comment https://forums.phpfreaks.com/topic/39518-solved-need-syntax-format/#findComment-190689 Share on other sites More sharing options...
JBS103 Posted February 21, 2007 Share Posted February 21, 2007 I would also just consider using an if statement, especially since you are trying to set 3 different variables at once ($a, $b, $myVar). Someone might be able to clarify, but I believe the ternary is generally used to set the outside variable. For example, in this case, it is checking $a and then setting $myVar. <?php $a = 0; $myVar = ($a > 1) ? true : false; echo $myVar; //Displays nothing ?> Link to comment https://forums.phpfreaks.com/topic/39518-solved-need-syntax-format/#findComment-190708 Share on other sites More sharing options...
itsmeArry Posted March 1, 2007 Share Posted March 1, 2007 use this $myVar = ($a > 1) ? 'true' : 'false'; Link to comment https://forums.phpfreaks.com/topic/39518-solved-need-syntax-format/#findComment-196950 Share on other sites More sharing options...
trq Posted March 1, 2007 Share Posted March 1, 2007 use this $myVar = ($a > 1) ? 'true' : 'false'; Still, if the op wants to set $myVar to a booleen.... $myVar = ($a > 1) ? true : false; is correct. Link to comment https://forums.phpfreaks.com/topic/39518-solved-need-syntax-format/#findComment-196959 Share on other sites More sharing options...
monk.e.boy Posted March 1, 2007 Share Posted March 1, 2007 use this $myVar = ($a > 1) ? 'true' : 'false'; Still, if the op wants to set $myVar to a booleen.... $myVar = ($a > 1) ? true : false; is correct. $myVar = ($a > 1); Wouldn't this do the same? monk.e.boy Link to comment https://forums.phpfreaks.com/topic/39518-solved-need-syntax-format/#findComment-196972 Share on other sites More sharing options...
obsidian Posted March 1, 2007 Share Posted March 1, 2007 $myVar = ($a > 1); Wouldn't this do the same? monk.e.boy Actually, no. I just tried it because you piqued my interest, but that method only assigns a value to $myVar upon a TRUE comparison. It would be much safer the way thorpe shows. Link to comment https://forums.phpfreaks.com/topic/39518-solved-need-syntax-format/#findComment-196994 Share on other sites More sharing options...
trq Posted March 1, 2007 Share Posted March 1, 2007 Wouldn't this do the same? Yes. Link to comment https://forums.phpfreaks.com/topic/39518-solved-need-syntax-format/#findComment-197001 Share on other sites More sharing options...
trq Posted March 1, 2007 Share Posted March 1, 2007 but that method only assigns a value to $myVar upon a TRUE Yes but no value is also the booleen FALSE. Link to comment https://forums.phpfreaks.com/topic/39518-solved-need-syntax-format/#findComment-197002 Share on other sites More sharing options...
obsidian Posted March 1, 2007 Share Posted March 1, 2007 but that method only assigns a value to $myVar upon a TRUE Yes but no value is also the booleen FALSE. True, but typically when you echo the boolean FALSE, it prints as a '0' so you know you have the value stored. This does not. Link to comment https://forums.phpfreaks.com/topic/39518-solved-need-syntax-format/#findComment-197004 Share on other sites More sharing options...
itsmeArry Posted March 1, 2007 Share Posted March 1, 2007 but that method only assigns a value to $myVar upon a TRUE Yes but no value is also the booleen FALSE. yes thats true... Link to comment https://forums.phpfreaks.com/topic/39518-solved-need-syntax-format/#findComment-197006 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.