Jump to content

Recommended Posts

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?

 

Link to comment
https://forums.phpfreaks.com/topic/141278-question-on-code-form/
Share on other sites

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 ....

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..

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

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?

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.

[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.

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.

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?

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.