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 Quote Link to comment Share on other sites More sharing options...
Mchl Posted November 8, 2008 Share Posted November 8, 2008 No, they can't. Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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'; ?> Quote Link to comment 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) Quote Link to comment 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' ; Quote Link to comment 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. Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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 } Quote Link to comment 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. Quote Link to comment 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"/>'; } Quote Link to comment 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"/>' : ''; Quote Link to comment 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"/>' : ''; Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted November 8, 2008 Share Posted November 8, 2008 That should work. Have you tested it? Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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 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.