To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Code Critique

Code Critique Having someone critique your code is always a great way to hone the skills. Stop in and post your code to see what your peers may have done differently.

Reply
 
Thread Tools Rate Thread Display Modes
Old 11-03-2009, 08:48 PM   #1
Toadums
Senior Member
 
Join Date: May 2009
Posts: 165
should i echo all html code?

Hey...just wondering what the norm/preferred method (if there is one) is.

So I'm wondering if I should have all my code look like this:


HTML Code:
<?php
        if($Approved == 0){
	        $approvedCheck = false;
	}
	?>
	<tr>
	<!--the next 8 lines set each record as the while loop passes thru it-->
	<td id="date<?php echo $j;?>"><? echo $Date; ?></td>		
	<td id="date<?php echo $j;?>"><? echo $Hours; ?></td>
	<td id="date<?php echo $j;?>"><? echo $Phase."&nbsp;"; ?></td>
.
.
.
.
.
        </tr>
or if it is better to have it like this where ALL code is wrapped in <?php ?> tags, and all html code is echo'd:

PHP Code:
<?php
        
if($Approved == 0){
            
$approvedCheck = false;
    }

    echo
"<tr>";
    
//the next 8 lines set each record as the while loop passes thru it
    
echo"<td id="date<?php echo $j;?>"><? echo $Date; ?></td>";    
    echo"<td id="date<?php echo $j;?>"><? echo $Hours; ?></td>";
    echo"<td id="date<?php echo $j;?>"><? echo $Phase."&nbsp;"; ?></td>";
.
.
.
.
.
        echo"</tr>";

Or maybe it doesnt make any difference what so ever. I am just wondering which way other people are doing it.

thanks!
Toadums is offline   Reply With Quote
Old 11-03-2009, 10:50 PM   #2
dagon
RTFM it's a way of life.
 
dagon's Avatar
 
Join Date: Nov 2001
Location: N.Z
Posts: 3,086
you may like to consider a template engine. Otherwise the norm is what ever is going to work best for you.

Last edited by dagon; 11-03-2009 at 11:05 PM.
dagon is offline   Reply With Quote
Old 11-03-2009, 11:16 PM   #3
Neville1985
@bored
 
Neville1985's Avatar
 
Join Date: Oct 2009
Location: Wolfcreek
Posts: 58
I find arrays to be a good way of outputting data effectively. Program scripts to format your data to the way you want it then add it to an array in the order of your liking. Also this gives you a dynamic way of outputting allot of data quickly, using multi-dimensional arrays for example.

PHP Code:
$output['data']['title'][] = 'datadatadata';
$output['data']['content'][] = 'moredata';
Then just using a simple display function that you can create to display to the view page. Most times just using a for...each/for/do...while/while/ statements will work too.

I don't know, I just rather this way. As he said, whatever works for you.
Neville1985 is offline   Reply With Quote
Old 11-03-2009, 11:45 PM   #4
Weedpacket
Custom User Title™
 
Weedpacket's Avatar
 
Join Date: Aug 2002
Location: Rapid Offensive Unit "Foreign Object Damage"
Posts: 19,122
Somewhere down the bottom of the pile of components that I've built on top of each other in order to automate the boring repetitive things about coding (like writing screeds of <tags>) there's an XHTML renderer. I presume it's still down there because XHTML is still coming out of it , but I haven't been down there in over a year.
__________________
On two occasions I have been asked [by Members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
Weedpacket is offline   Reply With Quote
Old 11-05-2009, 08:54 AM   #5
Haddman
1 4r3 1337!
 
Join Date: Sep 2007
Location: Iceland
Posts: 36
I would do something like this:
PHP Code:
<?
if($Approved == 0){
   
$approvedCheck = false;
}
?>
<tr>
   <td id="date<?=$j;?>"><?=$Date;?></td>        
   <td id="date<?=$j;?>"><?=$Hours;?></td>
   <td id="date<?=$j;?>"><?=$Phase."&nbsp;";?></td> // Why the "&nbsp;"?
</tr>
<?= is the same as <?php echo

But as dagon said "the norm is what ever is going to work best for you"
Haddman is offline   Reply With Quote
Old 11-05-2009, 10:02 AM   #6
laserlight
PHP Witch
 
laserlight's Avatar
 
Join Date: Apr 2003
Location: Singapore
Posts: 12,388
Quote:
Originally Posted by Haddman
<?= is the same as <?php echo
If short open tags are enabled, and they might not be, especially since they are not the default and can come into conflict with <?xml.
__________________
Use Bazaar for your version control system
Read the PHP Spellbook
Learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 11-11-2009, 10:25 AM   #7
ixalmida
Deviloper
 
Join Date: Jul 2008
Location: Chicago-area
Posts: 289
Just for pure readability, I don't like to jump in and out of PHP much. In many cases, I also find that it reduces the actual volume of code if I just write a block of PHP instead. Take your example and ask yourself which line is shorter:

HTML Code:
<td id="date<?php echo $j;?>"><? echo $Date; ?></td>
-- or --
PHP Code:
echo "<td id='$j'>$Date</td>";
I also take full advantage of the fact that while HTML doesn't care whether you use single or double quotes, they are treated differently by PHP - so you can nest single-quotes in a double-quoted string.

Additional note: you will probably want to add some source code formatting for troubleshooting the HTML, but your PHP line will still be shorter:

PHP Code:
echo "\t<td id='$j'>$Date</td>\n";

Last edited by ixalmida; 11-11-2009 at 10:30 AM. Reason: adding some source formatting
ixalmida is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes 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 Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 11:36 AM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.