Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #4088 > unrolled thread

empty - not empty()

Started bybill <nobody@spamcop.net>
First post2011-12-19 10:09 -0500
Last post2011-12-20 18:44 -0500
Articles 6 — 4 participants

Back to article view | Back to comp.lang.php


Contents

  empty - not empty() bill <nobody@spamcop.net> - 2011-12-19 10:09 -0500
    Re: empty - not empty() "J.O. Aho" <user@example.net> - 2011-12-19 18:48 +0100
      Re: empty - not empty() Chuck Anderson <cycletourist@invalid.invalid> - 2011-12-19 18:14 -0700
        Re: empty - not empty() bill <nobody@spamcop.net> - 2011-12-20 08:14 -0500
          Re: empty - not empty() "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2011-12-20 17:40 +0100
            Re: empty - not empty() bill <nobody@spamcop.net> - 2011-12-20 18:44 -0500

#4088 — empty - not empty()

Frombill <nobody@spamcop.net>
Date2011-12-19 10:09 -0500
Subjectempty - not empty()
Message-ID<iNGdnbO2--gpznLTnZ2dnUVZ_hCdnZ2d@cablespeedmi.com>
searching the manual gives lots of hits, none of which appear 
relevant (at least by the title).

I had the following code in a scheduling class:

if ($ptNum == "empty") {
	$ptNum = 0;
	$status= "e";
	}

I pass the string "empty" to the function as a patient number if 
we are creating an empty appointment slot.

now, this fails because with a patient number of 0 the if returns 
true.

I am aware of the function empty() that would work like that, but 
do not understand why the compare fails with the constant, "empty."

I fixed the function by making it a strict compare, but would 
like to know why the regular compare is true with the numeric 
value of zero.

bill

[toc] | [next] | [standalone]


#4089

From"J.O. Aho" <user@example.net>
Date2011-12-19 18:48 +0100
Message-ID<9l9bo5FelbU1@mid.individual.net>
In reply to#4088
bill wrote:
> searching the manual gives lots of hits, none of which appear relevant (at
> least by the title).
>
> I had the following code in a scheduling class:
>
> if ($ptNum == "empty") {
> $ptNum = 0;
> $status= "e";
> }
>
> I pass the string "empty" to the function as a patient number if we are
> creating an empty appointment slot.
>
> now, this fails because with a patient number of 0 the if returns true.
>
> I am aware of the function empty() that would work like that, but do not
> understand why the compare fails with the constant, "empty."

I assume you are comparing 0 and not the string "0", this makes you are 
comparing integer with string, there ain't any type conversion done.
The zero is always equal to a string, there is a slightly more information at

http://php.net/manual/en/language.operators.comparison.php


> I fixed the function by making it a strict compare, but would like to know why
> the regular compare is true with the numeric value of zero.

I would say it's not the right way to compare, use strcmp, is_numeric and so 
on, to make a proper compare instead of using the comparison operator.


-- 

   //Aho

[toc] | [prev] | [next] | [standalone]


#4101

FromChuck Anderson <cycletourist@invalid.invalid>
Date2011-12-19 18:14 -0700
Message-ID<jconi9$79i$1@dont-email.me>
In reply to#4089
J.O. Aho wrote:
> bill wrote:
>> searching the manual gives lots of hits, none of which appear 
>> relevant (at
>> least by the title).
>>
>> I had the following code in a scheduling class:
>>
>> if ($ptNum == "empty") {
>> $ptNum = 0;
>> $status= "e";
>> }
>>
>> I pass the string "empty" to the function as a patient number if we are
>> creating an empty appointment slot.
>>
>> now, this fails because with a patient number of 0 the if returns true.
>>
>> I am aware of the function empty() that would work like that, but do not
>> understand why the compare fails with the constant, "empty."
>
> I assume you are comparing 0 and not the string "0", this makes you 
> are comparing integer with string, there ain't any type conversion done.

Actually, it is because there *is* an automatic type conversion done 
when comparing a string to an int.

"If you compare a number with a string or the comparison involves 
numerical strings, then each string is converted to a number and the 
comparison performed numerically."

... http://us.php.net/manual/en/language.operators.comparison.php

> The zero is always equal to a string, 

Not true. 

"If the string starts with valid numeric data, this will be the value 
used. Otherwise, the value will be 0 (zero)."

.... 
http://us.php.net/manual/en/language.types.string.php#language.types.string.conversion

> there is a slightly more information at
>
> http://php.net/manual/en/language.operators.comparison.php

You might want to take a closer look at that page.

The literal string "empty" does not begin with a numerical character so 
it will be converted to the integer 0.

>> the regular compare is true with the numeric value of zero.
> I fixed the function by making it a strict compare, but would like to 
> know why
>
> I would say it's not the right way to compare, use strcmp, is_numeric 
> and so on, to make a proper compare instead of using the comparison 
> operator.
>

