Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-28-2002, 03:04 AM   PM User | #1
WA
Administrator


 
Join Date: Mar 2002
Posts: 2,596
Thanks: 2
Thanked 19 Times in 18 Posts
WA will become famous soon enough
Interesting thing I just learned about outputting text

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:

PHP Code:
<?
$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
__________________
- George
- JavaScript Kit- JavaScript tutorials and 400+ scripts!
- JavaScript Reference- JavaScript reference you can relate to.
WA is offline   Reply With Quote
Old 07-28-2002, 03:23 AM   PM User | #2
Spookster
Supreme Overlord


 
Spookster's Avatar
 
Join Date: May 2002
Location: Marion, IA USA
Posts: 6,224
Thanks: 4
Thanked 80 Times in 79 Posts
Spookster will become famous soon enough
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.
__________________
Spookster
CodingForums Supreme Overlord
All Hail Spookster
Who gave you that Ugging infraction? Yeah that's right it was me!
Spookster is offline   Reply With Quote
Old 07-28-2002, 09:50 AM   PM User | #3
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,910
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
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 ??
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is online now   Reply With Quote
Old 07-28-2002, 10:50 AM   PM User | #4
Phip
Registered User

 
Join Date: Jun 2002
Location: Arizona
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
Phip is an unknown quantity at this point
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>
Phip is offline   Reply With Quote
Old 07-30-2002, 09:54 PM   PM User | #5
Jeewhizz
Regular Coder


 
Join Date: May 2002
Location: London, England
Posts: 369
Thanks: 0
Thanked 0 Times in 0 Posts
Jeewhizz is an unknown quantity at this point
Also, this will work... (nabbed some example html from this page )

PHP Code:
<?
//php code here
echo<<<code;
<
td width="50%" height="50"><b><font color="#FFFFFF"
 
face="Arial">&nbsp;</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
__________________
Jeewhizz - MySQL Moderator
http://www.sitehq.co.uk
PHP and MySQL Hosting
Jeewhizz is offline   Reply With Quote
Old 07-30-2002, 10:22 PM   PM User | #6
Spookster
Supreme Overlord


 
Spookster's Avatar
 
Join Date: May 2002
Location: Marion, IA USA
Posts: 6,224
Thanks: 4
Thanked 80 Times in 79 Posts
Spookster will become famous soon enough
Quote:
Originally posted by Jeewhizz
Also, this will work... (nabbed some example html from this page )

PHP Code:
<?
//php code here
echo<<<code;
<
td width="50%" height="50"><b><font color="#FFFFFF"
 
face="Arial">&nbsp;</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/showthre...&threadid=2276
__________________
Spookster
CodingForums Supreme Overlord
All Hail Spookster
Who gave you that Ugging infraction? Yeah that's right it was me!
Spookster is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:53 AM.


Advertisement
Log in to turn off these ads.