Re: while-question

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

 



Nathan Rixham schreef:
> Jochem Maas wrote:
>> $a = range(1,10);
>> for ($i = 0, $c = count($a); $i < $c; print($a[$i]."\n"), $i++);
> 
> think the point of this is to count the items in an array without count
> mate :p no point in the above you could just:
> $c = count($a);

I thought the point was to avoid count() on every iteration.
not using count() but instead looping the complete array is stupid,
if the OP's teacher thinks count() is bad then that teacher suck's,
count() is efficient as it can be ... it does'nt actually do a
count when it's called merely looks up the length property of the
array (unless I'm grossly mistaken)

> 
> foreach!
> $a = range(1,10);
> $c = 0;
> foreach($a as $b) {
>  $c++;
> }
> echo $c. PHP_EOL;
> 
> if you really want a challenge try this one..
> 
> task: add the numbers 5 and 17 together, using php, without using the +
> operator. fill it in:
> 
> function add($a , $b) {
>  //code here but no + - / * operators

I'll assume no post/pre-increment operators either.

>  return $answer;
> }
> 
> echo add(5, 17);

function add($a, $b) {
	$a = array_merge(array_fill(0, $a, 1), array_fill(0, $b, 1));
	return count($a);
}

not very efficient :-) .. probably one of a 1000 ways to fudge it :-)

> 


-- 
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