nikifi Posted March 17, 2014 Share Posted March 17, 2014 Hello there, first of all I want to say I am a big fan of Larry Ullman's book. I used it to learn introductory PHP and now I am back learning advanced PHP. I am in chapter one working through the task list example. I have come across the following code: while (list($task_id, $parent_id, $task) = mysqli_fetch_array ($r, MYSQLI_NUM)) { echo "<option value=\"$task_id\">$task</option>\n"; $tasks[] = array('task_id' => $task_id, 'parent_id' => $parent_id, 'task' => $task); } My question is - I thought I read somewhere that it is a bad practice to set a variable in a conditional statement, as in the case here where it is setting a list of variables to the values of an array. Is there any basis to this statement? Link to comment Share on other sites More sharing options...
HartleySan Posted March 17, 2014 Share Posted March 17, 2014 Hello, and welcome to the forums. Sometimes, you have to set variables in loops. There's just no other way around it. I suppose it's always a best practice to keep loop computations to a minimum, but I've never heard of your bad practice. Could you please provide a resource? Thanks. Link to comment Share on other sites More sharing options...
nikifi Posted March 17, 2014 Author Share Posted March 17, 2014 Hello there, thanks for the response. I doubt it was even referring to PHP. I don't have a reference, it's just one of those things I remember reading somewhere and it remained in my head. I don't even know if it is from a reputable source, but I figure I'd post the question here and see your point of view. Link to comment Share on other sites More sharing options...
HartleySan Posted March 17, 2014 Share Posted March 17, 2014 Like I said before, my experience has shown that sometimes you just need variables within loops, and that's all there is to it. As such, doing that in your code isn't necessarily bad. Link to comment Share on other sites More sharing options...
Larry Posted March 17, 2014 Share Posted March 17, 2014 Agreeing with what HartleySan said here. And thanks for the nice words on my books! Link to comment Share on other sites More sharing options...
Recommended Posts