473,656 Members | 2,824 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(strtolowe r($_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 3310
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(somethin g):
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(st rtolower($_POST['answer']));

if ((string)$ans == (string)"0") {
include 'close.htm';
} elseif ((string)$ans == (string)"000000 0000") {
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.nyb bles.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(strtolowe r($_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(strtolowe r($_POST['answer']))):
{
case "_0":

case "_000000000 0":
}

--
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
2459
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 your name:\n";
8
2551
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 an alternative to pop-up windows in a web page. The code I have works (sort of). The problem is that I can move these layers around when I move the mouse slowly, however, if I move it fast the web browser looses track of the motion and leaves the...
5
8626
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 when the date state is not valid. But we may need to pass NULL dates (read from a database) to our COM clients! - Yes, this problem doesn't occur in Release version, but we can't have a Debug version that asserts from time to time :) A solution...
11
1548
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 HMAC-SHA1 function. I did the tests with the following: void icv_Calculation_test(char p_output) { char msgtest="what do ya want for nothing?"; char key="Jefe";
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 stays an area unpainted. After soms research I discovered that a combobox also contains a textbox. With the windows api function GetComboBoxInfo I can retrieve a handle to that textbox. When I create a graphics object with that handle and try to...
27
2296
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: http://www.johnjsal.devisland.net/switches.jpg Here's the situation: Each of the four rows in the diagram is considered a single 'panel'. Each panel has eight 'switches', which are composed of two columns each, and these columns have a total of 20...
7
1992
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 a switch within a switch, but I am not sure how to do this, as I am a beginner when it comes to javascript. In esscense, I want to set up a code so that each case of value A is calculated at each case of value B. Example:
4
2376
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
2356
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
8382
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8297
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8816
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8717
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8498
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8600
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5629
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2726
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 we have to send another system
2
1600
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.