473,378 Members | 1,334 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,378 software developers and data experts.

Does $i = 0 equal nothing?

155 100+
In $i = 0, is 0 the same as nothing or does it count as 1?

In a form I have these two fields - file and file_new. Only one (hopefully) should contain a value. I have to determine which one has a value to determine which function to execute.

[PHP]$file = mysql_real_escape_string($_POST['file']);
$file_new = mysql_real_escape_string($_POST['file_new']);

$i = 0;
if ($file > $i AND $file_new < $i)
{
function file();
}
else
{
funtion file_new();
}[/PHP]

I guess I could just leave off the second part of the if statement, unless this would help me to make sure that only one variable contains a value. If file_new also contained a value I could return an error message saying that only one of these fields can be filled out on the form.

Will this work or no?

OR is this better: [PHP]
if ($file != '' AND $file_new == '') {
run this code
}

if ($file == '' AND $file_new != '') {
run this code
}

if ($file != '' AND $file_new != '') {
run this code
}

[/PHP]

Thanks.
Jun 18 '07 #1
13 1928
nomad
664 Expert 512MB
In $i = 0, is 0 the same as nothing or does it count as 1?

In a form I have these two fields - file and file_new. Only one (hopefully) should contain a value. I have to determine which one has a value to determine which function to execute.

[PHP]$file = mysql_real_escape_string($_POST['file']);
$file_new = mysql_real_escape_string($_POST['file_new']);

$i = 0;
if ($file > $i AND $file_new < $i)
{
function file();
}
else
{
funtion file_new();
}[/PHP]

I guess I could just leave off the second part of the if statement, unless this would help me to make sure that only one variable contains a value. If file_new also contained a value I could return an error message saying that only one of these fields can be filled out on the form.

Will this work or no?

OR is this better: [PHP]
if ($file != '' AND $file_new == '') {
run this code
}

if ($file == '' AND $file_new != '') {
run this code
}

if ($file != '' AND $file_new != '') {
run this code
}

[/PHP]

Thanks.
what does an echo () return give you.
Jun 19 '07 #2
pbmods
5,821 Expert 4TB
Heya, DavidPr.

Try using empty.

$i = 0 evaluates to 0, which == false. However:

Expand|Select|Wrap|Line Numbers
  1. $bool = ($i = 0);
  2. var_dump($bool);  // int(0)
  3. var_dump($bool == false);  // boolean(true)
  4. var_dump($bool === false);  // boolean(false)
  5. var_dump($bool == '');  // boolean(true)
  6. var_dump($bool === '');  // boolean(false)
  7.  
http://www.php.net/manual/en/languag...comparison.php
Jun 19 '07 #3
moishy
104 100+
why don't you try if, elseif
Jun 19 '07 #4
Motoma
3,237 Expert 2GB
The way I would go about this a different way. First I would make a function to determine if a field was empty or not:

Expand|Select|Wrap|Line Numbers
  1. function hasvalue($var)
  2. {
  3.   if(isset($var))
  4.   {
  5.     if(trim($var) != '') return true;
  6.   }
  7.   return false;
  8. }
  9.  
Then I would do your tests on the value returning function:

Expand|Select|Wrap|Line Numbers
  1. if(hasvalue($file) xor hasvalue($file_new))
  2. {
  3.   if(hasvalue($file))
  4.   {
  5.      //Docrap
  6.   }
  7.   else
  8.   {
  9.      //Doothercrap
  10.   }
  11. }
  12. else
  13. {
  14.   echo "Please give a value for one and only one field";
  15. }
  16.  
This will allow you to adapt and change the hasvalue function later down the road, if your data types change, or if you want to perform some data validation in the function as well (highly recommended).

The other value of writing your code like this is that you KNOW how the function is going to react in later versions of PHP (as well as previous), and it is entirely obvious what the hell you are doing.
Jun 19 '07 #5
r035198x
13,262 8TB
In $i = 0, is 0 the same as nothing or does it count as 1?

....
Eh .
Jun 19 '07 #6
DavidPr
155 100+
I didn't know if whether or not the "0" in $i = 0 would be considered a value or the same as empty or ''.

I ended up with this:
[PHP]if(!empty($file) && empty($file_new)) {

// Do this

} elseif(!empty($file_new) && empty($file)) {

// Do this other thing

} else {

echo "Error - fill in just one field";

}[/PHP]

But I'm going to copy down what Motoma suggested and try it. I'll also read about "empty" as pbmods suggests.

