473,396 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

empty() returns false for 0.00

code green
1,726 Expert 1GB
I am using this line of code to test for a price
Expand|Select|Wrap|Line Numbers
  1. if(empty($line['price']))
  2. {
  3.     $err .= '<pre>No Price for '.print_r($line,1).'</pre>';
  4.     return false;
  5. }
The price comes from a database as DECIMAL and I am suprised 0.00 is not 'empty'. Is this normal behaviour?
It does suit my purposes as there is a difference between free-of-charge 0.00 and a missing price '', which I am tyring to trap.
Jan 19 '11 #1
9 7574
Rabbit
12,516 Expert Mod 8TB
0.00 is not empty. It's a number. You'll just need a logical or to test for the 0 case.
Jan 19 '11 #2
edaj
2
you probably need to write in the IF condition this:

empty($line['price'] === false

With that you can difference if is a 'really' false or other value
Jan 19 '11 #3
Dormilich
8,658 Expert Mod 8TB
0.00 is not empty. It's a number.
it’s not a number either, it’s a string (like anything you get off the $_POST/$_GET arrays).
Jan 20 '11 #4
code green
1,726 Expert 1GB
it’s not a number either, it’s a string (like anything you get off the $_POST/$_GET arrays).
It is directly from a database via mssql functions not a web-form.
But the important thing is, 0.00 is not empty - ever, so my test above is fine as free of charge goods will have a 0.00 price,
and un-priced goods will be NULL in the database and need to be trapped.
Thanks for all the comments
Jan 20 '11 #5
Dormilich
8,658 Expert Mod 8TB
you’re right, I misread that part. you could simply test the datatype with var_dump(), though I suspect even DB results are strings (not much different from $_POST)
Jan 20 '11 #6
code green
1,726 Expert 1GB
though I suspect even DB results are strings (not much different from $_POST)
Interesting point, I have never investigated this.
But I have found myself having to cast (float) in php doing calculations,
even though the data from the DB is supposedly in a numeric format.
Jan 20 '11 #7
Dormilich
8,658 Expert Mod 8TB
well, I have never doubted this as you send/get data in a serialized format. and I’m pretty sure the mysql functions don’t do any back conversion.

it might be different if you used prepared statements and have bound a parameter.
Jan 20 '11 #8
mfaisalwarraich
194 100+
according to php manual a variable is empty if meeting anyone of the following case:
Expand|Select|Wrap|Line Numbers
  1. "" (an empty string)
  2. 0 (0 as an integer)
  3. "0" (0 as a string)
  4. NULL
  5. FALSE
  6. array() (an empty array)
  7. var $var; (a variable declared, but without a value in a class)
  8.  
Jan 20 '11 #9
dlite922
1,584 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1.  
  2. if("0.00" == 0) echo "Just FYI, this returns true."; 
  3. else echo "Surprised if it doesn't!"; 
  4.  
  5.  
DM
Jan 21 '11 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Martin Lucas-Smith | last post by:
I wrote the function below as part of a larger class. The fopen stage works, and, as according to the documentation at www.php.net/fopen that succesfully creates a new file. The fwrite stage...
3
by: annie | last post by:
Hi I've got two linux boxes running redhat9 and either PHP Version 4.2.2 or PHP Version 4.3.7. The file structure is set up the same on both machines and both files exist. The problem is not...
5
by: lkrubner | last post by:
www.php.net says: >>>>>>>>>>>> Only for SELECT,SHOW,EXPLAIN or DESCRIBE statements mysql_query() returns a resource identifier or FALSE if the query was not executed correctly. For other type of...
0
by: Boris Zakharin | last post by:
I am using the .Net framework 1.1 and VS.NET 2003. I have some code where, for some reason, IsPostBack returns false messing up my code. I have a DataGrid, whose data I am trying to edit. Instead...
4
by: Sacha Korell | last post by:
First of all, sorry for cross-posting, but this newsgroup is much busier than "aspnet.mobile". I'm trying to create a portal page that redirects depending on the device that is connecting. ...
0
by: Markus Ernst | last post by:
Hi In an application installed with PHP5 it looks like ftp_nlist() returns false if the directory is empty, instead of an empty array as it should according to the manual. On the original server...
4
by: Italian Pete | last post by:
Hi, I have a website sitting on a server (ServerA) with images to be displayed sitting on a separate server (ServerB) on the same network. I am trying to test for the existance of an image...
12
by: snow | last post by:
Hi All, I noticed if file path has a white space, for example "C:\my document \test.txt", the function File.Exists(filePath) always return false in release mode. How could I make this function...
5
by: khalid Ahmed | last post by:
I'm attempting to execute a SQL SELECT query but it doesn't seem to be returning any records. I've confirmed that the connection is actually opening and when I tried to execute the same query on...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.