Re: [PHP] How can an elephant count for nothing?

2009-02-13 Thread Yeti
I guess the main reason for PHP to behave like this is to make life
easier for many everyday situations.

EXAMPLE:
User input via GET or POST - usually string
You compare it to some value - int/string or whatever

So if a user posts '17' (string) and you compare it to 17 (int),
unless you are using ===, PHP won't complain.

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



Re: [PHP] How can an elephant count for nothing?

2009-02-13 Thread Virgilio Quilario
 While PHP has a lot of nice features, it also has some traps which I am 
 forever falling
 into. One which I find particularly hard to understand is how mixed mode 
 comparisons work.
 For instance

 $string =  'elephant';
 If($string == 0) returns true;
 If($string != 0) returns false;
 If($string === 0) returns false;

 I know that in this case I should use 'If($string == '')', but I still manage 
 to forget.
 Can anyone explain clearly why comparing a string with zero gives this 
 apparently
 anomalous result?


when comparing string with integer, the interpreter first converts
string into an integer before comparison takes place.

Virgil
http://www.jampmark.com

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



[PHP] How can an elephant count for nothing?

2009-02-12 Thread Clancy
While PHP has a lot of nice features, it also has some traps which I am forever 
falling
into. One which I find particularly hard to understand is how mixed mode 
comparisons work.
For instance 

$string =  'elephant';
If($string == 0) returns true;
If($string != 0) returns false;
If($string === 0) returns false; 

I know that in this case I should use 'If($string == '')', but I still manage 
to forget.
Can anyone explain clearly why comparing a string with zero gives this 
apparently
anomalous result?

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



Re: [PHP] How can an elephant count for nothing?

2009-02-12 Thread Per Jessen
Clancy wrote:

 While PHP has a lot of nice features, it also has some traps which I
 am forever falling into. One which I find particularly hard to
 understand is how mixed mode comparisons work. For instance
 
 $string =  'elephant';
 If($string == 0) returns true;
 If($string != 0) returns false;
 If($string === 0) returns false;
 
 I know that in this case I should use 'If($string == '')', but I still
 manage to forget. Can anyone explain clearly why comparing a string
 with zero gives this apparently anomalous result?

I'm not certain, but I suspect it's because the interpreter attempts to
convert elephant to an integer first. 


/Per


-- 
Per Jessen, Zürich (-0.6°C)


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



Re: [PHP] How can an elephant count for nothing?

2009-02-12 Thread Jochem Maas
Clancy schreef:
 While PHP has a lot of nice features, it also has some traps which I am 
 forever falling
 into. One which I find particularly hard to understand is how mixed mode 
 comparisons work.
 For instance 
 
 $string =  'elephant';
 If($string == 0) returns true;
 If($string != 0) returns false;
 If($string === 0) returns false; 
 
 I know that in this case I should use 'If($string == '')', but I still manage 
 to forget.
 Can anyone explain clearly why comparing a string with zero gives this 
 apparently
 anomalous result?

it's called auto-casting (or auto-typecasting) and it's 'by design'
... welcome to the world of dynamic typing.

try this to see it working:

php -r '
var_dump((integer)elephant);
var_dump((float)elephant);
var_dump((bool)elephant);
var_dump((array)elephant);
var_dump((object)elephant);
var_dump((bool)(integer)elephant);
'

you can avoid auto-casting if needed, in a variety of ways:

php -r '
$foo = elephant;
if (!empty($foo))
echo $foo found!\n;
if (strlen($foo))
echo $foo found!\n;
if (is_string($foo)  strlen($foo))
echo $foo found!\n;
if ($foo !== )
echo $foo found!\n;
if ($foo === elephant)
echo $foo found!\n;
'

those last 2 show how to use 'type-checked' equality
testing.

 


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



Re: [PHP] How can an elephant count for nothing?

2009-02-12 Thread Yeti
 Can anyone explain clearly why comparing a string
 with zero gives this apparently anomalous result?

?php
$string = 'oleyphoont';
var_dump((int)$string, $string == 0, $string == 1);
?

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



Re: [PHP] How can an elephant count for nothing?

2009-02-12 Thread Dotan Cohen
Have you tried with a mouse?

-- 
Dotan Cohen

http://what-is-what.com
http://gibberish.co.il

א-ב-ג-ד-ה-ו-ז-ח-ט-י-ך-כ-ל-ם-מ-ן-נ-ס-ע-ף-פ-ץ-צ-ק-ר-ש-ת
ا-ب-ت-ث-ج-ح-خ-د-ذ-ر-ز-س-ش-ص-ض-ط-ظ-ع-غ-ف-ق-ك-ل-م-ن-ه‍-و-ي
А-Б-В-Г-Д-Е-Ё-Ж-З-И-Й-К-Л-М-Н-О-П-Р-С-Т-У-Ф-Х-Ц-Ч-Ш-Щ-Ъ-Ы-Ь-Э-Ю-Я
а-б-в-г-д-е-ё-ж-з-и-й-к-л-м-н-о-п-р-с-т-у-ф-х-ц-ч-ш-щ-ъ-ы-ь-э-ю-я
ä-ö-ü-ß-Ä-Ö-Ü


Re: [PHP] How can an elephant count for nothing?

2009-02-12 Thread 惠新宸




Jochem Maas wrote:

  Clancy schreef:
  
  
While PHP has a lot of nice features, it also has some traps which I am forever falling
into. One which I find particularly hard to understand is how mixed mode comparisons work.
For instance 

$string =  'elephant';
If($string == 0) returns true;
If($string != 0) returns false;
If($string === 0) returns false; 

