CyberShot Posted January 18, 2009 Share Posted January 18, 2009 I have seen a few times in my studies that people have used the php tags in their code but didn't explain why they did it the way the did. for example One tutorials I am watch is showing this code <?php $ages = array(4, 8, 15, 16, 23, 42); ?> <php foreach($ages as $age) { echo $age . ","; } ?> So my question is. Why put the array in a seperate <?php code block and the foreach loop in another? Is there any reason or just personal preferance? I found the code works both ways. I have see others put each line in a seperate block. Here is an example from a wordpress theme <?php if(function_exists('wp_pagenavi')) { ?> <?php wp_pagenavi(); ?> <?php } else { ?> <div class="nav-previous"><?php next_posts_link(__('<span class="meta-nav">«</span> Older posts', 'thematic')) ?></div> <div class="nav-next"><?php previous_posts_link(__('Newer posts <span class="meta-nav">»</span>', 'thematic')) ?></div> <?php } ?> Why not just put it all in the same tag? Quote Link to comment https://forums.phpfreaks.com/topic/141278-question-on-code-form/ Share on other sites More sharing options...
dawsba Posted January 18, 2009 Share Posted January 18, 2009 in your example theres no difference just coding style, ive done on many occasions before final beta special when u remove code that would normal go in between, i also use when sectioning loops instead of doing \" \" 3gagillion times. hope this answers that .... Quote Link to comment https://forums.phpfreaks.com/topic/141278-question-on-code-form/#findComment-739458 Share on other sites More sharing options...
RussellReal Posted January 18, 2009 Share Posted January 18, 2009 okay, many times you see many php tags because "echo" is four extra letters to type.. and also encasing (quoting) the output html.. E.G. <div class="nav-previous"><?php next_posts_link(__('<span class="meta-nav">«</span> Older posts', 'thematic')) ?></div> <div class="nav-next"><?php previous_posts_link(__('Newer posts <span class="meta-nav">»</span>', 'thematic')) ?></div> you might have to escape "s and 's which is tedius also ?> ends that code block which makes anytthing after ?> normal html when you re-open <?php it picks the php interpreter back up and then everything inside becomes php code again.. php echoes into html, or outputs plain text so doing <?php ?> in various locations is completely fine.. in your examples you also showed the use of an if statement like: <?php // THIS IS PHP if (true) { ?> THIS IS NOW PLAIN TEXT BECAUSE OF THE ?> <html goes here> <?php // REOPENS PHP //CLOSES IF STATEMENT } ?> ^^ terminates php again although this works I advise you to stay away from this form of php construction, it could lead to many errors.. Quote Link to comment https://forums.phpfreaks.com/topic/141278-question-on-code-form/#findComment-739459 Share on other sites More sharing options...
CyberShot Posted January 18, 2009 Author Share Posted January 18, 2009 some of that makes sense. but this doesn't. in the code I posted above. it's all php <?php wp_pagenavi(); ?> <?php } else { ?> why not make that into this <?php wp_pagenavi(); } else { ?> they have it in two code blocks for what apears to me to be no reason Quote Link to comment https://forums.phpfreaks.com/topic/141278-question-on-code-form/#findComment-739517 Share on other sites More sharing options...
dropfaith Posted January 18, 2009 Share Posted January 18, 2009 yea there really is no reason behind that unless they broke it up to explain it better in the tutorial but for that small of a code theres no need to break it up Quote Link to comment https://forums.phpfreaks.com/topic/141278-question-on-code-form/#findComment-739520 Share on other sites More sharing options...
CyberShot Posted January 18, 2009 Author Share Posted January 18, 2009 that chunk of code was taken right out of a wordpress theme. One that comes with the install. I was trying to make sense out of why they would do that. Quote Link to comment https://forums.phpfreaks.com/topic/141278-question-on-code-form/#findComment-739524 Share on other sites More sharing options...
trq Posted January 18, 2009 Share Posted January 18, 2009 Wordpress and its themes really isn't a good place to go looking for good code design or examples. Quote Link to comment https://forums.phpfreaks.com/topic/141278-question-on-code-form/#findComment-739536 Share on other sites More sharing options...
CyberShot Posted January 18, 2009 Author Share Posted January 18, 2009 I am not exactly looking for good code there. But the example I posted was perfect for my question. I am learning php so that I can work with wordpress more. Do you think they are bad coders or that the code is just hacked apart by the people who make custom themes? Quote Link to comment https://forums.phpfreaks.com/topic/141278-question-on-code-form/#findComment-739538 Share on other sites More sharing options...
dropfaith Posted January 18, 2009 Share Posted January 18, 2009 ive noticed alot of bad coding on the huge websites wordpress ,facebook and mysapce have huge errors on xhtml and css things its sad really Quote Link to comment https://forums.phpfreaks.com/topic/141278-question-on-code-form/#findComment-739540 Share on other sites More sharing options...
trq Posted January 18, 2009 Share Posted January 18, 2009 All I was saying is wordpress is definitely not a good example of good coding practice. I guess that means yes, I would consider wordpress developers to be bad coders. Quote Link to comment https://forums.phpfreaks.com/topic/141278-question-on-code-form/#findComment-739545 Share on other sites More sharing options...
DarkSuperHero Posted January 18, 2009 Share Posted January 18, 2009 i agress with that last wordpress/myspace/facebook comment.... but then again its to many hands in the pot to keep it all consistant at times.... Quote Link to comment https://forums.phpfreaks.com/topic/141278-question-on-code-form/#findComment-739546 Share on other sites More sharing options...
CyberShot Posted January 18, 2009 Author Share Posted January 18, 2009 as someone who is learning php. I would say that learning to make themes for wordpress is a very good way to learn how to use php. It's growing in popularity amongst web designers for many reasons. One of which is that is has so many plugins that can be added to it. I am going to use wordpress to sharpen my php skills. Maybe when I learn a lot more I will be able to agree with you on it's level of coding. But for now, I can't tell if it's bad. Quote Link to comment https://forums.phpfreaks.com/topic/141278-question-on-code-form/#findComment-739547 Share on other sites More sharing options...
trq Posted January 18, 2009 Share Posted January 18, 2009 [but for now, I can't tell if it's bad. Even more of a reason not to try and learn form it. All your really going to be learning are bad coding practices. Sure, put together some themes. You don't need to know a hell of alot of php to do so anyway. But for learning php, Id stick with a good book. Quote Link to comment https://forums.phpfreaks.com/topic/141278-question-on-code-form/#findComment-739553 Share on other sites More sharing options...
CyberShot Posted January 18, 2009 Author Share Posted January 18, 2009 I did buy a book. I am also watching video's on www.lynda.com and testing the code out in dreamweaver using wamp. It's working very good. Then when I get stuck or have trouble understanding something, my plan is to come here to get it sorted properly. I am working hard to learn proper coding skills. Quote Link to comment https://forums.phpfreaks.com/topic/141278-question-on-code-form/#findComment-739555 Share on other sites More sharing options...
dropfaith Posted January 18, 2009 Share Posted January 18, 2009 im not sure how many times i will say this but really the best site i found is tizag.com for learning php.. also you need to remember theres some pretty damn googd tutorials on this site Quote Link to comment https://forums.phpfreaks.com/topic/141278-question-on-code-form/#findComment-739556 Share on other sites More sharing options...
RussellReal Posted January 18, 2009 Share Posted January 18, 2009 and don't use dreamweaverrrrr ... USE notepad!! Quote Link to comment https://forums.phpfreaks.com/topic/141278-question-on-code-form/#findComment-739581 Share on other sites More sharing options...
dropfaith Posted January 18, 2009 Share Posted January 18, 2009 notepad ++ normal notepad is a biatch to code in i love my colored tags too much to leave notepad ++ mac users hyperedit or taco html edit Quote Link to comment https://forums.phpfreaks.com/topic/141278-question-on-code-form/#findComment-739583 Share on other sites More sharing options...
CyberShot Posted January 18, 2009 Author Share Posted January 18, 2009 I have wamp installed. I set up dreamweaver to use the wamp as a test server. I have it set on split view right now. I am using dreamweaver cs4 and it rocks. I can enter code and then press the live view button and it displays the results in the design view. No uploading, no entering addy's in the browser, just one press of a button. It also gives me the errors when there are errors. How can you not use dreamweaver? Quote Link to comment https://forums.phpfreaks.com/topic/141278-question-on-code-form/#findComment-739587 Share on other sites More sharing options...
trq Posted January 18, 2009 Share Posted January 18, 2009 Theres nothing wrong with using dreamweaver as an IDE. Some people however go to far an let DW write alot of code for them, DW is not the best at writting code. Thats what programmers are for. Quote Link to comment https://forums.phpfreaks.com/topic/141278-question-on-code-form/#findComment-739592 Share on other sites More sharing options...
dropfaith Posted January 18, 2009 Share Posted January 18, 2009 most of those things exist on hyperedit i never really liked dreamweaver but havent tryed it out in ages maybe its changed alot Quote Link to comment https://forums.phpfreaks.com/topic/141278-question-on-code-form/#findComment-739593 Share on other sites More sharing options...
CyberShot Posted January 18, 2009 Author Share Posted January 18, 2009 the new version..dreamweaver cs4. does write better code. It writes css shorthand even. I will also put the css into the external stylesheet. I don't like using dreamweaver to write my code. I do all the code writing. I think people who design a website with dreamweaver only can not call themselves web designers. It is almost insulting to think that they would even try. What I really do like about dreamweaver is the support for ftp, and code writing. If I open a document that has scripts attached to the document, cs4 will automatically open that document is a sub window. That way I can make my changes to the code without having to leave the main page. It will also render code in design view now. If you press the live view button, it will show you your javascript right in the window. I love the ability to hit control + s to save and send my documents to the web server. Then all I have to do is refresh a browser to see how it looks. In the old days, using notepad, I had to minimize the window, find the file on the desktop, open the ftp window, drag and drop, click yes to overwrite then refresh. wow! Dreamweaver took all that away. So yes, I do like dreamweaver for those functions. As for code writing. I do all of it myself. Quote Link to comment https://forums.phpfreaks.com/topic/141278-question-on-code-form/#findComment-739825 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.