[PHP-DB] Advice to PHP beginners

2002-03-27 Thread Adam Royle

Just some advice... You should use a consistent programming style, 
especially with PHP. You can read some guys advice here. 
http://www.phpbuilder.com/columns/tim20010101.php3?page=1

Some of the advantages of having a consistent style, is when you are 
looking for bugs. If you look over your code and see something unusual, 
then you can target that area to see if it is the culprit. If you don't 
have a consistent style, then sometimes that can cause serious 
heartache, as everything will look unusual.

A few issues that trip up most people when beginning to use PHP, is the 
syntax errors. Usually these arise from quote issues, to semi-colon and 
brace issues. A lot of this trouble can be avoided (or easily debugged) 
by simply using tabs to your advantage. Consider the following:

?php if ($condition){ echo correct;} else {
echo 'what';
if (!$condition2){
include 'thing.php';
while (!$dead)
{ if ($jam!= $yes){ $dead = true;
} else{
for ($i=0;$i100;$i++)
{ $thing = processSomething('something', something2);
$string = 'something'.$here.too;
}
?

Technically I *think* this would be syntactically correct, but if I was 
looking for a bug, I would be shot in the foot. A better way to write 
this would be the following:

?php

if ($condition){
echo correct;
} else {
echo what;
if (!$condition2){
include ('thing.php');
while (!$dead){
if ($jam != $yes){
$dead = true;
} else {
for ($i=0;$i100;$i++){
$thing = processSomething(something, 
something2);
$string = something $here too;
}
}
}
}
}

?

So its a couple more lines, but if I came back to that script a month or 
two months later trying to fix something, or add a new feature, it would 
be easy. Couple that style with comments and you're on fire!!!

Hope this helps for someone out there...

Adam


-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP-DB] Re: OT - [PHP-DB] Advice to PHP beginners

2002-03-27 Thread -BD-


did the tabs get stripped in your mail, or is there a reason the code
couldn't be written like the following?
i'm curious, since this is the way i do 90% of my code - makes it easy to
see what's going on... but i dunno about performance or parsing impact
(never gave it much thought until now)...?

newbily yours...

?php

if ($condition){
echo correct;
} else {
echo what;
if (!$condition2){
include ('thing.php');
while (!$dead){
if ($jam != $yes){
$dead = true;
} else {
for ($i=0;$i100;$i++){
$thing = processSomething(something, something2);
$string = something $here too;
}
}
}
}
}

?


- Original Message -
From: Adam Royle [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
Sent: Wednesday, March 27, 2002 4:44 PM
Subject: [PHP-DB] Advice to PHP beginners


 Just some advice... You should use a consistent programming style,
 especially with PHP. You can read some guys advice here.
 http://www.phpbuilder.com/columns/tim20010101.php3?page=1

 Some of the advantages of having a consistent style, is when you are
 looking for bugs. If you look over your code and see something unusual,
 then you can target that area to see if it is the culprit. If you don't
 have a consistent style, then sometimes that can cause serious
 heartache, as everything will look unusual.

 A few issues that trip up most people when beginning to use PHP, is the
 syntax errors. Usually these arise from quote issues, to semi-colon and
 brace issues. A lot of this trouble can be avoided (or easily debugged)
 by simply using tabs to your advantage. Consider the following:

 ?php if ($condition){ echo correct;} else {
 echo 'what';
 if (!$condition2){
 include 'thing.php';
 while (!$dead)
 { if ($jam!= $yes){ $dead = true;
 } else{
 for ($i=0;$i100;$i++)
 { $thing = processSomething('something', something2);
 $string = 'something'.$here.too;
 }
 ?

 Technically I *think* this would be syntactically correct, but if I was
 looking for a bug, I would be shot in the foot. A better way to write
 this would be the following:

 ?php

 if ($condition){
 echo correct;
 } else {
 echo what;
 if (!$condition2){
 include ('thing.php');
 while (!$dead){
 if ($jam != $yes){
 $dead = true;
 } else {
 for ($i=0;$i100;$i++){
 $thing = processSomething(something, something2);
 $string = something $here too;
 }
 }
 }
 }
 }

 ?

 So its a couple more lines, but if I came back to that script a month or
 two months later trying to fix something, or add a new feature, it would
 be easy. Couple that style with comments and you're on fire!!!

 Hope this helps for someone out there...

 Adam





-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php