Jump to content

Recommended Posts

New. Simple question. A lot of times I will see an example coded something like this

 

  <body>  
    <?php if($this->values) { ?> 
    <h3>You just submitted the following values:</h3> 
    <ul> 
      <?php foreach ($this->values as $value) :?> 
      <li> 
        <?= $this->escape($value); ?> 
      </li> 
      <?php endforeach; ?> 
    </ul> 
    <?= $this->form; ?>  
  </body>  
</html> 

 

Actually this example didn't work. But the getting it to work isn't my question. To my addled brain it is easier for me to think of it as this.

 

  	<?php 
  	if ($this->values) {
		echo "<h3>You just submitted the following values:</h3>"; 
		echo "<ul>";
		foreach ($this->values as $value) {
			echo "<li>";
			echo $this->escape($value);
			echo "</li>";
		}
	}
	echo $this->form;
?> 

 

The former lays everything out with small PHP code chunks embedded into HTML. The latter is one big PHP code chunk that echos the HTML.

 

Is the second manner that much less efficient?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/107707-newbie-re-php-coding/
Share on other sites

It's argued that the less php code and the more straight html means less server processing and more client processing, which means faster page load times. 

 

edit:

 

while I'm not denying that idea at face value, 99.999% of the time we're talking about time and overall scale so small that nobody really truly should care unless they are anal retentive.  I personally hate that style of coding.  If I have extended html code I will occasionally wrap it up with HEREDOC

Link to comment
https://forums.phpfreaks.com/topic/107707-newbie-re-php-coding/#findComment-552173
Share on other sites

It's not less efficient, it's just that people are too lazy to escape quotes, so they exit out of PHP. =P

 

Btw, you can use a templating engine for something like this if you want.  Check out Smarty.

 

Thanks!

 

The example actually came from the Zend Framework quickstart. I was playing around with that.

Link to comment
https://forums.phpfreaks.com/topic/107707-newbie-re-php-coding/#findComment-552174
Share on other sites

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.