dbTalk Databases Forums  

Question

comp.databases.mysql comp.databases.mysql


Discuss Question in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Bint
 
Posts: n/a

Default Question - 09-26-2011 , 11:08 AM






Hi,

Can someone tell me why this little piece of php/mysql code fails?

$row = $result->fetch_assoc();

if ($clog)
fwrite($logfile,"row['nametag'] {$row['nametag']}\n");

if ($row['nametag'] != 0) {
if ($clog)
fwrite($logfile,"name {$row['nametag']}\n");
}


I've got this logfile where I am putting debug output. In the above, the
first fwrite works -- it writes the name contained in the row value
'nametag'.

But then, when I test that same value for nonzero, it fails? If never
prints that second line. Why should $row['nametag'] fail the != 0 test?

Thanks
Bob

Reply With Quote
  #2  
Old   
Jacek Krysztofik
 
Posts: n/a

Default Re: Question - 09-26-2011 , 11:19 AM






On 26.09.2011 18:08, Bint wrote:
Quote:
if ($row['nametag'] != 0) {
try
if ($row['nametag'] + 0)

Reply With Quote
  #3  
Old   
Jacek Krysztofik
 
Posts: n/a

Default Re: Question - 09-26-2011 , 11:21 AM



On 26.09.2011 18:08, Bint wrote:
Quote:
if ($row['nametag'] != 0) {
Is $row['nametag'] supposed to be numeric?

Reply With Quote
  #4  
Old   
Bint
 
Posts: n/a

Default Re: Question - 09-26-2011 , 11:44 AM



Quote:
On 26.09.2011 18:08, Bint wrote:
if ($row['nametag'] != 0) {
Is $row['nametag'] supposed to be numeric?
No, it's a string. If I just use
If ($row['nametag']) {

then it works. But it is just extremely counterintuitive that that != 0
test should fail. It is stuff like that that is always tripping me up in
PHP.

Thanks
Bob

Reply With Quote
  #5  
Old   
The Natural Philosopher
 
Posts: n/a

Default Re: Question - 09-26-2011 , 11:52 AM



Bint wrote:
Quote:
On 26.09.2011 18:08, Bint wrote:
if ($row['nametag'] != 0) {
Is $row['nametag'] supposed to be numeric?

No, it's a string. If I just use
If ($row['nametag']) {

then it works. But it is just extremely counterintuitive that that != 0
test should fail. It is stuff like that that is always tripping me up in
PHP.

weakly typed languages eh?

where '0'=0 !!!


Quote:
Thanks
Bob


Reply With Quote
  #6  
Old   
Jacek Krysztofik
 
Posts: n/a

Default Re: Question - 09-26-2011 , 11:56 AM



On 26.09.2011 18:44, Bint wrote:
Quote:
But it is just extremely counterintuitive that that != 0 test should fail.
No, it's bad coding style. Nobody but you understands what you're trying
to check here. You should always write EXACTLY what you test for, and
here you're not checking if the string is nonzero. And non-numeric
string equals zero.

Reply With Quote
  #7  
Old   
Bint
 
Posts: n/a

Default Re: Question - 09-26-2011 , 02:48 PM



Quote:
Bint wrote:
On 26.09.2011 18:08, Bint wrote:
if ($row['nametag'] != 0) {
Is $row['nametag'] supposed to be numeric?

No, it's a string. If I just use
If ($row['nametag']) {

then it works. But it is just extremely counterintuitive that that != 0
test should fail. It is stuff like that that is always tripping me up in
PHP.


weakly typed languages eh?

where '0'=0 !!!


But not even that! Where 'I am a string' == 0!
I was just trying to check whether the string was NULL or not basically, and
it IS counterintuitive to me at least that a string with something in it
would equate to 0.

Bob

Reply With Quote
  #8  
Old   
The Natural Philosopher
 
Posts: n/a

Default Re: Question - 09-26-2011 , 03:04 PM



Bint wrote:
Quote:
Bint wrote:
On 26.09.2011 18:08, Bint wrote:
if ($row['nametag'] != 0) {
Is $row['nametag'] supposed to be numeric?
No, it's a string. If I just use
If ($row['nametag']) {

then it works. But it is just extremely counterintuitive that that != 0
test should fail. It is stuff like that that is always tripping me up in
PHP.

weakly typed languages eh?

where '0'=0 !!!



But not even that! Where 'I am a string' == 0!
I was just trying to check whether the string was NULL or not basically, and
it IS counterintuitive to me at least that a string with something in it
would equate to 0.

I tend to use (strlen($string)==0)

Or ($string == "")..


Quote:
Bob

Reply With Quote
  #9  
Old   
Bint
 
Posts: n/a

Default Re: Question - 09-26-2011 , 03:39 PM



Quote:
Bint wrote:
Bint wrote:
On 26.09.2011 18:08, Bint wrote:
if ($row['nametag'] != 0) {
Is $row['nametag'] supposed to be numeric?
No, it's a string. If I just use
If ($row['nametag']) {

then it works. But it is just extremely counterintuitive that that != 0
test should fail. It is stuff like that that is always tripping me up in
PHP.

weakly typed languages eh?

where '0'=0 !!!



But not even that! Where 'I am a string' == 0!
I was just trying to check whether the string was NULL or not basically, and
it IS counterintuitive to me at least that a string with something in it
would equate to 0.


I tend to use (strlen($string)==0)

Or ($string == "")..

Well like I said,

if ($string)

is fine as long as it works, and it seems to work. I just cannot believe
that

if ($string)

does not give the same result as

if ($string != 0)

Bob

Reply With Quote
  #10  
Old   
Peter H. Coffin
 
Posts: n/a

Default Re: Question - 09-26-2011 , 03:58 PM



On Mon, 26 Sep 2011 21:04:05 +0100, The Natural Philosopher wrote:
Quote:
Bint wrote:
Bint wrote:
On 26.09.2011 18:08, Bint wrote:
if ($row['nametag'] != 0) {
Is $row['nametag'] supposed to be numeric?
No, it's a string. If I just use
If ($row['nametag']) {

then it works. But it is just extremely counterintuitive that that != 0
test should fail. It is stuff like that that is always tripping me up in
PHP.

weakly typed languages eh?

where '0'=0 !!!



But not even that! Where 'I am a string' == 0!
I was just trying to check whether the string was NULL or not basically, and
it IS counterintuitive to me at least that a string with something in it
would equate to 0.


I tend to use (strlen($string)==0)

Or ($string == "")..
Don't chop wood with a shovel. There's a right way to do this.

$ cat foo.php
<?php

echo "Test #1\n";

if (empty($string)) {
echo "not set, 0, 0.0, '' but not ' ', etc.\n";
if (! isset($string)) {
echo "not set at all\n";
}
}

echo "Test #2\n";

$string = 0;

if (empty($string)) {
echo "not set, 0, 0.0, '' but not ' ', etc.\n";
if (! isset($string)) {
echo "not set at all\n";
}
}

echo "Test #3\n";

$string = '';

if (empty($string)) {
echo "not set, 0, 0.0, '' but not ' ', etc.\n";
if (! isset($string)) {
echo "not set at all\n";
}
}

echo "Test #4\n";

$string = ' ';

if (empty($string)) {
echo "not set, 0, 0.0, '' but not ' ', etc.\n";
if (! isset($string)) {
echo "not set at all\n";
}
}

?>
$ php -f foo.php
Test #1
not set, 0, 0.0, '' but not ' ', etc.
not set at all
Test #2
not set, 0, 0.0, '' but not ' ', etc.
Test #3
not set, 0, 0.0, '' but not ' ', etc.
Test #4
$

--
2. My ventilation ducts will be too small to crawl through.
--Peter Anspach's list of things to do as an Evil Overlord

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.