Better style on coding

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Jaxolotl
Forum Contributor
Posts: 137
Joined: Mon Nov 13, 2006 4:19 am
Location: Argentina and Italy

Better style on coding

Post by Jaxolotl »

Which would be a good Coding style for doing this?

Code: Select all


$query = "SELECT `field` FROM `table`";

/*
here my DB Connection Abstraction that returns
the resource $results
*/
sql_select($query,$results);

/* HERE IS WHAT I MEAN */

while($my_row = mysql_fetch_array($results)){
 // DO SOMETHING
}
I Want to avoid the "Assignment In Condition" Horrible coding thing with something better/cleaner.
Every suggestion will be appreciate!!!!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Assign prior to the loop then assign again during the loop (at the continue point.)
User avatar
Jaxolotl
Forum Contributor
Posts: 137
Joined: Mon Nov 13, 2006 4:19 am
Location: Argentina and Italy

Post by Jaxolotl »

feyd the omnipresent ;)
It's been a long time, I move to Argentina 2 month ago. Now I'm back online.

Can you show me an example of what you mean?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

viewtopic.php?p=382263#382263 is the most recent example.
User avatar
Jaxolotl
Forum Contributor
Posts: 137
Joined: Mon Nov 13, 2006 4:19 am
Location: Argentina and Italy

Post by Jaxolotl »

thanks I'll take my time to understand the inner logic of this code style and if I have some questions or deductions I'll put them here.

Should I be so picky when coding? I think I should if i want to be better...isn't it?

Many thanks for your fast "response".
User avatar
Jaxolotl
Forum Contributor
Posts: 137
Joined: Mon Nov 13, 2006 4:19 am
Location: Argentina and Italy

Post by Jaxolotl »

Sorry about the "tnx" staff.

Is my conclusion correct?

Code: Select all

$query = "SELECT `field` FROM `table`";

sql_select($query,$results);

// This fetch the first data of the resourse into an array


while($my_row){ 
 // DO SOMETHING

// once the first data is used it gets the next one if available so it would be at the end of the cycle
$my_row = mysql_fetch_array($results);
} 
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You need one more call to mysql_fetch_array() prior to the loop starting or $my_row will not exist.
User avatar
Jaxolotl
Forum Contributor
Posts: 137
Joined: Mon Nov 13, 2006 4:19 am
Location: Argentina and Italy

Post by Jaxolotl »

Oops I'm tired today, I made the mistake when posting. (cut instead of copy). I'd better go home ;)
Thanks again feyd

Code: Select all

$query = "SELECT `field` FROM `table`";

sql_select($query,$results);

// This fetch the first data of the resourse into an array
$my_row = mysql_fetch_array($results);

while($my_row){ 
 // DO SOMETHING

// once the first data is used it gets the next one if available so it MUST be at the end of the cycle
$my_row = mysql_fetch_array($results);
} 
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Why are you not into assignment during the conditional?
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Re: Better style on coding

Post by stereofrog »

Jaxolotl wrote:Which would be a good Coding style for doing this?

Code: Select all


/* HERE IS WHAT I MEAN */

while($my_row = mysql_fetch_array($results)){
 // DO SOMETHING
}
Just out of curiosity, what exactly is wrong about "while($row = mysql_fetch_array"?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

I'll third that. There are special features in PHP specifically for making assignments in conditions more practical. Assignments in condition generally improve readability as long as they remain simple. They also reduce duplication...in short they are good practise, like I said, as long as they are simple

Code: Select all

while ($row = fetch())
if fine but

Code: Select all

while (($i = fetch()) > 5 && $i < 10)
probably isn't so great.
User avatar
Jaxolotl
Forum Contributor
Posts: 137
Joined: Mon Nov 13, 2006 4:19 am
Location: Argentina and Italy

Post by Jaxolotl »

Everah wrote:Why are you not into assignment during the conditional?
I was testing some scripts on an agency and the develope under Zend Studio so whe I debug or analyze the script the advise "Assignment In Condition" blabla comes out. I agree that readibility is better using assignment with condition and I still havent decide if I want to change my scripting method but it makes me curious about what advantages would give me the alternative method.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

First off, I wouldn't rely on the Zend Studio debugger messages to that extent. If you comment your code, any decent developer would have no problem understanding what you are doing when assigning during the condition check. I do it all the time and it makes perfect sense to me.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Yeah and Zend Studio code Analyser also grumbles about these

Code: Select all

foreach (array('foo', 'bar', 'zim', 'gir') as $critter) {
when there's absolutely nothing wrong with an anonymous foreach.
User avatar
Jaxolotl
Forum Contributor
Posts: 137
Joined: Mon Nov 13, 2006 4:19 am
Location: Argentina and Italy

Post by Jaxolotl »

Everah wrote:First off, I wouldn't rely on the Zend Studio debugger messages to that extent. If you comment your code, any decent developer would have no problem understanding what you are doing when assigning during the condition check. I do it all the time and it makes perfect sense to me.
I agree about that ;) but sometimes I have to work with different teams and just have to adapt to the environment and development standards they use (even though if I think they took ....call them "funny" desitions).
Can any of you suggest me a good debugging environment? I know is up to your development target and customs but I'm always testing new ways of work (on non working hours)

I'm developing under Apache 1.3.33, PHP 4.3.10 everithing running on Windows (got to move quickly to Linux but I work many hours on Win graph applications)
Last edited by Jaxolotl on Mon May 21, 2007 11:25 am, edited 1 time in total.
Post Reply