Page 1 of 2
Better style on coding
Posted: Fri May 18, 2007 4:29 pm
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!!!!
Posted: Fri May 18, 2007 4:31 pm
by feyd
Assign prior to the loop then assign again during the loop (at the continue point.)
Posted: Fri May 18, 2007 4:37 pm
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?
Posted: Fri May 18, 2007 4:40 pm
by feyd
Posted: Fri May 18, 2007 4:47 pm
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".
Posted: Fri May 18, 2007 5:13 pm
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);
}
Posted: Fri May 18, 2007 5:15 pm
by feyd
You need one more call to mysql_fetch_array() prior to the loop starting or $my_row will not exist.
Posted: Fri May 18, 2007 5:18 pm
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);
}
Posted: Fri May 18, 2007 6:09 pm
by RobertGonzalez
Why are you not into assignment during the conditional?
Re: Better style on coding
Posted: Fri May 18, 2007 6:10 pm
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"?
Posted: Fri May 18, 2007 6:46 pm
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
if fine but
Code: Select all
while (($i = fetch()) > 5 && $i < 10)
probably isn't so great.
Posted: Sun May 20, 2007 5:33 pm
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.
Posted: Mon May 21, 2007 10:31 am
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.
Posted: Mon May 21, 2007 10:46 am
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.
Posted: Mon May 21, 2007 11:24 am
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)