I know that in this case I should use 'If($string == '')', but I still manage to forget.
Can anyone explain clearly why comparing a string with zero gives this apparently
anomalous result?

  
  
it's called auto-casting (or auto-typecasting) and it's 'by design'
... welcome to the world of dynamic typing.

try this to see it working:

php -r '
var_dump((integer)"elephant");
var_dump((float)"elephant");
var_dump((bool)"elephant");
var_dump((array)"elephant");
var_dump((object)"elephant");
var_dump((bool)(integer)"elephant");
'

you can avoid auto-casting if needed, in a variety of ways:

php -r '
$foo = "elephant";
if (!empty($foo))
	echo "$foo found!\n";
if (strlen($foo))
	echo "$foo found!\n";
if (is_string($foo)  strlen($foo))
	echo "$foo found!\n";
if ($foo !== "")
	echo "$foo found!\n";
if ($foo === "elephant")
	echo "$foo found!\n";
'

those last 2 show how to use 'type-checked' equality
testing.

  
  

  

because  intval("elephant") == 0;
intval will convert the string into integer , Strings
will most likely return 0 although this depends on the leftmost
characters of the string.

-- 
Laruence's Signature




惠
新宸 xinchen.hui |  SYS |  (+8610)82602112-7974 | :laruence






Re: [PHP] How can an elephant count for nothing?

2009-02-12 Thread Shawn McKenzie
惠新宸 wrote:
 Jochem Maas wrote:
 Clancy schreef:
   
 While PHP has a lot of nice features, it also has some traps which I am 
 forever falling
 into. One which I find particularly hard to understand is how mixed mode 
 comparisons work.
 For instance 

 $string =  'elephant';
 If($string == 0) returns true;
 If($string != 0) returns false;
 If($string === 0) returns false; 

 I know that in this case I should use 'If($string == '')', but I still 
 manage to forget.
 Can anyone explain clearly why comparing a string with zero gives this 
 apparently
 anomalous result?
 

 it's called auto-casting (or auto-typecasting) and it's 'by design'
 ... welcome to the world of dynamic typing.

 try this to see it working:

 php -r '
 var_dump((integer)elephant);
 var_dump((float)elephant);
 var_dump((bool)elephant);
 var_dump((array)elephant);
 var_dump((object)elephant);
 var_dump((bool)(integer)elephant);
 '

 you can avoid auto-casting if needed, in a variety of ways:

 php -r '
 $foo = elephant;
 if (!empty($foo))
  echo $foo found!\n;
 if (strlen($foo))
  echo $foo found!\n;
 if (is_string($foo)  strlen($foo))
  echo $foo found!\n;
 if ($foo !== )
  echo $foo found!\n;
 if ($foo === elephant)
  echo $foo found!\n;
 '

 those last 2 show how to use 'type-checked' equality
 testing.

   


   
 because  intval(elephant) == 0;
 intval will convert the string into integer , Strings will most likely
 return 0 although this depends on the leftmost characters of the string.
 
 -- 
 
 Baidu惠新宸 xinchen.hui* | * SYS *| * (+8610)82602112-7974 *|* Hi:laruence
 
'2 elephants' != 0

-- 
Thanks!
-Shawn
http://www.spidean.com

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



Re: [PHP] How can an elephant count for nothing?

2009-02-12 Thread Ashley Sheridan
On Thu, 2009-02-12 at 13:12 +0200, Dotan Cohen wrote:
 Have you tried with a mouse?
 
Non-strings equate to a boolean value of 1 when they are converted to a
boolean value automatically (in the case of comparison queries) when
they contain a value. Strings of 0 length are converted to a 0. In
useful terms, 1 is true and 0 is false in the PHP world. As PHP is a
loose-typed language you should really use the === operator if you need
to compare variable mathc and type match.


Ash
www.ashleysheridan.co.uk


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



Re: [PHP] How can an elephant count for nothing?

2009-02-12 Thread Clancy
On Thu, 12 Feb 2009 23:47:31 +0800, huixinc...@baidu.com (???) wrote:

Jochem Maas wrote:
 Clancy schreef:
   
 While PHP has a lot of nice features, it also has some traps which I am 
 forever falling
 into. One which I find particularly hard to understand is how mixed mode 
 comparisons work.
.
 you can avoid auto-casting if needed, in a variety of ways:

 php -r '
 $foo = elephant;
 if (!empty($foo))
  echo $foo found!\n;
 if (strlen($foo))
  echo $foo found!\n;
 if (is_string($foo)  strlen($foo))
  echo $foo found!\n;
 if ($foo !== )
  echo $foo found!\n;
 if ($foo === elephant)
  echo $foo found!\n;
 '

 those last 2 show how to use 'type-checked' equality
 testing.

   
because  intval(elephant) == 0;
intval will convert the string into integer , Strings will most likely 
return 0 although this depends on the leftmost characters of the string.

This seems to be the nearest to the correct answer. In fact it appears that if 
you compare
a string with an integer the effective value of the string is the value of the 
first
character(s), if it/they  are integers, or zero.

elephant == 0; true
an elephant == 0; true
1 elephant == 0; false
0 elephants == 0; true
a herd of elephants == 0; true
7 elephants == 7; true
 elephants == ; true

The next question is ' how is the order of conversion determined?' I thought it 
might have
converted the second element to the same type as the first element, so I 
reversed the
comparison, but I got exactly the same results, so perhaps it converts from the 
more
complex type to the simpler type.

Clearly the lesson is to be learnt is not to compare disparate types, unless 
you really
have to.

One situation where this is unavoidable is if you are searching an arbitrary 
set of
strings for a given word.  In this case it is essential to do the exact 
comparison, or you
will get erroneous results.

Thank you all for your suggestions.

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