473,770 Members | 1,826 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

if statement always evaluates to true

Hello,

We have a form that is collecting user input. There are three fields
for each phone number (area code, prefix and suffix). I'm trying to
use a simple if statement to confirm if the first character in any of
the fields is "0". If it is, then I just redirect them to a different
page because it's someone that is just messing around. Unfortunately,
whenever the form loads, the if statement automatically evaluates to
true and the user always gets sent to the other page, even when they
have not typed anything in the form. Below is the code for the if
statement. if I comment out the header line, the form loads. Any help
would be greatly appreciated!

if (substr($HomePh one1, 0, 1) == "0" || substr($HomePho ne2, 0, 1) ==
"0" || substr($HomePho ne3, 0, 1) == "0" || substr($Busines sPhone1, 0,
1) == "0" || substr($Busines sPhone2, 0, 1) == "0" ||
substr($Busines sPhone3, 0, 1) == "0" || substr($CellPho ne1, 0, 1) ==
"0" || substr($CellPho ne2, 0, 1) == "0" || substr($CellPho ne3, 0, 1)
== "0"){
header("Locatio n: /otherpage.php") ;
}

BTW, I tried it with the zero as a number instead of a string and it
still worked incorrectly.

Thank you for any help.
- Eric
Jul 17 '05 #1
3 4044
Eric Linders <el********@hot mail.com> wrote:
if (substr($HomePh one1, 0, 1) == "0" || substr($HomePho ne2, 0, 1) ==
"0" || substr($HomePho ne3, 0, 1) == "0" || substr($Busines sPhone1, 0,
1) == "0" || substr($Busines sPhone2, 0, 1) == "0" ||
substr($Busines sPhone3, 0, 1) == "0" || substr($CellPho ne1, 0, 1) ==
"0" || substr($CellPho ne2, 0, 1) == "0" || substr($CellPho ne3, 0, 1)
== "0"){
header("Locatio n: /otherpage.php") ;
}


Couple of remarks
- to get the first character from a string: $HomePhone1{0}
- "0" evaluates to false, you should use === IMHO, now an undefined
variable will return false in substr and thus trigger the if
- grouping in the if, in your current code only 1 "0" is enough to
qualify:
if
(
($HomePhone1{0} ==="0" && $HomePhone2{0}= =="0" && $HomePhone3{0]==="0")
||
($BusiPhone1{0} ==="0" && $BusiPhone2{0}= =="0" && $BusiPhone3{0]==="0")
)
- Where is $HomePhone1 etc coming from? Make sure they are in _GET/_POST

--

Daniel Tryba

Jul 17 '05 #2
I'm guessing that $HomePhone1 is not defined. (substr() will evaluate to
false, as will "0", hence the condition will be satisified.) You need to use
$_GET['HomePhone1'] or $_POST['HomePhone1'] depending on how you submit the
form. See the 20,000 register_global s warnings in the manual for more
information.

hth
// Ian Fette
// Proponent, comp.lang.php

Eric Linders wrote:
Hello,

We have a form that is collecting user input. There are three fields
for each phone number (area code, prefix and suffix). I'm trying to
use a simple if statement to confirm if the first character in any of
the fields is "0". If it is, then I just redirect them to a different
page because it's someone that is just messing around. Unfortunately,
whenever the form loads, the if statement automatically evaluates to
true and the user always gets sent to the other page, even when they
have not typed anything in the form. Below is the code for the if
statement. if I comment out the header line, the form loads. Any help
would be greatly appreciated!

if (substr($HomePh one1, 0, 1) == "0" || substr($HomePho ne2, 0, 1) ==
"0" || substr($HomePho ne3, 0, 1) == "0" || substr($Busines sPhone1, 0,
1) == "0" || substr($Busines sPhone2, 0, 1) == "0" ||
substr($Busines sPhone3, 0, 1) == "0" || substr($CellPho ne1, 0, 1) ==
"0" || substr($CellPho ne2, 0, 1) == "0" || substr($CellPho ne3, 0, 1)
== "0"){
header("Locatio n: /otherpage.php") ;
}

BTW, I tried it with the zero as a number instead of a string and it
still worked incorrectly.

Thank you for any help.
- Eric

Jul 17 '05 #3

"Eric Linders" <el********@hot mail.com> wrote in message
news:49******** *************** ***@posting.goo gle.com...
Hello,

We have a form that is collecting user input. There are three fields
for each phone number (area code, prefix and suffix). I'm trying to
use a simple if statement to confirm if the first character in any of
the fields is "0". If it is, then I just redirect them to a different
page because it's someone that is just messing around. Unfortunately,
whenever the form loads, the if statement automatically evaluates to
true and the user always gets sent to the other page, even when they
have not typed anything in the form. Below is the code for the if
statement. if I comment out the header line, the form loads. Any help
would be greatly appreciated!

if (substr($HomePh one1, 0, 1) == "0" || substr($HomePho ne2, 0, 1) ==
"0" || substr($HomePho ne3, 0, 1) == "0" || substr($Busines sPhone1, 0,
1) == "0" || substr($Busines sPhone2, 0, 1) == "0" ||
substr($Busines sPhone3, 0, 1) == "0" || substr($CellPho ne1, 0, 1) ==
"0" || substr($CellPho ne2, 0, 1) == "0" || substr($CellPho ne3, 0, 1)
== "0"){
header("Locatio n: /otherpage.php") ;
}

You need to check that the form variables are set. Do something like:

$HomePhone1 = isset($_POST['HomePhone1']) ? $_POST['HomePhone1'] : null;
$HomePhone2 = isset($_POST['HomePhone2']) ? $_POST['HomePhone2'] : null;
$HomePhone3 = isset($_POST['HomePhone3']) ? $_POST['HomePhone3'] : null;
$BusinessPhone1 = isset($_POST['BusinessPhone1 ']) ? $_POST['BusinessPhone1 ']
: null;
$BusinessPhone2 = isset($_POST['BusinessPhone2 ']) ? $_POST['BusinessPhone2 ']
: null;
$BusinessPhone3 = isset($_POST['BusinessPhone3 ']) ? $_POST['BusinessPhone3 ']
: null;
$CellPhone1 = isset($_POST['CellPhone1']) ? $_POST['CellPhone1'] : null;
$CellPhone2 = isset($_POST['CellPhone2']) ? $_POST['CellPhone2'] : null;
$CellPhone3 = isset($_POST['CellPhone3']) ? $_POST['CellPhone3'] : null;

Then you need to rework your if statement. You are using the bitwise or
operator. The logical or operator in PHP is the keyword "or". And also,
using regex would be a much more reliable method:

if(($HomePhone1 !== null and preg_match("/^0/", $HomePhone1)) or
($HomePhone2 !== null and preg_match("/^0/", $HomePhone2)) or
($HomePhone3 !== null and preg_match("/^0/", $HomePhone3)) or
($BusinessPhone 1 !== null and preg_match("/^0/", $BusinessPhone1 )) or
($BusinessPhone 2 !== null and preg_match("/^0/", $BusinessPhone2 )) or
($BusinessPhone 3 !== null and preg_match("/^0/", $BusinessPhone3 )) or
($CellPhone1 !== null and preg_match("/^0/", $CellPhone1)) or
($CellPhone2 !== null and preg_match("/^0/", $CellPhone2)) or
($CellPhone3 !== null and preg_match("/^0/", $CellPhone3)))
echo "There's a zero in a number!";
else
echo "There're no zeroes here!";
This means that if ANY of the numbers submitted contain a "0" as their first
character, then this code will echo "There's a zero in a number!". If there
are no leading zeroes, it will echo "There're no zeroes here!"
HTH.

Plankmeister.
Jul 17 '05 #4

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

Similar topics

8
2194
by: NotGiven | last post by:
I have the code below that always evaluates to true. Why and what do I do about it? Many thanks in advance! In the code, I have tried sending in the URL ?indentNum=23 ?indentNum=23.2 ?indentNum=23sadkjsi8 ?indentNum=aanns No matter what I throw at it, it always evaluates to true despite echo'ing
3
1895
by: Roy Adams | last post by:
Hello everyone I'm doing a multiple insert from ten text fields. all named color when I submit the from with the text fields it goes to an asp page with the script to do the job and the go to another page, I've got that bit going but what if the user leaves the fields empty an hits submit? ok a simple enough thing of putting an else in the script, this isn't working!! it always does the first thing I've tried so many different...
3
1740
by: Mark Morton | last post by:
I'm writing an if statement for a UK credit card form validation script. Users who specify that their card is Switch need to enter either the issue number or the 'valid from' date. I'm trying to write an if statement so that if: 1. The word "Month" is in the validFromMonth field (where they haven't selected a month a the drop-down menu) or the word "Year" is in the validFromYear field (where they haven't selected a year another the...
7
11445
by: mark | last post by:
Access 2000: I creating a report that has a record source built by the user who selects the WHERE values. An example is: SELECT * FROM CHARGELOG WHERE STDATE Between #10/27/2003# And #11/2/2003# And VehicleID='00000000' And BattID='LKO500HF'. I need to use the records returned to populate text boxes, but the data requires further manipulation. I attempting to use expressions in the control source
3
4611
by: Andy_Khosravi | last post by:
I have been trying to build a user friendly search engine for a small database I have created. I'm having some particular problems with one of my date fields. Here's the setup: I'm using Access 97 (I know it's old. But, it's the tool they give me to work with) My working knowledge of SQL is on the low side. My working knowledge of VBA is beginner.
3
1563
by: tconkling | last post by:
I have an if statement that looks like this: if(foo(&x) && x > y) ... where the value of x is modified by foo, and the comparison between x and y only makes sense after x has been modified by foo (and, of course, if foo returns true). Am I guaranteed (assuming my compiler generates correct code) that x > y is evaluated after foo(&x) returns?
8
1266
by: GaryFe | last post by:
Can anyone tell me why this statement doesn't work? for (int at=0;at==-1;at=msg.IndexOf("\n",at+1)) n++; All I'm trying to do is count the number of newlines in a string. When I step through it in the debugger, it highlights the first statement (int at=0), then highlights the second statement, then jumps to the next line of code. It doesn't appear to increment n at all, nor does it appear to ever evaluate the 3rd statement of the for...
0
2273
by: peter.bittner | last post by:
I have developed a Windows application in Visual Studio .NET 2003 and created a Setup project for it. In the File System Editor I have added a shortcut to the User's Desktop folder to point to the application once it has been installed. I have added a condition to the User's Desktop named "DESKTOPSHORTCUT" (I have used several names, this is just the latest). In the User interface, I have added a screen with a checkbox that asks the user...
18
7975
by: dspfun | last post by:
Hi! The words "expression" and "statement" are often used in C99 and C- textbooks, however, I am not sure of the clear defintion of these words with respect to C. Can somebody provide a sharp defintion of "expression" and "statement"? What is the difference between an expression and a statement?
0
10257
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
10099
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
10037
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
9904
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
8931
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7456
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
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
4007
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
3
2849
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.