Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Invoking PHP

Status
Not open for further replies.

MrPantsTm

Programmer
Joined
Jan 22, 2004
Messages
71
Location
US
Is there any performance hit if I call:
<?php
code here
?>

multiple times?

In other words, I have snippets of php code which I end and then start new. The reason I do this is a personal way of thinking of things. To me it delineates where certain things begin and end. Just makes things clearer really.

So my code looks like:

<?php
code here
?>

HTML here

<?php
code here
?>

<?php
code here
?>

HTML here

<?php
code here
?>

HTML here


etc....

I was wondering if I'm hurting my sites performance by calling the php tag. Does the tag just tell the server, hey I'm php code do something with me or does it load up something like a library. I feel like it is the former but I was curious if anyone could clarify it for me. Thanks.
 
I have been trying to answer that question for some time now. I've never found a definitive answer -- or even an educated guess, for that matter.

I recommend against using the &quot;context switch&quot; style of programming for another reason. As your programs get more complex, it's a lot easier to follow the program flow if you stay in interpreted mode and use print statements.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Could you explain &quot;context switch&quot; style and &quot;interpreted mode&quot; style more. I think I know what you mean.

By context you mean switching between php (or whatever) and html (or whatever) while interpreted means straight shots of one language followed by another?

Thanks. I'm glad I'm not the only one wondering this.
 
&quot;Interpreted mode&quot; is the condition PHP is in when it is parsing a script between a set of <?php...?> tags. &quot;Pass-through mode&quot; is the condition PHP is in when outside those tags:


<?php
//interpreted mode begin

print 'foo';

//interpreted mode end
?>

pass-through mode output

<?php
//re-enter interpreted mode

print 'bar';

//end interpreted mode
?>


&quot;Context switch style&quot; is like the above program example. You switch between the interpreted context and the pass-through context.

The other style, call it &quot;Single context mode&quot;, is where the first line of your script is the PHP interpreted-mode entry tag (&quot;<?php&quot;) and the last line is the end-interpreted-mode tag (&quot;?>&quot;). Here is a singexample that is functionally equivalent to the previous example:

<?php
print &quot;foo&quot;;
print &quot;pass-through mode output&quot;;
print &quot;bar&quot;;
?>

My contention is that single-context mode is much more readable, particularly as opposed to switching contexts in mid-HTML-tag:

<?php
//do some stuff
?>
<form method=&quot;POST&quot; action=&quot;<?php print $PHP_SELF; ?>&quot;>
<?php
//do some more stuff
?>


Want the best answers? Ask the best questions: TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top