I think you have that backwards.  The identical (strict) comparison is 
far more efficient than a function call and this it's intended usage.  
"===" compares value and type (so no type conversion).  Hence the string 
"empty" does not equal (===) the integer 0.

-- 
*****************************
 Chuck Anderson • Boulder, CO
  http://cycletourist.com
 Turn Off, Tune Out, Drop In
*****************************

[toc] | [prev] | [next] | [standalone]


#4107

Frombill <nobody@spamcop.net>
Date2011-12-20 08:14 -0500
Message-ID<Mpudncv3fKaxF23TnZ2dnUVZ_o6dnZ2d@cablespeedmi.com>
In reply to#4101
On 12/19/2011 8:14 PM, Chuck Anderson wrote:
> J.O. Aho wrote:
>> bill wrote:
>>> searching the manual gives lots of hits, none of which appear
>>> relevant (at
>>> least by the title).
>>>
>>> I had the following code in a scheduling class:
>>>
>>> if ($ptNum == "empty") {
>>> $ptNum = 0;
>>> $status= "e";
>>> }
>>>
>>> I pass the string "empty" to the function as a patient number
>>> if we are
>>> creating an empty appointment slot.
>>>
>>> now, this fails because with a patient number of 0 the if
>>> returns true.
>>>
>>> I am aware of the function empty() that would work like that,
>>> but do not
>>> understand why the compare fails with the constant, "empty."
>>
>> I assume you are comparing 0 and not the string "0", this makes
>> you are comparing integer with string, there ain't any type
>> conversion done.
>
> Actually, it is because there *is* an automatic type conversion
> done when comparing a string to an int.
>
> "If you compare a number with a string or the comparison involves
> numerical strings, then each string is converted to a number and
> the comparison performed numerically."
>
> ... http://us.php.net/manual/en/language.operators.comparison.php
>
>> The zero is always equal to a string,
>
> Not true.
> "If the string starts with valid numeric data, this will be the
> value used. Otherwise, the value will be 0 (zero)."
>
> ....
> http://us.php.net/manual/en/language.types.string.php#language.types.string.conversion
>
>
>> there is a slightly more information at
>>
>> http://php.net/manual/en/language.operators.comparison.php
>
> You might want to take a closer look at that page.
>
> The literal string "empty" does not begin with a numerical
> character so it will be converted to the integer 0.

Thank you, that makes sense.
>
>>> the regular compare is true with the numeric value of zero.
>> I fixed the function by making it a strict compare, but would
>> like to know why
>>
>> I would say it's not the right way to compare, use strcmp,
>> is_numeric and so on, to make a proper compare instead of using
>> the comparison operator.
>>
>
> I think you have that backwards. The identical (strict)
> comparison is far more efficient than a function call and this
> it's intended usage. "===" compares value and type (so no type
> conversion). Hence the string "empty" does not equal (===) the
> integer 0.
>
Glad to know that I stumbled onto the right way to do it.
Your comments are much appreciated and enlightening.

bill

[toc] | [prev] | [next] | [standalone]


#4109

From"M. Strobel" <sorry_no_mail_here@nowhere.dee>
Date2011-12-20 17:40 +0100
Message-ID<9lbs4jFv23U1@mid.uni-berlin.de>
In reply to#4107
Am 20.12.2011 14:14, schrieb bill:
> On 12/19/2011 8:14 PM, Chuck Anderson wrote:
>> J.O. Aho wrote:
>>> bill wrote:
>>>> searching the manual gives lots of hits, none of which appear
>>>> relevant (at
>>>> least by the title).
>>>>
>>>> I had the following code in a scheduling class:
>>>>
>>>> if ($ptNum == "empty") {
>>>> $ptNum = 0;
>>>> $status= "e";
>>>> }
>>>>
>>>> I pass the string "empty" to the function as a patient number
>>>> if we are
>>>> creating an empty appointment slot.
>>>>
>>>> now, this fails because with a patient number of 0 the if
>>>> returns true.
>>>>
>>>> I am aware of the function empty() that would work like that,
>>>> but do not
>>>> understand why the compare fails with the constant, "empty."
>>>
>>> I assume you are comparing 0 and not the string "0", this makes
>>> you are comparing integer with string, there ain't any type
>>> conversion done.
>>
>> Actually, it is because there *is* an automatic type conversion
>> done when comparing a string to an int.
>>
>> "If you compare a number with a string or the comparison involves
>> numerical strings, then each string is converted to a number and
>> the comparison performed numerically."
>>
>> ... http://us.php.net/manual/en/language.operators.comparison.php
>>
>>> The zero is always equal to a string,
>>
>> Not true.
>> "If the string starts with valid numeric data, this will be the
>> value used. Otherwise, the value will be 0 (zero)."
>>
>> ....
>> http://us.php.net/manual/en/language.types.string.php#language.types.string.conversion
>>
>>
>>
>>> there is a slightly more information at
>>>
>>> http://php.net/manual/en/language.operators.comparison.php
>>
>> You might want to take a closer look at that page.
>>
>> The literal string "empty" does not begin with a numerical
>> character so it will be converted to the integer 0.
> 
> Thank you, that makes sense.
>>
>>>> the regular compare is true with the numeric value of zero.
>>> I fixed the function by making it a strict compare, but would
>>> like to know why
>>>
>>> I would say it's not the right way to compare, use strcmp,
>>> is_numeric and so on, to make a proper compare instead of using
>>> the comparison operator.
>>>
>>
>> I think you have that backwards. The identical (strict)
>> comparison is far more efficient than a function call and this
>> it's intended usage. "===" compares value and type (so no type
>> conversion). Hence the string "empty" does not equal (===) the
>> integer 0.
>>
> Glad to know that I stumbled onto the right way to do it.
> Your comments are much appreciated and enlightening.
> 
> bill
> 

