473,503 Members | 6,385 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with 'switch' and zero

I'm trying to use a switch construct to compare the result of a form
with a list of known values and display a different page according to
the submitted string. The problem is that some of the answers I need to
check for are strings of the character zero of varying length:

E.g.
<?php

switch (trim(strtolower($_POST['answer']))):

case "abc":
case "xyz":
case "0":
include 'close.htm';
break;
case "0000000000":
include 'vclose.htm';
break;
default:
include 'sorry.htm';
endswitch;
?>

What happens is that a submission consisting of a string of any number
of zeroes cause the first page to be displayed rather than the second. I
thought this was because it was treating it as an integer 0, so I tried
putting (string) casts all over the place, which didn't help.

Could someone please tell me how to achieve what I want?
--
Steve Loft
Jul 17 '05 #1
7 3300
Steve Loft wrote:
I'm trying to use a switch construct to compare the result of a form
with a list of known values and display a different page according to
the submitted string. The problem is that some of the answers I need to
check for are strings of the character zero of varying length: I thought this was because it was treating it as an integer 0, so I
tried putting (string) casts all over the place, which didn't help.


No. PHP switch()s compare with "=="

and, for PHP
"000000" == "0"
is true.

You have to turn that switch into a series of if()s -- AFAIK there is no
way to do "000000" === "0" (which if false) inside the switch() construct

so, instead of
switch(something):
case "abc":
...
do
if (something === "abc") ...
elseif (something === "0") ...
elseif (something === "000000") ...
...
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #2
Pedro Graca wrote:
You have to turn that switch into a series of if()s -- AFAIK there is no
way to do "000000" === "0" (which if false) inside the switch() construct


Ah. OK, thanks very much, even if it wasn't good news. That seems pretty
stupid of PHP to me, but anyway...
--
Steve Loft
Jul 17 '05 #3
Pedro Graca wrote:

You have to turn that switch into a series of if()s -- AFAIK there is no
way to do "000000" === "0" (which if false) inside the switch() construct


I've tried this, and I get the same result. I've even tried putting
string casts in, and I've tried single quotes and double quotes:

$ans = (string)trim(strtolower($_POST['answer']));

if ((string)$ans == (string)"0") {
include 'close.htm';
} elseif ((string)$ans == (string)"0000000000") {
include 'vclose.htm';
} else {
include 'sorry.htm';
}

If the form sends 0000000000, then the 'close.htm' file gets displayed.
This is my first attempt at writing PHP and it's driving me insane -
what am I doing wrong?
--
Steve Loft
Jul 17 '05 #4
I wrote:
I've tried this, and I get the same result. I've even tried putting
string casts in, and I've tried single quotes and double quotes:


Pedro,

Sorry. Sorry. Sorry. I didn't RTFM. I didn't realise what you meant when
you used the "===" comparison. I now have it working with if...elseif...
Thanks very much!
--
Steve Loft
Jul 17 '05 #5
Steve Loft wrote:
I didn't realise what you meant when you used the "===" comparison.
I call that the "very equal" comparison
and similarly, the "!==" I call the "very different" comparison :)

so I read this
if ($a === false) whatever();

as "if a is very false" do whatever
I now have it working with if...elseif...
Thanks very much!


You're welcome
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #6
I noticed that Message-ID:
<c2*************@ID-203069.news.uni-berlin.de> from Pedro Graca
contained the following:
I didn't realise what you meant when you used the "===" comparison.


I call that the "very equal" comparison
and similarly, the "!==" I call the "very different" comparison :)


Not "barely different"?

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #7
"Steve Loft" <st***@nybbles.co.uk> wrote in message
news:7l************@galena.nybbles.co.uk...
I'm trying to use a switch construct to compare the result of a form
with a list of known values and display a different page according to
the submitted string. The problem is that some of the answers I need to
check for are strings of the character zero of varying length:

E.g.
<?php

switch (trim(strtolower($_POST['answer']))):

case "abc":
case "xyz":
case "0":
include 'close.htm';
break;
case "0000000000":
include 'vclose.htm';
break;
default:
include 'sorry.htm';
endswitch;
?>

What happens is that a submission consisting of a string of any number
of zeroes cause the first page to be displayed rather than the second. I
thought this was because it was treating it as an integer 0, so I tried
putting (string) casts all over the place, which didn't help.

Could someone please tell me how to achieve what I want?
--
Steve Loft


just a hunch, a bandaid if you will. I have this problem in vb apps with
keys, keys that start with numbers are not allowed in collections, so I
preface them.

switch "_" . (trim(strtolower($_POST['answer']))):
{
case "_0":

case "_0000000000":
}

--
Mike Bradley
http://www.gzentools.com -- free online php tools
Jul 17 '05 #8

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

Similar topics

7
2452
by: Vladimir Lushnikov | last post by:
Hi, I have just started learning C++ and have been trying to do simple string manipulation. However, when calling this function: int helloString() { string name; cout << "Please enter...
8
2536
by: rdlebreton | last post by:
Hi, Folks! I've been trying to develop my own version of these draggable layers and I have been limiting myself to IE6...for now. I have looked at some other examples to get ideas of creating...
5
8601
by: Paolo | last post by:
We are experiencing a problem with COleDateTime while migrating our MFC&ATL project from VC++6.0 to VC++7.1 (Visual Studio .NET 2003). COleDateTime has a DATE conversion operator that in 7.1 asserts...
11
1531
by: Fernando Barsoba | last post by:
Hi all, I very much need your help here guys.. I'm working on a IPSec implementation, and when almost finished, I found a considerable problem. I'm sending a particular array + a key to a...
1
599
by: Frederik Vanderhaegen | last post by:
Hi, I'm trying change the layout of a combobox when f.e. a certain property is set to true. My problem is the following: when I execute the code, the combobox is repainted but there always...
27
2272
by: John Salerno | last post by:
Ok, here's a problem I've sort of assigned to myself for fun, but it's turning out to be quite a pain to wrap my mind around. It's from a puzzle game. It will help if you look at this image: ...
7
1984
by: cytec123187 | last post by:
Hello, I am working on an Adobe Acrobat file that uses javascript for calculations. I am trying to create a field that uses two other fields to determine a number value. I think this requires...
4
2357
by: Gaijinco | last post by:
I'm not quite sure why this code doesn't works: #include <iostream> #include <algorithm> #include <vector> #include <string> using namespace std; class word {
43
2311
by: John | last post by:
Hi This .net is driving me crazy!! In VB6 I had a type which contained a couple of multi-dimentional arrays which i used to create and read records: Type AAA : Array1(10,10,2) as Integer
0
7070
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7316
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...
1
6976
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
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5566
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,...
1
4993
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
729
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.