Question on style

This is a discussion on Question on style within the PHP Language forums, part of the PHP Programming Forums category; I come from a corporate C background and I find style to be important in readability. Especially when multiple people ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-07-2004
Mark C
 
Posts: n/a
Default Question on style

I come from a corporate C background and I find style to be important in
readability. Especially when multiple people are working on the same code.
I have seen 3 way to deal with large bocks of static HTML code in PHP. What
are your preferences? I have listed the techniques in the order of my
preference.

Hope I don't start an argument ;)

Mark C


Method 1:

switch ($_POST[Mode])
{
case "":
?>
<br><span class='instruct'>Select the area you need to administer.</span>
<form action="Admin.php" method="post">
<input type="submit" name="Mode" value="User">
</form>
<form action="Admin.php" method="post">
<input type="submit" name="Mode" value="Church">
</form>
<form action="Admin.php" method="post">
<input type="submit" name="Mode" value="Ministry">
</form>
<?php
break;

case "User":
$hits = GetEMailList($mn[MinistryID]);

================================================== ====

Method 2:

switch ($_POST[Mode])
{
case "":

print "<br><span class='instruct'>Select the area you need to
administer.</span>";
print "<form action=\"Admin.php\" method=\"post\">";
print " <input type=\"submit\" name=\"Mode\" value=\"User\">";
print "</form>";
print "<form action=\"Admin.php" method="post">";
print " <input type=\"submit" name="Mode" value="Church">";
print "</form>";
print "<form action=\"Admin.php\" method=\"post\">";
print " <input type=\"submit\" name=\"Mode\" value=\"Ministry\">";
print "</form>";
break;

case "User":
$hits = GetEMailList($mn[MinistryID]);

================================================== ====

Method 3:

switch ($_POST[Mode])
{
case "":

$Var = <<<EOQ
<br><span class='instruct'>Select the area you need to administer.</span>
<form action="Admin.php" method="post">
<input type="submit" name="Mode" value="User">
</form>
<form action="Admin.php" method="post">
<input type="submit" name="Mode" value="Church">
</form>
<form action="Admin.php" method="post">
<input type="submit" name="Mode" value="Ministry">
</form>
EOQ;

print $Var;
break;

case "User":
$hits = GetEMailList($mn[MinistryID]);


Reply With Quote
  #2 (permalink)  
Old 03-07-2004
Swartz
 
Posts: n/a
Default Re: Question on style

Hi Mark.

> Method 1:
> switch ($_POST[Mode])
> {
> case "":
> ?>
> <br><span class='instruct'>Select the area you need to

administer.</span>
<snip...>
> </form>
> <?php
> break;
>
> case "User":
> $hits = GetEMailList($mn[MinistryID]);
>



I'd go with method 1. Main reason for choosing is that its far more
effecient. Not having to echo/print statid HTML can save ya a lot of CPU
time.

Just my $0.02...

--
Swartz



Reply With Quote
  #3 (permalink)  
Old 03-07-2004
Nikolai Chuvakhin
 
Posts: n/a
Default Re: Question on style

"Mark C" <nospam@today.com> wrote in message
news:<104knpthdotn153@corp.supernews.com>...
>
> I come from a corporate C background


Then you probably understand the difference between a constant (Mode)
and a literal ('Mode') and should stop using $_POST[Mode] and switch
to $_POST['Mode'].

> and I find style to be important in readability.


Unfortunately, readable code is not necessarily the best-performing
code...

> I have seen 3 way to deal with large bocks of static HTML code in PHP.
> What are your preferences?


None of the three you suggest. It is usually considered a good idea
to separate logic from content. So my preference would be to define
the long block of static HTML elsewhere and display it when needed
via include() or even readfile(). But then, again, that would be
my personal preference, stemming from aesthetics, not performance
testing. So here goes:

Method 4:

switch ($_POST['Mode']) {
case '':
include ('form.php'); // the idea being, HTML code for the form
echo $form; // is defined as variable $form in form.php
break;
case 'User':
$hits = GetEMailList($mn['MinistryID']);
}

Method 5:

switch ($_POST['Mode']) {
case '':
readfile ('form.htm'); // the idea being, HTML code for the form
// is contained in file form.htm
break;
case 'User':
$hits = GetEMailList($mn['MinistryID']);
}

It is also usually considered a good idea to conceal form.* file
by putting it into a directory outside the document root or into
a restricted-access directory protected with .htaccess.

Cheers,
NC
Reply With Quote
  #4 (permalink)  
Old 03-07-2004
Hayden Kirk
 
Posts: n/a
Default Re: Question on style

Yes...

I have been using method 1 for along time and I dont like it at all. Its a
pig of a way to do things. Including the html is much better. You can store
"hook" in the html so it can be a bit dynamic. EG <title>%TITLE%</title>.

I find this is alot better to work with, you can edit the html in an editor
like Dreamweaver and use PHPed or something similer to edit the php...

I also use a class called page that renders a html page... maybe something
else to think about?

Just my 0.02c :P

"Nikolai Chuvakhin" <nc@iname.com> wrote in message
news:32d7a63c.0403061823.2b4d7dbe@posting.google.c om...
> "Mark C" <nospam@today.com> wrote in message
> news:<104knpthdotn153@corp.supernews.com>...
> >
> > I come from a corporate C background

>
> Then you probably understand the difference between a constant (Mode)
> and a literal ('Mode') and should stop using $_POST[Mode] and switch
> to $_POST['Mode'].
>
> > and I find style to be important in readability.

>
> Unfortunately, readable code is not necessarily the best-performing
> code...
>
> > I have seen 3 way to deal with large bocks of static HTML code in PHP.
> > What are your preferences?

>
> None of the three you suggest. It is usually considered a good idea
> to separate logic from content. So my preference would be to define
> the long block of static HTML elsewhere and display it when needed
> via include() or even readfile(). But then, again, that would be
> my personal preference, stemming from aesthetics, not performance
> testing. So here goes:
>
> Method 4:
>
> switch ($_POST['Mode']) {
> case '':
> include ('form.php'); // the idea being, HTML code for the form
> echo $form; // is defined as variable $form in form.php
> break;
> case 'User':
> $hits = GetEMailList($mn['MinistryID']);
> }
>
> Method 5:
>
> switch ($_POST['Mode']) {
> case '':
> readfile ('form.htm'); // the idea being, HTML code for the form
> // is contained in file form.htm
> break;
> case 'User':
> $hits = GetEMailList($mn['MinistryID']);
> }
>
> It is also usually considered a good idea to conceal form.* file
> by putting it into a directory outside the document root or into
> a restricted-access directory protected with .htaccess.
>
> Cheers,
> NC



Reply With Quote
  #5 (permalink)  
Old 03-07-2004
Mark C
 
Posts: n/a
Default Re: Question on style


"Nikolai Chuvakhin" <nc@iname.com> wrote in message
news:32d7a63c.0403061823.2b4d7dbe@posting.google.c om...
> "Mark C" <nospam@today.com> wrote in message
> news:<104knpthdotn153@corp.supernews.com>...
> >
> > I come from a corporate C background

>
> Then you probably understand the difference between a constant (Mode)
> and a literal ('Mode') and should stop using $_POST[Mode] and switch
> to $_POST['Mode'].


I had seen the different syntax, but none of the texts I have indicated the
difference. Sinice it worked either way I assumed there was no difference.
But as I think about all the thing you can put in the [] I see how the ' can
help the interpterter.


>
> > I have seen 3 way to deal with large bocks of static HTML code in PHP.
> > What are your preferences?

>
> None of the three you suggest. It is usually considered a good idea
> to separate logic from content. So my preference would be to define
> the long block of static HTML elsewhere and display it when needed
> via include() or even readfile(). But then, again, that would be
> my personal preference, stemming from aesthetics, not performance
> testing. So here goes:
>
> Method 4:
>
> switch ($_POST['Mode']) {
> case '':
> include ('form.php'); // the idea being, HTML code for the form
> echo $form; // is defined as variable $form in form.php
> break;
> case 'User':
> $hits = GetEMailList($mn['MinistryID']);
> }
>
> Method 5:
>
> switch ($_POST['Mode']) {
> case '':
> readfile ('form.htm'); // the idea being, HTML code for the form
> // is contained in file form.htm
> break;
> case 'User':
> $hits = GetEMailList($mn['MinistryID']);
> }


I like both these ideas. I did not think of either right off the top of my
head. I would have to get used to looking to another document for content.
I generaly try to put logic in functions and classes and keep the main page
more output oriatned with calls to libraries for number crunching.

Good discussion,

Mark C


Reply With Quote
  #6 (permalink)  
Old 03-07-2004
CountScubula
 
Posts: n/a
Default Re: Question on style

"Mark C" <nospam@today.com> wrote in message
news:104knpthdotn153@corp.supernews.com...
> I come from a corporate C background and I find style to be important in
> readability. Especially when multiple people are working on the same

code.
> I have seen 3 way to deal with large bocks of static HTML code in PHP.

What
> are your preferences? I have listed the techniques in the order of my
> preference.
>
> Hope I don't start an argument ;)
>
> Mark C
>
>
> Method 1:
>
> switch ($_POST[Mode])
> {
> case "":
> ?>
> <br><span class='instruct'>Select the area you need to

administer.</span>
> <form action="Admin.php" method="post">
> <input type="submit" name="Mode" value="User">
> </form>
> <form action="Admin.php" method="post">
> <input type="submit" name="Mode" value="Church">
> </form>
> <form action="Admin.php" method="post">
> <input type="submit" name="Mode" value="Ministry">
> </form>
> <?php
> break;
>
> case "User":
> $hits = GetEMailList($mn[MinistryID]);
>
> ================================================== ====
>
> Method 2:
>
> switch ($_POST[Mode])
> {
> case "":
>
> print "<br><span class='instruct'>Select the area you need to
> administer.</span>";
> print "<form action=\"Admin.php\" method=\"post\">";
> print " <input type=\"submit\" name=\"Mode\" value=\"User\">";
> print "</form>";
> print "<form action=\"Admin.php" method="post">";
> print " <input type=\"submit" name="Mode" value="Church">";
> print "</form>";
> print "<form action=\"Admin.php\" method=\"post\">";
> print " <input type=\"submit\" name=\"Mode\" value=\"Ministry\">";
> print "</form>";
> break;
>
> case "User":
> $hits = GetEMailList($mn[MinistryID]);
>
> ================================================== ====
>
> Method 3:
>
> switch ($_POST[Mode])
> {
> case "":
>
> $Var = <<<EOQ
> <br><span class='instruct'>Select the area you need to

administer.</span>
> <form action="Admin.php" method="post">
> <input type="submit" name="Mode" value="User">
> </form>
> <form action="Admin.php" method="post">
> <input type="submit" name="Mode" value="Church">
> </form>
> <form action="Admin.php" method="post">
> <input type="submit" name="Mode" value="Ministry">
> </form>
> EOQ;
>
> print $Var;
> break;
>
> case "User":
> $hits = GetEMailList($mn[MinistryID]);
>
>


Well, you know what they say about opionion, they are like... well, lets
just say everyone has one. :)

I can warrent times when you would need to do all the 3 you listed above.
method 1: if you needed to edit the html section in say, dreamweaver.
method 2: if you jsut need to spit out a couple lines
method 3: if the block has vars in it, but I would do print <<< instead of
$var <<<
method 4: method 1, as was stated by someone else, put html in a seperate
file, use to keep site very dynamic

just my 1 cent, need other 1 cent for another post

--
Mike Bradley
http://www.gzentools.com -- free online php tools


Reply With Quote
  #7 (permalink)  
Old 03-07-2004
Pedro Graca
 
Posts: n/a
Default Re: Question on style

Mark C wrote:
>
> "Nikolai Chuvakhin" <nc@iname.com> wrote in message
> news:32d7a63c.0403061823.2b4d7dbe@posting.google.c om...


>> Then you probably understand the difference between a constant (Mode)
>> and a literal ('Mode') and should stop using $_POST[Mode] and switch
>> to $_POST['Mode'].

>
> I had seen the different syntax, but none of the texts I have indicated the
> difference. Sinice it worked either way I assumed there was no difference.
> But as I think about all the thing you can put in the [] I see how the ' can
> help the interpterter.


If you enable all errors, warnings, and notices (as you should, at
least, while developing the script) you'll get a notice for using
undefined constants.

<?php
error_reporting(E_ALL);

define('Mode2', 'something');

echo 'Mode'; // OK
echo Mode; // Notice: Use of undefined constant Mode - assumed 'Mode'

echo 'Mode2'; // OK
echo Mode2; // OK; prints "something"
?>
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 07:21 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0