Most PHP books begin by telling you how you can output text using echo or print. I find it interesting how the langauge can display text without even any explicit function calls:
<?
$test=1;
if ($test==1){
?>
<b>This text is displayed if $test equals 1</b>
<?
}
?>
Coming from JavaScript, this really illustrates how PHP is a server side language, and JavaScript is not :)
Spookster
07-28-2002, 03:23 AM
Yes that is a useful feature being able to just intermix php and html in that manner. I use that technique in both PHP and JSP.
firepages
07-28-2002, 09:50 AM
A common mistake (especially with those with a PERL background) is to echo() or print() everything.
PHP parses pages differently to some other languages in that it does not jump in and out of 'parser' mode when parsing a file so...
<?
echo "<html>";
echo "<body>";
echo $php_var;
echo "more_html";
echo $another_var;
echo "</body>";
echo "</html>";
?>
is SLOWER than
<html>
<body>
<?echo $php_var;?>
..some html
<?echo $another_var;?>
</body>
</html>
as PHP ignores anything that is not enclosed in PHP tags and parses the bits it finds.
another excellent saver is
<?readfile('some_file.txt);?>
which passes the contents of some_file.txt directly to the browser so its a bit like using include() only a little faster (good for big blocks of text/html)
other lifesaver that most PHPers find out after hours of string_replace()ing etc , is
echo nl2br($string);
which takes text from input with newlines (say a textarea) and tags a <br /> onto the end for you (XHTML compliant as well!) - the newline is not replaced so your code still looks neat when you view source etc , &
trim($string)
which cleans up whitespace and newlines especially good for password input etc
just thought I would pop those out now as they are perhaps where I wasted the most time when I started with PHP ... anyone else with any lifesavers ??
htmlentities($string, ENT_QUOTES)
cleans out all the html tags so people can't mess up your hard work by posting something like <table><tr> :thumbsup:
Jeewhizz
07-30-2002, 09:54 PM
Also, this will work... (nabbed some example html from this page :))
<?
//php code here
echo<<<code;
<td width="50%" height="50"><b><font color="#FFFFFF"
face="Arial"> </font><font size="5" face="Arial"
color="#DEDFDF">CodingForums.com</font></b><font color="#FFFFFF" face="Arial"><font face="Tahoma" size="5"><b><br>
code;
?>
I.e. no commenting needed :)
Jee
Spookster
07-30-2002, 10:22 PM
Originally posted by Jeewhizz
Also, this will work... (nabbed some example html from this page :))
<?
//php code here
echo<<<code;
<td width="50%" height="50"><b><font color="#FFFFFF"
face="Arial"> </font><font size="5" face="Arial"
color="#DEDFDF">CodingForums.com</font></b><font color="#FFFFFF" face="Arial"><font face="Tahoma" size="5"><b><br>
code;
?>
I.e. no commenting needed :)
Jee
Yes that is heredoc syntax. Available since php4. You can also store it in a variable for echoing at a later time as well. I used it in a reply for someone else here
http://www.codingforums.com/showthread.php?s=&threadid=2276