The same or separated php tag

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
xzappa
Forum Newbie
Posts: 1
Joined: Sat Oct 09, 2010 7:49 am

The same or separated php tag

Post by xzappa »

Hi to all. I am asking maybe a stupid question, but i would like to know if there is any defference between the writing code to the same php tag or in the same. Example:

<?php include ("dir/filename.php"); ?>
<?php echo "{$variable_from_the_included_file}"; ?>

OR THIS WAY:

<?php
include ("dir/filename.php");
echo "{$variable_from_the_included_file}";
?>
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: The same or separated php tag

Post by twinedev »

I find code easier to maintain when the logic is separated from the presentation, so most of my php code is in either a separate file or all wrapped up by the time I hit the <html> tag.

Because of this, once I hit the presentation, I'll usually do html as needed, even if it means having it on two lines:

Code: Select all

<select name="optStates" id="optStates">
	<?php foreach ($aryStates as $abbr->$state): ?>
		<option value="<?php echo $abbr; ?>"><?php echo $state; ?></option>
	<?php endforeach; ?>
</select>
(that is also an example of the one place where I miss the use of short tags, not being able to do <?= $abbr ?> LOL

As for something such as speed, I wrote a small block that had a few commands, one where they were all within the same PHP tags, one where each had their own.
I had it loop through and do them each 10,000 times, and to be honest, my extremely non-scientific method didn't come up with anything major as a difference that I could call one better than the other.

-Greg
Post Reply