RES: foreach

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



There is some cases that more code (and more cycles) is a good thing. For
example, a multi-layer architecture (like presentation, business and data
access) is more cpu-intensive than a single page doing everything in an
entangled procedural style, but is far more easy to evolve!

As Steven said, you need to use the right tool for the job. If you're going
to write some cryptographic API for mission-critical applications, or a
network protocol for games with extreme bandwidth demands, every ">="
matters, otherwise, better to stick with readability and separation of
concerns.

Cheers,
Samuel.

-----Mensagem original-----
De: Steven Staples [mailto:sstaples@xxxxxxxx] 
Enviada em: terça-feira, 16 de outubro de 2012 10:46
Para: 'David McGlone'; 'Bastien'; 'PHP-GENERAL'
Assunto: RE:  foreach

> Here's what I ended up with after you gave me the advise:
> $result = mysql_query("SELECT * FROM items");
>   $rows = array();
>   while($row = mysql_fetch_array($result))
>    $rows[] = $row;
>   foreach($rows as $row){
>   $product = $row['product'];
>   $price = $row['price'];
>     echo "$product ";
>     echo "$price ";
> 
> 
>     $justright = 0;
>     $toohigh = 5; //I was going to use this to check if the price was 
> too high
> 				just so I could use an elseif in the
exercise, but
> I realized that
> 				it would only work if the if() evaluated to
false,
> which would be
> 				impossible. Ahhh pizz on it, it was fun
anyway! :-
> )
> 
>     if($justright <= $price){
>      echo "Not bad. I'll buy it.<br />";
>       } else
>      echo "Too expensive I'm going home LOL ";
> 
>   }
> 
> It's a dumb script that makes no sense but I had a blast doing this. 
> When things start coming together like this, it gets so gratifying. 
> :-)
> 
>  --
> David M.


David, 

Just putting this out there, but the use of a foreach() loop here, is
redundant...
You are putting your query results into an array, and then looping through
them after with the foreach(), instead of just using the while loop to loop
through them initially... so you're doing the same thing, twice, just using
the foreach() after the while.

One thing I was always told when I was learning c++ (my teacher was anal,
and always forced us to try and be more efficient), using >= uses more cpu
cycles, than > or <, so when you're checking 0 <= 0.1 true, false, you could
exchange your check to be 0 > 0.1 false, else true...

Also, setting the variable $product and $price, with the value from the
database $row['product'], would be less cycles than to just echo the
$row['product']...

That is just some $0.02...  I wouldn't personally create more code, just to
"try" something out... use the right function for the job, write less code
than needed (sometimes, a little more code for readability is better
though), and most importantly... have fun.

One thing I do, is my coding and bracing style is something that Tedd
Sperling doesn't like (there have been many discussions about bracing
styles),  I keep my braces all in line, and always use them in my if()s...
ie:
	If($yourmom == $hot)
	{
		Echo "MILF!";
	}
	Else
	{
		Echo "Pass.";
	}

And I do this for readability, so I can see if I forgot a brace somewhere,
and also, I always know that there are braces (with a 4space indentation, or
tab stops set at 4 space)

1 more point, doing multiline comments, use /*  insert comment here */  and
not just //, and with that, I use inline comments with #,  but that is just
me.

Overall though, I am glad to see you're learning, and having fun doing so...


Steve Staples.


--
PHP General Mailing List (http://www.php.net/) To unsubscribe, visit:
http://www.php.net/unsub.php



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux