Re: [PHP] I know this is not easy and I'm not stupid but...

2007-08-11 Thread Tijnema
On 8/10/07, Zoltán Németh <[EMAIL PROTECTED]> wrote:
> 2007. 08. 10, péntek keltezéssel 02.31-kor Jan Reiter ezt írta:
> > Hi!
> > Thank you for your response!
> >
> > The only intention of my code was to investigate the (back then) unexpected
> > behavior of the if statement.
> >
> >
> > With $var['test'] set to "blah" this expression should be false
> > ($var['test'] == 0) for what I know ...
> >
> > $var['test'] = "blah";
> > var_dump($var['test'] == 0);
> >
> > //returns bool(true)
> >
> > Now I know why this happens! According to Table 6.5 of the Operators page in
> > the PHP Manual in this comparison all of the values are converted to
> > integer. And  atoi("blah") for sure will fail!;-) So you have to use === to
> > keep the types of the values!
>
> I found something interesting:
>
> [EMAIL PROTECTED]:~ php -r '$var = 'bla'; var_dump('0' == $var);'
> bool(true)
> [EMAIL PROTECTED]:~ php -r '$var = 'bla'; var_dump("0" == $var);'
> bool(false)
>
> version is 5.2.1
>
> is this expected behaviour to be a difference between the two types of
> quotes in this case?
>
> greets
> Zoltán Németh

I think you're little bit confused by the quotes :P
and since you don't have error reporting including E_NOTICE, you don't
see that there's some problem with your code :P
If I execute your code with the -n option, I get same results.
When I put them in a PHP file like this:


I get :
bool(false)
bool(false)

and you probably also ;)


Tijnema
>
> >
> > Jan
> >
> >
> >
> > -Original Message-
> > From: Jim Lucas [mailto:[EMAIL PROTECTED]
> > Sent: Friday, August 10, 2007 1:47 AM
> > To: Jan Reiter
> > Cc: [EMAIL PROTECTED]; php-general@lists.php.net
> > Subject: Re: [PHP] I know this is not easy and I'm not stupid but...
> >
> > Jan Reiter wrote:
> > > Hi!
> > >
> > > Phil:
> > > Still I am curious what var_dump($userValues['afterDark']); at line 102.5
> > > would return.
> > > I managed to recreate that fault with
> > >
> > > $var['test'] = "blah";
> > >
> > > echo ($var['test']);
> > >
> > > if( $var['test'] == 0)
> > > {
> > > echo "ok";
> > > }
> > >
> > > //this returns blahok -- not expected.
> > >
> > > In my case Var_dump() returns string(4) "blah" as expected.
> > >
> > > Using
> > >
> > > if( $var['test'] === 0)
> > >
> > > behaves as expected!!
> >
> > Are you wanting to only test for a empty/non-empty string?
> >
> > if so, use this.
> >
> > if ( empty($var['test']) ) {
> >   echo "var['test'] is empty";
> > }
> >
> > >
> > >
> > > Jim:
> > > TypeCasting would only be effective if you used the type sensitive
> > > comparison operator === , because with "==" 0 equals NULL equals false
> > > equals "" and so on ... or do I miss something here??
> > >
> > >
> > > Hope that solves it for you! I'm still investigating why my first examples
> > > fails. I've got the strong feeling that I'm missing something there. I
> > don't
> > > believe in a php bug or a memory leak in this case! Must be something
> > pretty
> > > obvious! Anyone a clue??
> > >
> > > Thanks,
> > > Jan
> > >
> > >
> >
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


-- 
Vote for PHP Color Coding in Gmail! -> http://gpcc.tijnema.info

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



Re: [PHP] I know this is not easy and I'm not stupid but...

2007-08-10 Thread Richard Lynch
On Thu, August 9, 2007 1:24 am, Phil Curry wrote:

$userValues = ' 1';

> line 102  echo  ($userValues['afterDark']); // outputs 1
>
> line 103  if ( $userValues['afterDark'] == 0 ) {// passes

When using echo to display debug output ALWAYS surround the data with
some kind of delimiter:
echo "'", ($userValues['afterDark']), "'";

Or, use var_dump or print_r to have PHP do this for you.

-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

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



RE: [PHP] I know this is not easy and I'm not stupid but...

2007-08-10 Thread Zoltán Németh
2007. 08. 10, péntek keltezéssel 02.31-kor Jan Reiter ezt írta:
> Hi! 
> Thank you for your response! 
> 
> The only intention of my code was to investigate the (back then) unexpected
> behavior of the if statement. 
> 
> 
> With $var['test'] set to "blah" this expression should be false
> ($var['test'] == 0) for what I know ...
> 
> $var['test'] = "blah";
> var_dump($var['test'] == 0); 
> 
> //returns bool(true)
> 
> Now I know why this happens! According to Table 6.5 of the Operators page in
> the PHP Manual in this comparison all of the values are converted to
> integer. And  atoi("blah") for sure will fail!;-) So you have to use === to
> keep the types of the values! 

I found something interesting:

[EMAIL PROTECTED]:~ php -r '$var = 'bla'; var_dump('0' == $var);'
bool(true)
[EMAIL PROTECTED]:~ php -r '$var = 'bla'; var_dump("0" == $var);'
bool(false)

version is 5.2.1

is this expected behaviour to be a difference between the two types of
quotes in this case?

greets
Zoltán Németh

> 
> Jan
> 
> 
> 
> -----Original Message-----
> From: Jim Lucas [mailto:[EMAIL PROTECTED] 
> Sent: Friday, August 10, 2007 1:47 AM
> To: Jan Reiter
> Cc: [EMAIL PROTECTED]; php-general@lists.php.net
> Subject: Re: [PHP] I know this is not easy and I'm not stupid but...
> 
> Jan Reiter wrote:
> > Hi!
> > 
> > Phil:
> > Still I am curious what var_dump($userValues['afterDark']); at line 102.5
> > would return. 
> > I managed to recreate that fault with 
> > 
> > $var['test'] = "blah";
> > 
> > echo ($var['test']);
> > 
> > if( $var['test'] == 0)
> > {
> > echo "ok";
> > }
> > 
> > //this returns blahok -- not expected. 
> > 
> > In my case Var_dump() returns string(4) "blah" as expected.
> > 
> > Using 
> > 
> > if( $var['test'] === 0)
> > 
> > behaves as expected!!
> 
> Are you wanting to only test for a empty/non-empty string?
> 
> if so, use this.
> 
> if ( empty($var['test']) ) {
>   echo "var['test'] is empty";
> }
> 
> > 
> > 
> > Jim:
> > TypeCasting would only be effective if you used the type sensitive
> > comparison operator === , because with "==" 0 equals NULL equals false
> > equals "" and so on ... or do I miss something here??
> > 
> > 
> > Hope that solves it for you! I'm still investigating why my first examples
> > fails. I've got the strong feeling that I'm missing something there. I
> don't
> > believe in a php bug or a memory leak in this case! Must be something
> pretty
> > obvious! Anyone a clue??
> > 
> > Thanks,
> > Jan
> > 
> >
> 

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



RE: [PHP] I know this is not easy and I'm not stupid but...

2007-08-09 Thread Jan Reiter
Ummh, no!

(int) and (integer) perform a C style atoi() conversion. 

(int)"blah" => integer 0
(int)"" => integer 0
(int)"1"=> integer 1
(int)"12"   => integer 12

 ("1" == 0) => false
 ("blah" == 0)  => true

( (int)$userValues['afterDark'] === 0 ) only is true, if
$userValues['afterDark'] is a string not containing an integer. This is what
happens:

$userValues['afterDark'] gets converted to integer by atoi(), and after that
is compared by type and value to integer 0. So the type part of the test
will pass for sure. 

But perhaps you should try to adapt the comparison to the DB value. So try
($userValues['afterDark'] == (string)0)
This expression is only true if $userValues['afterDark'] equals "0", not
when $userValues['afterDark'] equals "" or "blah" or "1"

Hope that helps!

Jan


-----Original Message-
From: Phil Curry [mailto:[EMAIL PROTECTED] 
Sent: Friday, August 10, 2007 2:49 AM
To: Stut
Cc: php-general
Subject: Re: [PHP] I know this is not easy and I'm not stupid but...

> Please include the list when replying.
>
> Phil Curry wrote:
>>> Phil Curry wrote:
>>>> how can this be? This is not the first time I've run into a  
>>>> situation like this. What am I missing?
>>>> line 102echo  ($userValues['afterDark']); //  
>>>> outputs 1
>>>> line 103if ( $userValues['afterDark'] == 0 ) {// passes
>>>
>>> Add a line at 102.5...
>>>
>>> var_dump($userValues['afterDark']);
>>>
>>> What type is that variable?
>> Don't have to do a dump, I know its a tinyint(1), not null, default 0
>
> That would be the type in the database, not the type of that  
> variable at that time.
>
> -Stut
>
> -- 
> http://stut.net/

Absolutely right. didn't think of that.

var_dump($userValues['afterDark']);  ==> string(1) "1"

but then I'm comparing a string to an int and the result always seems  
to pass. So does that mean any string compared to any int will be true?
By casting (int)$userValues['afterDark'] the test still passes and  
var_dump shows the (int)$userValues['afterDark'] as an int(1) but  
does give a value like the previous example.
ie. string(1)"1" but only int(1) Not sure if the expression has a  
value of "1" now that its been cast.

Then finally  if ( (int)$userValues['afterDark'] === 0 ) {//  
passes

So:
if ( $userValues['afterDark'] == 0 ) {  // passes
if ( (int)$userValues['afterDark'] == 0 ) {   // passes
if ( (int)$userValues['afterDark'] === 0 ) { // passes

And the value of afterdark in the mysql table is tinyint(1) 1.
And echo( 1+(int)$userValues['afterDark']);// outputs 2

Now I'm confused!
-Phil

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

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



Re: [PHP] I know this is not easy and I'm not stupid but...

2007-08-09 Thread Phil Curry

Please include the list when replying.

Phil Curry wrote:

Phil Curry wrote:
how can this be? This is not the first time I've run into a  
situation like this. What am I missing?
line 102echo  ($userValues['afterDark']); //  
outputs 1

line 103if ( $userValues['afterDark'] == 0 ) {// passes


Add a line at 102.5...

var_dump($userValues['afterDark']);

What type is that variable?

Don't have to do a dump, I know its a tinyint(1), not null, default 0


That would be the type in the database, not the type of that  
variable at that time.


-Stut

--
http://stut.net/


Absolutely right. didn't think of that.

var_dump($userValues['afterDark']);  ==> string(1) "1"

but then I'm comparing a string to an int and the result always seems  
to pass. So does that mean any string compared to any int will be true?
By casting (int)$userValues['afterDark'] the test still passes and  
var_dump shows the (int)$userValues['afterDark'] as an int(1) but  
does give a value like the previous example.
ie. string(1)"1" but only int(1) Not sure if the expression has a  
value of "1" now that its been cast.


Then finally  if ( (int)$userValues['afterDark'] === 0 ) {//  
passes


So:
if ( $userValues['afterDark'] == 0 ) {  // passes
if ( (int)$userValues['afterDark'] == 0 ) {   // passes
if ( (int)$userValues['afterDark'] === 0 ) { // passes

And the value of afterdark in the mysql table is tinyint(1) 1.
And echo( 1+(int)$userValues['afterDark']);// outputs 2

Now I'm confused!
-Phil

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



RE: [PHP] I know this is not easy and I'm not stupid but...

2007-08-09 Thread Jan Reiter
Hi! 
Thank you for your response! 

The only intention of my code was to investigate the (back then) unexpected
behavior of the if statement. 


With $var['test'] set to "blah" this expression should be false
($var['test'] == 0) for what I know ...

$var['test'] = "blah";
var_dump($var['test'] == 0); 

//returns bool(true)

Now I know why this happens! According to Table 6.5 of the Operators page in
the PHP Manual in this comparison all of the values are converted to
integer. And  atoi("blah") for sure will fail!;-) So you have to use === to
keep the types of the values! 

Jan



-Original Message-
From: Jim Lucas [mailto:[EMAIL PROTECTED] 
Sent: Friday, August 10, 2007 1:47 AM
To: Jan Reiter
Cc: [EMAIL PROTECTED]; php-general@lists.php.net
Subject: Re: [PHP] I know this is not easy and I'm not stupid but...

Jan Reiter wrote:
> Hi!
> 
> Phil:
> Still I am curious what var_dump($userValues['afterDark']); at line 102.5
> would return. 
> I managed to recreate that fault with 
> 
> $var['test'] = "blah";
> 
> echo ($var['test']);
> 
> if( $var['test'] == 0)
> {
>   echo "ok";
> }
> 
> //this returns blahok -- not expected. 
> 
> In my case Var_dump() returns string(4) "blah" as expected.
> 
> Using 
> 
> if( $var['test'] === 0)
> 
> behaves as expected!!

Are you wanting to only test for a empty/non-empty string?

if so, use this.

if ( empty($var['test']) ) {
echo "var['test'] is empty";
}

> 
> 
> Jim:
> TypeCasting would only be effective if you used the type sensitive
> comparison operator === , because with "==" 0 equals NULL equals false
> equals "" and so on ... or do I miss something here??
> 
> 
> Hope that solves it for you! I'm still investigating why my first examples
> fails. I've got the strong feeling that I'm missing something there. I
don't
> believe in a php bug or a memory leak in this case! Must be something
pretty
> obvious! Anyone a clue??
> 
> Thanks,
> Jan
> 
>

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



Re: [PHP] I know this is not easy and I'm not stupid but...

2007-08-09 Thread Jim Lucas

Jan Reiter wrote:

Hi!

Phil:
Still I am curious what var_dump($userValues['afterDark']); at line 102.5
would return. 
I managed to recreate that fault with 


$var['test'] = "blah";

echo ($var['test']);

if( $var['test'] == 0)
{
echo "ok";
}

//this returns blahok -- not expected. 


In my case Var_dump() returns string(4) "blah" as expected.

Using 


if( $var['test'] === 0)

behaves as expected!!


Are you wanting to only test for a empty/non-empty string?

if so, use this.

if ( empty($var['test']) ) {
echo "var['test'] is empty";
}




Jim:
TypeCasting would only be effective if you used the type sensitive
comparison operator === , because with "==" 0 equals NULL equals false
equals "" and so on ... or do I miss something here??


Hope that solves it for you! I'm still investigating why my first examples
fails. I've got the strong feeling that I'm missing something there. I don't
believe in a php bug or a memory leak in this case! Must be something pretty
obvious! Anyone a clue??

Thanks,
Jan


-Original Message-----
From: Jim Lucas [mailto:[EMAIL PROTECTED] 
Sent: Thursday, August 09, 2007 8:08 PM

To: Stut
Cc: Phil Curry; php-general
Subject: Re: [PHP] I know this is not easy and I'm not stupid but...

Stut wrote:

Please include the list when replying.

Phil Curry wrote:

Phil Curry wrote:
how can this be? This is not the first time I've run into a 
situation like this. What am I missing?

line 102echo  ($userValues['afterDark']); // outputs 1
line 103if ( $userValues['afterDark'] == 0 ) {// passes

Add a line at 102.5...

var_dump($userValues['afterDark']);

What type is that variable?

Don't have to do a dump, I know its a tinyint(1), not null, default 0


PHP does not have a type of tinyint

knowing now that this comes from a database, if it is mysql, then it is of
type string

PHP converts all fields when pulled from the database into strings.  It does
not take into account 
what type the field is actually set to in mysql.


Try puting a (int) in front of the condition like this

if ( (int)$userValues['afterDark'] == 0 ) {
...
}

Maybe this will help

That would be the type in the database, not the type of that variable at 
that time.


-Stut







--
Jim Lucas

   "Some men are born to greatness, some achieve greatness,
   and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
by William Shakespeare

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



RE: [PHP] I know this is not easy and I'm not stupid but...

2007-08-09 Thread Jan Reiter
Hi!

Phil:
Still I am curious what var_dump($userValues['afterDark']); at line 102.5
would return. 
I managed to recreate that fault with 

$var['test'] = "blah";

echo ($var['test']);

if( $var['test'] == 0)
{
echo "ok";
}

//this returns blahok -- not expected. 

In my case Var_dump() returns string(4) "blah" as expected.

Using 

if( $var['test'] === 0)

behaves as expected!!


Jim:
TypeCasting would only be effective if you used the type sensitive
comparison operator === , because with "==" 0 equals NULL equals false
equals "" and so on ... or do I miss something here??


Hope that solves it for you! I'm still investigating why my first examples
fails. I've got the strong feeling that I'm missing something there. I don't
believe in a php bug or a memory leak in this case! Must be something pretty
obvious! Anyone a clue??

Thanks,
Jan


-Original Message-
From: Jim Lucas [mailto:[EMAIL PROTECTED] 
Sent: Thursday, August 09, 2007 8:08 PM
To: Stut
Cc: Phil Curry; php-general
Subject: Re: [PHP] I know this is not easy and I'm not stupid but...

Stut wrote:
> Please include the list when replying.
> 
> Phil Curry wrote:
>>> Phil Curry wrote:
>>>> how can this be? This is not the first time I've run into a 
>>>> situation like this. What am I missing?
>>>> line 102echo  ($userValues['afterDark']); // outputs 1
>>>> line 103if ( $userValues['afterDark'] == 0 ) {// passes
>>>
>>> Add a line at 102.5...
>>>
>>> var_dump($userValues['afterDark']);
>>>
>>> What type is that variable?
>>
>> Don't have to do a dump, I know its a tinyint(1), not null, default 0

PHP does not have a type of tinyint

knowing now that this comes from a database, if it is mysql, then it is of
type string

PHP converts all fields when pulled from the database into strings.  It does
not take into account 
what type the field is actually set to in mysql.

Try puting a (int) in front of the condition like this

if ( (int)$userValues['afterDark'] == 0 ) {
...
}

Maybe this will help

> 
> That would be the type in the database, not the type of that variable at 
> that time.
> 
> -Stut
> 


-- 
Jim Lucas

"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
 by William Shakespeare

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

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



Re: [PHP] I know this is not easy and I'm not stupid but...

2007-08-09 Thread Jim Lucas

Stut wrote:

Please include the list when replying.

Phil Curry wrote:

Phil Curry wrote:
how can this be? This is not the first time I've run into a 
situation like this. What am I missing?

line 102echo  ($userValues['afterDark']); // outputs 1
line 103if ( $userValues['afterDark'] == 0 ) {// passes


Add a line at 102.5...

var_dump($userValues['afterDark']);

What type is that variable?


Don't have to do a dump, I know its a tinyint(1), not null, default 0


PHP does not have a type of tinyint

knowing now that this comes from a database, if it is mysql, then it is of type 
string

PHP converts all fields when pulled from the database into strings.  It does not take into account 
what type the field is actually set to in mysql.


Try puting a (int) in front of the condition like this

if ( (int)$userValues['afterDark'] == 0 ) {
...
}

Maybe this will help



That would be the type in the database, not the type of that variable at 
that time.


-Stut




--
Jim Lucas

   "Some men are born to greatness, some achieve greatness,
   and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
by William Shakespeare

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



Re: [PHP] I know this is not easy and I'm not stupid but...

2007-08-09 Thread Stut

Please include the list when replying.

Phil Curry wrote:

Phil Curry wrote:
how can this be? This is not the first time I've run into a situation 
like this. What am I missing?

line 102echo  ($userValues['afterDark']); // outputs 1
line 103if ( $userValues['afterDark'] == 0 ) {// passes


Add a line at 102.5...

var_dump($userValues['afterDark']);

What type is that variable?


Don't have to do a dump, I know its a tinyint(1), not null, default 0


That would be the type in the database, not the type of that variable at 
that time.


-Stut

--
http://stut.net/

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



Re: [PHP] I know this is not easy and I'm not stupid but...

2007-08-09 Thread Stut

Phil Curry wrote:
how can this be? This is not the first time I've run into a situation 
like this. What am I missing?


line 102echo  ($userValues['afterDark']); // outputs 1

line 103if ( $userValues['afterDark'] == 0 ) {// passes


Add a line at 102.5...

var_dump($userValues['afterDark']);

What type is that variable?

-Stut

--
http://stut.net/

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