PDA

View Full Version : What is the best "method" to code PHP.



Agnostic Bear
24-01-2007, 01:41 PM
Righty, say you have a code, which would you say is the cleanest coded, which one would you say is the best to look at, and which would / do you code like.

Example 1 (My style of coding):


<?php
if(isset($_POST["username"]) && isset($_POST["password"]))
{
if($_POST["username"] == "")
{
do this
}
else
{
do this
}
}
?>


Example 2


<?php
if(isset($_POST[username]) && isset($_POST[password]))
{if($_POST[username] == "")
{do this}
else
{do this}}
?>


Example 3


<?php
if(isset($_POST['username']) && isset($_POST['password']))
{
if($_POST['username'] == "")
{ do this
}
else
{ do this
}
}
?>

?php?
24-01-2007, 01:45 PM
I'd say the first method as it looks better cleaner, and the rest look confusing, although it's the same code.

Heinous
25-01-2007, 06:08 AM
1, by far.

Then use OOP.

nets
30-01-2007, 11:58 PM
if(isset($_POST['u'], $_POST['p']) && !empty($_POST['u'])) foo();
else bar();

Sam
31-01-2007, 01:14 AM
Josh!

:o

<3 Long time no see.

Agnostic Bear
31-01-2007, 12:05 PM
if(isset($_POST['u'], $_POST['p']) && !empty($_POST['u'])) foo();
else bar();

"eww"
6ewws

ZAG
31-01-2007, 05:21 PM
The first one, its cleaner and more organised.

Mentor
31-01-2007, 09:04 PM
1, by far.

Then use OOP.
I can agree with pretty much all of this, although unluckily full oop capability's have only come around in php5, so your somewhat limited in 4 and below "/

nets: That doesn't really look cleaner, its more efficient, but not as obvious or clear as the previous two.
Plus i belive php may take issue with it, since your if is on one line, and the else is not, hence php may(in my recent experience (not actually in php though)) treats the else as part of a different statement, so youd need the else on the line with the if in order for your method to work, would you not?

nets
31-01-2007, 09:18 PM
I can agree with pretty much all of this, although unluckily full oop capability's have only come around in php5, so your somewhat limited in 4 and below "/

nets: That doesn't really look cleaner, its more efficient, but not as obvious or clear as the previous two.
Plus i belive php may take issue with it, since your if is on one line, and the else is not, hence php may(in my recent experience (not actually in php though)) treats the else as part of a different statement, so youd need the else on the line with the if in order for your method to work, would you not?
It shouldn't be a problem, although if you've got multiple statements you'd need to encapsulate the code within curly braces. I believe you'd get an error if you used the OR operator after a semi-colon, however.


$foo = $bar OR exit;
$foo = $bar; OR exit; //error

Want to hide these adverts? Register an account for free!