473,325 Members | 2,816 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,325 software developers and data experts.

how can one variable equal 2 things??

I'm having a problem that is baffling me... say I set a session variable
as so:

$_SESSION['temp'] = "default";

Now if I test the following two conditionals:

if($_SESSION['temp'] == "default")

and

if($_SESSION['temp'] == 0)

they both return TRUE. I don't understand why this is... if anything, I
would have thought testing if it == 1 would be a boolean true since the
variable exists and is not NULL, but testing if == 1 returns FALSE.
Thanks in advance.
Jul 17 '05 #1
3 2248
When (non-numeric) strings are used in the context of a number (your ==
0 comparison above) they evaluate to zero. Hence, both of your tests
evaluate to true.

Check out:

http://www.php.net/types.string#lang...ing.conversion

Jul 17 '05 #2
On 02/07/2005 23:34, Marcus wrote:

[snip]
if anything, I would have thought testing if ['default'] == 1 would
be a boolean true since the variable exists and is not NULL, but
testing if ['default'] == 1 returns FALSE.


This is due to implicit type conversion. When you use the equality (==)
rather than strict equality (===) and compare two different types, the
values are converted into something that can be compared in some
meaningful way.

If you compare a string to a number, an attempt is made to convert that
string to a number first. If the string starts with valid number data,
then that will be the used value, otherwise the comparison will be
against zero (0).

The string 'default' doesn't have anything resembling a number at its
start, so it will be converted to zero. You then perform a comparison
against literal zero, hence the expression evaluates to true.

See the type comparison page[1], as well as the section on converting
strings to numbers[2], in the manual.

Mike
[1] <URL:http://www.php.net/manual/en/types.comparisons.php>
[2]
<URL:http://www.php.net/manual/en/language.types.string.php#language.types.string.co nversion>

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Jul 17 '05 #3
Michael Winter wrote:
On 02/07/2005 23:34, Marcus wrote:

[snip]
if anything, I would have thought testing if ['default'] == 1 would be
a boolean true since the variable exists and is not NULL, but testing
if ['default'] == 1 returns FALSE.

This is due to implicit type conversion. When you use the equality (==)
rather than strict equality (===) and compare two different types, the
values are converted into something that can be compared in some
meaningful way.

If you compare a string to a number, an attempt is made to convert that
string to a number first. If the string starts with valid number data,
then that will be the used value, otherwise the comparison will be
against zero (0).

The string 'default' doesn't have anything resembling a number at its
start, so it will be converted to zero. You then perform a comparison
against literal zero, hence the expression evaluates to true.

See the type comparison page[1], as well as the section on converting
strings to numbers[2], in the manual.

Mike
[1] <URL:http://www.php.net/manual/en/types.comparisons.php>
[2]
<URL:http://www.php.net/manual/en/language.types.string.php#language.types.string.co nversion>


Thank you both for the links and info, I enclosed my 0 in quotes and
everything now works.
Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

83
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in...
10
by: David Casey | last post by:
I'm working on a program for my C++ class and have it all written and working except for one part. I need to compare two numeric variables to determine decimal accuracy between them. For example:...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
19
by: Skybuck Flying | last post by:
Hi, I think I might have just invented the variable bit cpu :) It works simply like this: Each "data bit" has a "meta data bit". The meta data bit describes if the bit is the ending bit...
4
by: Bob T | last post by:
Hi All, I am trying to pass a variable from my VB asp.net script (from for example Sub Page_Load in mypage.aspx.vb) to my Client side script. I have found and looked at a very good example...
5
by: chadsmith76 | last post by:
hello, I have listings with coordinates, i would like to do ORDER BY and display listings with coords first. If they don't have coords the value is blank and the coords go both positive and...
2
by: whiskers | last post by:
I'm debugging some code and I have to admit that I don't know yet how it works. But I ran into a problem I can't explain The program is a DLL that retrieves raw data from a camera, builds...
7
by: stefan.istrate | last post by:
I have a short int variable and I'm trying to read it with scanf("%d",&x); but I'm getting a warning message which sounds like this: warning: format '%d' expects type 'int*', but argument 2 has...
1
by: mcolson | last post by:
Is it possible to set a variable in a stored procedure equal to a value from a column when that column's respective ID is equal to max(id)-1 ID A B 1 24 24 2 53 ...
4
by: Constantine AI | last post by:
I set a global variable to equal a new password which was the wrong way of doing things, now i have tried many things to get rid of the global variable. All the text evidence has been deleted but it...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.