Moon-Man.net Posted June 8, 2007 Share Posted June 8, 2007 I know how to do it one several lines, and I have seen it done on ONE line before useing a ":" I think. this is how I know, what about any other ways? if(statement){ Do something ; } How can I do this one one line? if(isset($use_db)){ include('pg_connect.php') ; } Link to comment https://forums.phpfreaks.com/topic/54674-solved-if-statement-on-one-line-how/ Share on other sites More sharing options...
Caesar Posted June 8, 2007 Share Posted June 8, 2007 You mean, like this... <?php if($_SERVER['REMOTE_ADDR'] != '') echo'this works.'; ?> Basically anything following that if statement will output if it holds true. Are you refering to switches maybe? Link to comment https://forums.phpfreaks.com/topic/54674-solved-if-statement-on-one-line-how/#findComment-270382 Share on other sites More sharing options...
ToonMariner Posted June 8, 2007 Share Posted June 8, 2007 If you are referring to teh ternary operator it works like this... $x = isset($y) ? 10 : 5; so if y isset x will be 10 otherwise it will be 5. This method is only for assignment - you can't use for constructs or functions directly. You can however use the result for such operations. Link to comment https://forums.phpfreaks.com/topic/54674-solved-if-statement-on-one-line-how/#findComment-270386 Share on other sites More sharing options...
Moon-Man.net Posted June 8, 2007 Author Share Posted June 8, 2007 $x = isset($y) ? 10 : 5; That is what I saw. I don't quite understand how it works though, can someone help a little more please? Thanks -- Nathan Link to comment https://forums.phpfreaks.com/topic/54674-solved-if-statement-on-one-line-how/#findComment-270389 Share on other sites More sharing options...
Caesar Posted June 8, 2007 Share Posted June 8, 2007 $x = isset($y) ? 10 : 5; That is what I saw. I don't quite understand how it works though, can someone help a little more please? Thanks -- Nathan It's like he said... <?php $thisVar = isset($thatVar) ? 10 : 5; ?> If $thatVar has been set prior to this point, $thisVar will be assigned a value of 10...otherwise, it will equal 5. Link to comment https://forums.phpfreaks.com/topic/54674-solved-if-statement-on-one-line-how/#findComment-270391 Share on other sites More sharing options...
Dragen Posted June 8, 2007 Share Posted June 8, 2007 I believe it checks if $y has been set. The '?' checks true or false, then the first value before the ':' (which is ten in this case) will be what $x equals if it's true ($y has been set). Otherwise it will be false ($y has not been set) and $x will equal 5. $x = isset($y) ? 10 : 5; I hope that was clear.. and correct Link to comment https://forums.phpfreaks.com/topic/54674-solved-if-statement-on-one-line-how/#findComment-270392 Share on other sites More sharing options...
ToonMariner Posted June 8, 2007 Share Posted June 8, 2007 $x = $y >= 20 ? 10 : 5; OK $x = is the assignment part - this means that you will be assigning a value to $x the next term before the ? is the conditional statement - in this if ($y > 20).... ? means condition is met (true) then use the next value (or calculation) ---- $x = 10; : means condition is not met (false) then use this value (or calculation) ---- $x = 5; so you could do..... <?php $x = $_GET['foo'] > $_GET['bar'] ? $_GET['foo'] / $_GET['bar'] : $_GET['bar'] / $_GET['foo']; ?> Link to comment https://forums.phpfreaks.com/topic/54674-solved-if-statement-on-one-line-how/#findComment-270396 Share on other sites More sharing options...
Moon-Man.net Posted June 8, 2007 Author Share Posted June 8, 2007 Awesome, so I learned something new Thanks heaps guys, its a lot easier than doing that in a big if block... thanks people I love these forums, big help Link to comment https://forums.phpfreaks.com/topic/54674-solved-if-statement-on-one-line-how/#findComment-270406 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.