You are using a browser which is not compatible with CSS (for more information, see Tara's tutorial).
Because of this, it is possible that our website may not appear
correctly in your browser. We apologise for the inconvenience, and
recommend you upgrade your browser to one which is compatible with CSS.
For more information, please visit our Browser Upgrade page.
Something very important when coding a substantial script, is the time it takes to be processed by PHP. Some people seem to forget this, and write scripts which take an unnecessarily long time to be parsed.
Now, I'm not going to go through all the ways to make your scripts more efficient; I'll just focus on one particular thing: the use of the echo(); command. This command is overused in PHP, often by ignorance.
The alternative to echo();, when all you want to output is pure HTML, is "inline HTML".
Did you know that echo(); is twice as slow as inline HTML?
Here's an example of the improper use of echo();:
<?php
if ($variable=="bla")
{
echo ("<b>bla HTML Code</b>");
}
?>
Here's an example of "inline HTML":
<?php
if ($variable=="bla")
{
?>
<b>bla HTML Code</b>
<?
}
?>
Not only is this approach faster, it will also save putting those backslashes in front of " and '.
© 4WebHelp and Daniel
oh and i also hate when people use
if($v == 'no') {
echo 'why not?';
} else
if( $v == 'yes' ) {
echo 'oh! yes!';
} else {
echo 'what kind of answer is that?'; }
but yea anyway here is the code below highlighted.
<?php
$v = $_GET['v'];
switch ($v) {
case "1":
echo '<b>Yeeeehaa! Let's get it on!</b>';
break;
case "0":
echo '<i>Doh! Not now!</i>';
break;
}
?>
<?php
$v = $_GET['v'];
switch ($v) {
case "1":
echo '<b>Yeeeehaa! Let's get it on!</b>';
break;
case "0":
echo '<i>Doh! Not now!</i>';
break;
}
?>
There is no benchmarking at all, so there are really no good reasons for that because php scripts are executed by php and html is also filtered by php!
Someone may begin to argue by saying, "your wrong because the php engine still has to look at echo and echo it" but that is incorrect because the entire script goes through the php engine anyway.
single quotes = fast, double quotes = slow, <<< = slow.
Hope that helps.
J
echo "<a href=\"somelink.php\">";
?>
echo '<a href="somelin.php">';
?>
For the ones who allways get the nice little error when writing HTML with echo
<?php
if ($v==1) {
?>
<b>Yeeeehaa! Let's get it on!</b>
<?php
}
elseif ($v==0) {
?>
<i>Doh! Not now!</i>"
<?php
}
?>
if ($v==1) {
?>
<b>Yeeeehaa! Let's get it on!</b>
<?php
}
else {
?>
<i>Doh! Not now!</i>"
<?php
}
?>
That code doesn't need the many different opening and closing tags. Just use:
<?php
if ($v=="1")
{
echo ('<b>Yeeeehaa! Let's get it on!</b>}');
}
else
{
echo ('<i>Doh! Not now!</i>');
}
?>
That just makes the coding easier on the eyes.
((NOTE: I am just getting into PHP - correct me if the code I have posted won't work, but I think it will...))
~Daskar
if ($v==1) : //note the ":"
?>
<b>Yeeeehaa! Let's get it on!</b>
<?
else : //note the ":"
?>
<i>Doh! Not now!</i>
<?
endif
?>
So it is faster to use:
than
"what about readability na dproper PHP coding?"
haha@ the unintended irony of that sentence
anyway, thanks Daniel!
That is a personal coding choice. I myself have not read anything which says you have to do that.
Quote:
Let's face it, in the end most of the HTML output by a PHP script is usually static. However for the HTML that contains variables, you may find it easier to use an echo statement. I personally find it "cleaner" to keep my HTML seperate (this is going one step further towards the seperation of code and layout, although much more can be done).
I though that if you were going to drop out of php in the middle of a control structure you were supposed to use the alternate syntax of if :
elseif:
endif:
instead of encapsulating with curly brackets.
my other comment is readability.
If you are populating a table for instance from a database, why would I use inline HTML and then have 12 <?=$text;?> in the middle of the HTML then???
just curious
echo<<< END
...code
END;
if ($v==1) {
?>
<b>Yeeeehaa! Let's get it on!</b>
<?php
}
else {
?>
<i>Doh! Not now!</i>"
<?php
}
?>
Add a new comment