runnerjp Posted November 8, 2008 Share Posted November 8, 2008 i know that they work like this but could it be also used in a code such as.. <?php if ($getthreads3['forumlock'] == 1) { echo ' 1'; } else { echo ' 2'; } ?> im just wanting to learn so it cuts my code down and makes me a little more efficient Link to comment https://forums.phpfreaks.com/topic/131961-solved-ternary-statements/ Share on other sites More sharing options...
Mchl Posted November 8, 2008 Share Posted November 8, 2008 No, they can't. Link to comment https://forums.phpfreaks.com/topic/131961-solved-ternary-statements/#findComment-685666 Share on other sites More sharing options...
runnerjp Posted November 8, 2008 Author Share Posted November 8, 2008 ok so it always has to be done like that as it is the shortes way Link to comment https://forums.phpfreaks.com/topic/131961-solved-ternary-statements/#findComment-685668 Share on other sites More sharing options...
Mchl Posted November 8, 2008 Share Posted November 8, 2008 Use less whitespace. That's all you can do. For example I would write it like this: if ($getthreads3['forumlock'] == 1) { echo ' 1'; } else { echo ' 2'; } You can also use an IDE with code templates. For example I use NetBeans, where I can type 'if' and press tab, and I get if (condition) { ; } with cursor placed over 'condition' so that I can type it right away, and then when I press enter I go straight before semicolon; Speeds things a little Nice thing is one can edit those templates to his/hers own liking. Link to comment https://forums.phpfreaks.com/topic/131961-solved-ternary-statements/#findComment-685677 Share on other sites More sharing options...
wildteen88 Posted November 8, 2008 Share Posted November 8, 2008 No, they can't. That OP code can be converted to a ternary operator. <?php echo ($getthreads3['forumlock'] == 1) ? '1' : '2'; ?> Link to comment https://forums.phpfreaks.com/topic/131961-solved-ternary-statements/#findComment-685680 Share on other sites More sharing options...
runnerjp Posted November 8, 2008 Author Share Posted November 8, 2008 haha learn summat new every day mchl thanks wildteen i will give it a shot! and mchl i will add the code into wildbean (gonna dl it now) Link to comment https://forums.phpfreaks.com/topic/131961-solved-ternary-statements/#findComment-685682 Share on other sites More sharing options...
runnerjp Posted November 8, 2008 Author Share Posted November 8, 2008 could it also work like this echo ($getthreads3['forumlock'] == 1) ? '1' ; Link to comment https://forums.phpfreaks.com/topic/131961-solved-ternary-statements/#findComment-685686 Share on other sites More sharing options...
Mchl Posted November 8, 2008 Share Posted November 8, 2008 Ok. This will work for echo. (I knew that, srsly ) I was talking in more general terms i.e. you can't do this with blocks of code. Link to comment https://forums.phpfreaks.com/topic/131961-solved-ternary-statements/#findComment-685687 Share on other sites More sharing options...
runnerjp Posted November 8, 2008 Author Share Posted November 8, 2008 Ok. This will work for echo. (I knew that, srsly ) I was talking in more general terms i.e. you can't do this with blocks of code. what do you mean? Link to comment https://forums.phpfreaks.com/topic/131961-solved-ternary-statements/#findComment-685689 Share on other sites More sharing options...
Mchl Posted November 8, 2008 Share Posted November 8, 2008 could it also work like this echo ($getthreads3['forumlock'] == 1) ? '1' ; Something like that will be possible from PHP 5.3 See http://wiki.php.net/doc/scratchpad/upgrade/53 at the very bottom. Link to comment https://forums.phpfreaks.com/topic/131961-solved-ternary-statements/#findComment-685691 Share on other sites More sharing options...
wildteen88 Posted November 8, 2008 Share Posted November 8, 2008 As in if(condition) { line 1 line 2 line 3 } else { line 1 line 2 line 3 } Link to comment https://forums.phpfreaks.com/topic/131961-solved-ternary-statements/#findComment-685692 Share on other sites More sharing options...
Mchl Posted November 8, 2008 Share Posted November 8, 2008 Ok. This will work for echo. (I knew that, srsly ) I was talking in more general terms i.e. you can't do this with blocks of code. what do you mean? I mean you will not be able to use ternary operator for something like this: if ($getthreads3['forumlock'] == 1) { echo ' 1'; doSomething(1); } else { echo ' 2'; doSomethingElse(2); } The only place you can use it, is where a statement is evaluated. Link to comment https://forums.phpfreaks.com/topic/131961-solved-ternary-statements/#findComment-685694 Share on other sites More sharing options...
runnerjp Posted November 8, 2008 Author Share Posted November 8, 2008 dang got 5.2,, have to stick to the method of if ($getthreads3['forumlock'] == 1) { echo ' <img src="http://www.runningprofiles.com/images/quick_lock.gif" alt="locked"/>'; } Link to comment https://forums.phpfreaks.com/topic/131961-solved-ternary-statements/#findComment-685699 Share on other sites More sharing options...
Mchl Posted November 8, 2008 Share Posted November 8, 2008 But you can do echo ($getthreads3['forumlock'] == 1) ? ' <img src="http://www.runningprofiles.com/images/quick_lock.gif" alt="locked"/>' : ''; Link to comment https://forums.phpfreaks.com/topic/131961-solved-ternary-statements/#findComment-685712 Share on other sites More sharing options...
runnerjp Posted November 8, 2008 Author Share Posted November 8, 2008 how would i echo the image? echo ($getthreads3['forumlock'] == 1) ? ' <img src="http://www.runningprofiles.com/images/quick_lock.gif" alt="locked"/>' : ''; Link to comment https://forums.phpfreaks.com/topic/131961-solved-ternary-statements/#findComment-685726 Share on other sites More sharing options...
wildteen88 Posted November 8, 2008 Share Posted November 8, 2008 That should work. Have you tested it? Link to comment https://forums.phpfreaks.com/topic/131961-solved-ternary-statements/#findComment-685727 Share on other sites More sharing options...
runnerjp Posted November 9, 2008 Author Share Posted November 9, 2008 yes i have tested it and it echos out <img src="http://www.runningprofiles.com/images/quick_lock.gif" alt="locked"/> in text form :S Link to comment https://forums.phpfreaks.com/topic/131961-solved-ternary-statements/#findComment-685890 Share on other sites More sharing options...
wildteen88 Posted November 9, 2008 Share Posted November 9, 2008 Where are you echoing it out to. You dont need to do anything special to output HTML within PHP. Link to comment https://forums.phpfreaks.com/topic/131961-solved-ternary-statements/#findComment-685907 Share on other sites More sharing options...
Mchl Posted November 9, 2008 Share Posted November 9, 2008 It should be output between <html></html> tags... just saying Link to comment https://forums.phpfreaks.com/topic/131961-solved-ternary-statements/#findComment-685909 Share on other sites More sharing options...
runnerjp Posted November 9, 2008 Author Share Posted November 9, 2008 humm all of a sudden it started working :S wierd lol... thanks guys for this handy lesson .. will be chnaging my code to cut down on the {} they annoy me having them all over the place lol Link to comment https://forums.phpfreaks.com/topic/131961-solved-ternary-statements/#findComment-685919 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.