Page 2 of 2

Posted: Wed Oct 22, 2003 3:57 pm
by Cruzado_Mainfrm
i prefer this way:

Code: Select all

<?php
for ($i=3;$i<12;$i++) {
     echo $i;
     echo "this might or not be a long line but when things go"
            ." crazy you might want to cut that so u can read"
            ." and you will not get sore eyes";
     print_r ($i);
     //for insert queries that have indexes:
     //table:  id-firstname-lastname
     $query = "INSERT INTO table (firstname,lastname) VALUES ("Johnny","Blaer")"; //see? you don;t need to specify the 'id' field, that's redundant, cuz mySQL does that for you anyways!
     mysql_query($query,$somelink);
     include "WhyUseParenthesesIfTheyAreNotNeededAnyways.php";
}
?>

Posted: Wed Oct 22, 2003 4:36 pm
by jason
I prefer this method:

Code: Select all

<?php

function name ()
{
	while (false) {
		do_something();
	}
}

?>
A consistant style that makes thing easier to read in general.

You may be wondering why my function { is on it's own line, and my while { is not. This is for practical purposes, and to signify that function declarations and block elements are different.

function name () {} doesn't do anything.

Really, it doesn't. It's there simply the define a function. However, a while () {} or if () {} does do something.

The reason I indent and setup the way I do is primarily because of this:

Consider a block of code that is filtered like such:

Code: Select all

1. ###############
2. ###############
3. ###############
4.     ###############
5.     ###############
6. ###############
Now, where does the block start? If I was putting my { on it's own line, then the block would start on row 2. Why is this important? Because when you are looking through a large amount of code, you are usually not reading the code, but rather, following it's paths.

Basically, you should read "Code Complete" for my reasons for structuring my code the way I do.

However, whatever method you choose, consistancy is the key.

Posted: Wed Oct 22, 2003 4:54 pm
by d3ad1ysp0rk
I use a mix sometimes, because I was taught to write like:

Code: Select all

<?PHP 
if($thing=="seven"){
echo "guess what? thing = seven!!";
}
?>
but i personally like reading code like this:

Code: Select all

<?PHP 
if ($thing == "seven")
{
  echo "thing = seven!!";
}
?>
but sometimes i forget to write like that.. so i end of going back and adding line breaks :P

Posted: Wed Oct 22, 2003 10:13 pm
by fractalvibes
Either style works fine. No matter the style, being consistant will probably save some time and effort in the long run and enable you to catch some
oops much easier! The same applies for variable naming, sqlstrings, etc.etc. etc.

fv