Question on style – PHP

  php

Q(Question):

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]);

A(Answer):

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

A(Answer):

"Mark C" <no****@today.com> wrote in message
news:<10*************@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

A(Answer):

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 😛

"Nikolai Chuvakhin" <nc@iname.com> wrote in message
news:32**************************@posting.google.c om…

"Mark C" <no****@today.com> wrote in message
news:<10*************@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

A(Answer):

"Nikolai Chuvakhin" <nc@iname.com> wrote in message
news:32**************************@posting.google.c om…

"Mark C" <no****@today.com> wrote in message
news:<10*************@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

A(Answer):

"Mark C" <no****@today.com> wrote in message
news:10*************@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

A(Answer):

Mark C wrote:

"Nikolai Chuvakhin" <nc@iname.com> wrote in message
news:32**************************@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 =–

LEAVE A COMMENT