When they come out with a new version of PHP, is it not backwards compatible?

Thanks you all for the help. I have more questions and will post them in another thread.

David
Jun 19 '07 #7
Motoma
3,237 Expert 2GB
When they come out with a new version of PHP, is it not backwards compatible?
They try to be as much as is safe HOWEVER:
Things like number evaluating to booleans and nulls being equivalent to unset and non-set variables have no defined behavior. This means that even though it may act a certain way, it is neither intended nor unintended for it to act that way. Using such hacks detracts from readability and reliability, and I would strongly suggest against relying on undocumented behavior.
Jun 19 '07 #8
DavidPr
155 100+
I didn't think about the security issues, but it makes sense... unfortunately.
Jun 19 '07 #9
pbmods
5,821 Expert 4TB
Heya, David.

You may also find this page useful:
http://www.php.net/manual/en/types.comparisons.php
Jun 19 '07 #10
Motoma
3,237 Expert 2GB
I didn't think about the security issues, but it makes sense... unfortunately.
Well, security wasn't exactly the point I was trying to make.

To restate the point I intended to assert:
Just because it works one way, does not mean it is supposed to work that way.

In your case, you were trying to determine if the value of a string was larger than the value of an integer. In this situation, the greater than operator is not strictly defined by the language with regards to what it evaluates to.

The fact that it evaluates to anything may be a mere coincidence caused by the internal state of the PHP interpreter at the moment it was executed, or it may be due to implicit type casting beforehand. The problem is: we don't know.

If you wrote your code expecting the greater than operator to handle the situation a particular way, and the reason it does is because of casting, you are all set as long as string comparisons do not change. However, if the result of the operator is in fact due to coincidence, you cannot rely on that behavior between different executions of the script, between different versions of PHP, or between different computers or operating systems.

I hope this clarifies the point I was trying to make.
Good luck with your code, and come back if you have any other questions.
Jun 19 '07 #11
pbmods
5,821 Expert 4TB
Are we having fun yet?

To expand on Motoma's point, what if one day PHP became truly Object-Oriented and supported operator overloading?

Code is PHP/Java/C hybrid

Expand|Select|Wrap|Line Numbers
  1. String.prototype['>'] = function($rhs) {
  2.     if($rhs->constructor != String)
  3.         return $this->length > String($rhs)->length;
  4.     return $this->length > $rhs->length;
  5. }
  6.  
Now, if $i = '', $i > 0 will actually return false because (''->length ! > '0'->length).
Jun 20 '07 #12
r035198x
13,262 8TB
Are we having fun yet?

To expand on Motoma's point, what if one day PHP became truly Object-Oriented .....
They should ...
Jun 20 '07 #13
Motoma
3,237 Expert 2GB
They should ...
There is a PECL extension that allows one to perform operator overloading.
Jun 20 '07 #14

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

Similar topics

24
by: David Mathog | last post by:
If this: int i,sum; int *array; for(sum=0, i=0; i<len; i++){ sum += array; } is converted to this (never mind why for the moment):
7
by: hugo27 | last post by:
obrhy8 June 18, 2004 Most compilers define EOF as -1. I'm just putting my toes in the water with a student's model named Miracle C. The ..h documentation of this compiler does state that when...
17
by: orekinbck | last post by:
Hi There Say I want to check if object1.Property1 is equal to a value, but object1 could be null. At the moment I have code like this: if (object1 != null) { if (object1.Property ==...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
29
by: Vol | last post by:
I think 'atan' can get the angle but it is not the four quadrant angle. Is there any function that i can get the angle from -pi to pi? or I have to use some if ... else? I know in Matlab, we use...
38
by: ssg31415926 | last post by:
I need to compare two string arrays defined as string such that the two arrays are equal if the contents of the two are the same, where order doesn't matter and every element must be unique. ...
10
by: Thierry Lam | last post by:
What does the following macro mean, especially the << sign: #define hello(x) (( int64 ) floor( (double) x ) << 32) Thanks Thierry
130
by: Daniel Manes | last post by:
I'm baffled. I have a column in a SQL Server Express database called "Longitude," which is a float. When I view the table in a DataGridView, some of the numbers, which only have two decimal places...
2
by: Nikhil.S.Ketkar | last post by:
Hi, How does the == operator for multimap in STL behave ? I was under the impression that this is supposed to properly compare multimaps for equality. It seems that what it actually does is just...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.