[PHP] Problem with loose typing

2004-12-17 Thread Chris Boget
Consider the following:

  function test() {
static $i = 0;

$i++;
$retval = ( $i = 10 ) ? $i : '';

return $retval;

  }
  while( $bob = test()) {
echo $bob . 'br';

  }

You would expect the while loop to go on forever just looking
at the above code.  However, what's happening is that when the
empty string is getting returned due to $i being  10, the while
loop is resolving FALSE when $bob is set to the value of the
empty string.
Is there any way that I can force this not to happen?  That the
while loop resolves as FALSE only when the function actually
returns a (boolean) FALSE value?

thnx,
Chris

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



RE: [PHP] Problem with loose typing

2004-12-17 Thread Michael Sims
Chris Boget wrote:
   function test() {
 static $i = 0;
 
 $i++;
 $retval = ( $i = 10 ) ? $i : '';
 
 return $retval;
 
   }
   while( $bob = test()) {
 echo $bob . 'br';
 
   }
 
 You would expect the while loop to go on forever just looking
 at the above code. 

I wouldn't, but apparently you do. :)

 However, what's happening is that when the
 empty string is getting returned due to $i being  10, the while
 loop is resolving FALSE when $bob is set to the value of the
 empty string.
 Is there any way that I can force this not to happen?  That the
 while loop resolves as FALSE only when the function actually
 returns a (boolean) FALSE value?

while ( ($bob = test()) !== false ) {
  echo $bob . 'br';
}

should do the trick.

HTH

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



Re: [PHP] Problem with loose typing

2004-12-17 Thread Jason Wong
On Saturday 18 December 2004 00:02, Chris Boget wrote:

 Is there any way that I can force this not to happen?  That the
 while loop resolves as FALSE only when the function actually
 returns a (boolean) FALSE value?

Make the test more explicit:

 while (TRUE === ($bob = test())) { ... }

-- 
Jason Wong - Gremlins Associates - www.gremlins.biz
Open Source Software Systems Integrators
* Web Design  Hosting * Internet  Intranet Applications Development *
--
Search the list archives before you post
http://marc.theaimsgroup.com/?l=php-general
--
/*
It is very difficult to prophesy, especially when it pertains to the future.
*/

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



Re: [PHP] Problem with loose typing

2004-12-17 Thread Matt M.
 You would expect the while loop to go on forever just looking
 at the above code.  However, what's happening is that when the
 empty string is getting returned due to $i being  10, the while
 loop is resolving FALSE when $bob is set to the value of the
 empty string.
 Is there any way that I can force this not to happen?  That the
 while loop resolves as FALSE only when the function actually
 returns a (boolean) FALSE value?

I think this should do it.

  function test() {
   static $i = 0;

   $i++;
   $retval = ( $i = 10 ) ? $i : '';

   return $retval;

 }
 while( ($bob = test()) !== false ) {
   echo $bob . 'ECHObr';

 }

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



Re: [PHP] Problem with loose typing

2004-12-17 Thread Richard Lynch
Chris Boget wrote:
 Consider the following:

   function test() {
 static $i = 0;

 $i++;
 $retval = ( $i = 10 ) ? $i : '';

 return $retval;

   }
   while( $bob = test()) {
 echo $bob . 'br';

   }

 You would expect the while loop to go on forever just looking
 at the above code.  However, what's happening is that when the
 empty string is getting returned due to $i being  10, the while
 loop is resolving FALSE when $bob is set to the value of the
 empty string.
 Is there any way that I can force this not to happen?  That the
 while loop resolves as FALSE only when the function actually
 returns a (boolean) FALSE value?

The easiest thing is to change the '' in your ternary operator to TRUE.

TRUE ain't never gonna evaluate to FALSE in PHP.  Well, I guess you could
hack the source to make that happen, if you were bored enough...

Another option is to use === to test $bob explicitly against '' and/or
FALSE so that PHP's loose type conversion doesn't confuse you.

You could even change '' to 'LOTS AND LOTS' so that after 10, you see
'LOTS AND LOTS'

Or, if you were wanting this to actually be useful, you could do something
like:

$retval = ($i = 10) ? $i : ('~10^' . log($i, 10));

For the English majors, I imagine there's a http://php.net/printf input
that would let you get things like 'hundreds', 'thousands', 'millions',
etc.

-- 
Like Music?
http://l-i-e.com/artists.htm

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