Comparison in PHP, like in many scripting languages, has its
special cases, the same as javascript.

There is one link to php comparison that I use very often, and it
is very difficult to find, you will see very concise tables about
the different compares:

http://de3.php.net/manual/en/types.comparisons.php

[toc] | [prev] | [next] | [standalone]


#4115

Frombill <nobody@spamcop.net>
Date2011-12-20 18:44 -0500
Message-ID<qe2dna4PF6x-gGzTnZ2dnUVZ_rqdnZ2d@cablespeedmi.com>
In reply to#4109
On 12/20/2011 11:40 AM, M. Strobel wrote:
> Am 20.12.2011 14:14, schrieb bill:
>> On 12/19/2011 8:14 PM, Chuck Anderson wrote:
>>> J.O. Aho wrote:
>>>> bill wrote:
>>>>> searching the manual gives lots of hits, none of which appear
>>>>> relevant (at
>>>>> least by the title).
>>>>>
>>>>> I had the following code in a scheduling class:
>>>>>
>>>>> if ($ptNum == "empty") {
>>>>> $ptNum = 0;
>>>>> $status= "e";
>>>>> }
>>>>>
>>>>> I pass the string "empty" to the function as a patient number
>>>>> if we are
>>>>> creating an empty appointment slot.
>>>>>
>>>>> now, this fails because with a patient number of 0 the if
>>>>> returns true.
>>>>>
>>>>> I am aware of the function empty() that would work like that,
>>>>> but do not
>>>>> understand why the compare fails with the constant, "empty."
>>>>
>>>> I assume you are comparing 0 and not the string "0", this makes
>>>> you are comparing integer with string, there ain't any type
>>>> conversion done.
>>>
>>> Actually, it is because there *is* an automatic type conversion
>>> done when comparing a string to an int.
>>>
>>> "If you compare a number with a string or the comparison involves
>>> numerical strings, then each string is converted to a number and
>>> the comparison performed numerically."
>>>
>>> ... http://us.php.net/manual/en/language.operators.comparison.php
>>>
>>>> The zero is always equal to a string,
>>>
>>> Not true.
>>> "If the string starts with valid numeric data, this will be the
>>> value used. Otherwise, the value will be 0 (zero)."
>>>
>>> ....
>>> http://us.php.net/manual/en/language.types.string.php#language.types.string.conversion
>>>
>>>
>>>
>>>> there is a slightly more information at
>>>>
>>>> http://php.net/manual/en/language.operators.comparison.php
>>>
>>> You might want to take a closer look at that page.
>>>
>>> The literal string "empty" does not begin with a numerical
>>> character so it will be converted to the integer 0.
>>
>> Thank you, that makes sense.
>>>
>>>>> the regular compare is true with the numeric value of zero.
>>>> I fixed the function by making it a strict compare, but would
>>>> like to know why
>>>>
>>>> I would say it's not the right way to compare, use strcmp,
>>>> is_numeric and so on, to make a proper compare instead of using
>>>> the comparison operator.
>>>>
>>>
>>> I think you have that backwards. The identical (strict)
>>> comparison is far more efficient than a function call and this
>>> it's intended usage. "===" compares value and type (so no type
>>> conversion). Hence the string "empty" does not equal (===) the
>>> integer 0.
>>>
>> Glad to know that I stumbled onto the right way to do it.
>> Your comments are much appreciated and enlightening.
>>
>> bill
>>
>
> Comparison in PHP, like in many scripting languages, has its
> special cases, the same as javascript.
>
> There is one link to php comparison that I use very often, and it
> is very difficult to find, you will see very concise tables about
> the different compares:
>
> http://de3.php.net/manual/en/types.comparisons.php

thank you, that is very helpful.
bill

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web