FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » empty - not empty()
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
empty - not empty() [message #176291] Mon, 19 December 2011 15:09 Go to next message
bill is currently offline  bill
Messages: 310
Registered: October 2010
Karma: 0
Senior Member
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
Re: empty - not empty() [message #176292 is a reply to message #176291] Mon, 19 December 2011 17:48 Go to previous messageGo to next message
J.O. Aho is currently offline  J.O. Aho
Messages: 194
Registered: September 2010
Karma: 0
Senior Member
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
Re: empty - not empty() [message #176304 is a reply to message #176292] Tue, 20 December 2011 01:14 Go to previous messageGo to next message
Chuck Anderson is currently offline  Chuck Anderson
Messages: 63
Registered: September 2010
Karma: 0
Member
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
*****************************
Re: empty - not empty() [message #176310 is a reply to message #176304] Tue, 20 December 2011 13:14 Go to previous messageGo to next message
bill is currently offline  bill
Messages: 310
Registered: October 2010
Karma: 0
Senior Member
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
Re: empty - not empty() [message #176312 is a reply to message #176310] Tue, 20 December 2011 16:40 Go to previous messageGo to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
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
Re: empty - not empty() [message #176318 is a reply to message #176312] Tue, 20 December 2011 23:44 Go to previous message
bill is currently offline  bill
Messages: 310
Registered: October 2010
Karma: 0
Senior Member
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
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: FastCGI + ignore_user_abort
Next Topic: united governments of planet earth
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Tue Nov 26 04:19:15 GMT 2024

Total time taken to generate the page: 0.08258 seconds