dbTalk Databases Forums  

Question

comp.databases.mysql comp.databases.mysql


Discuss Question in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #11  
Old   
Doug Miller
 
Posts: n/a

Default Re: Question - 09-26-2011 , 09:32 PM






On 9/26/2011 4:39 PM, Bint wrote:

[snip[
Quote:
I just cannot believe that

if ($string)

does not give the same result as

if ($string != 0)
That is a failing of your belief system, not of the language design.
There is no reason to expect that comparing a string to a numeric value
will produce a meaningful result.

Reply With Quote
  #12  
Old   
richard
 
Posts: n/a

Default Re: Question - 09-27-2011 , 02:12 AM






On Mon, 26 Sep 2011 11:08:53 -0500, Bint wrote:

Quote:
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
Simple. You're trying to check to see if a string value is a numeric zero or not. That ain't gonna happen.
For instance
if ($a)=1 do this.
What you need to do is to test for NULL. That is, the string has no content.
BASIC would use something like if a$<>"" then dothis.
Or if len(a$)<1 then do this. len=length
Which, IMO is more proper since a string of 1 character could have a value of zero.

If you are attempting to test for the string value to be not zero, then you must convert the string to a numeric value and check that result.



If my line wrap is screwed up, that's because I hit some keys I should not have.

Reply With Quote
  #13  
Old   
Erick T. Barkhuis
 
Posts: n/a

Default Re: Question - 09-27-2011 , 02:21 AM



richard:

Quote:
Simple. You're trying to check to see if a string value is a numeric
zero or not. That ain't gonna happen. For instance
if ($a)=1 do this.
'This' will be done, because now, you assign value 1 to $a successfully.

Quote:
What you need to do is to test for NULL.
In PHP? That would be isset($a), which returns true for an empty string.

Quote:
That is, the string has no
content. BASIC would use something like if a$<>"" then dothis.
An empty string is not equivalent to NULL.

Quote:
If my line wrap is screwed up, that's because I hit some keys I
should not have.
The line wrap is OK, but your comments aren't 100% correct.



--
Erick

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

Default Re: Question - 09-27-2011 , 03:10 AM



On 27.09.2011 09:21, Erick T. Barkhuis wrote:
Quote:
if ($a)=1 do this.

'This' will be done, because now, you assign value 1 to $a successfully.
Nope. That is syntax error. Parentheses make this a value, not a variable.

Reply With Quote
  #15  
Old   
Erick T. Barkhuis
 
Posts: n/a

Default Re: Question - 09-27-2011 , 07:00 AM



Jacek Krysztofik:

Quote:
On 27.09.2011 09:21, Erick T. Barkhuis wrote:
if ($a)=1 do this.

'This' will be done, because now, you assign value 1 to $a
successfully.
Nope. That is syntax error. Parentheses make this a value, not a
variable.
Ah well, I considered that a typo, and thought that
if ($a=1)
was meant.



--
Erick

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.