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

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($HomePhone1, 0, 1) == "0" || substr($HomePhone2, 0, 1) ==
"0" || substr($HomePhone3, 0, 1) == "0" || substr($BusinessPhone1, 0,
1) == "0" || substr($BusinessPhone2, 0, 1) == "0" ||
substr($BusinessPhone3, 0, 1) == "0" || substr($CellPhone1, 0, 1) ==
"0" || substr($CellPhone2, 0, 1) == "0" || substr($CellPhone3, 0, 1)
== "0"){
header("Location: /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 4025
Eric Linders <el********@hotmail.com> wrote:
if (substr($HomePhone1, 0, 1) == "0" || substr($HomePhone2, 0, 1) ==
"0" || substr($HomePhone3, 0, 1) == "0" || substr($BusinessPhone1, 0,
1) == "0" || substr($BusinessPhone2, 0, 1) == "0" ||
substr($BusinessPhone3, 0, 1) == "0" || substr($CellPhone1, 0, 1) ==
"0" || substr($CellPhone2, 0, 1) == "0" || substr($CellPhone3, 0, 1)
== "0"){
header("Location: /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_globals 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($HomePhone1, 0, 1) == "0" || substr($HomePhone2, 0, 1) ==
"0" || substr($HomePhone3, 0, 1) == "0" || substr($BusinessPhone1, 0,
1) == "0" || substr($BusinessPhone2, 0, 1) == "0" ||
substr($BusinessPhone3, 0, 1) == "0" || substr($CellPhone1, 0, 1) ==
"0" || substr($CellPhone2, 0, 1) == "0" || substr($CellPhone3, 0, 1)
== "0"){
header("Location: /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********@hotmail.com> wrote in message
news:49**************************@posting.google.c om...
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($HomePhone1, 0, 1) == "0" || substr($HomePhone2, 0, 1) ==
"0" || substr($HomePhone3, 0, 1) == "0" || substr($BusinessPhone1, 0,
1) == "0" || substr($BusinessPhone2, 0, 1) == "0" ||
substr($BusinessPhone3, 0, 1) == "0" || substr($CellPhone1, 0, 1) ==
"0" || substr($CellPhone2, 0, 1) == "0" || substr($CellPhone3, 0, 1)
== "0"){
header("Location: /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
($BusinessPhone1 !== null and preg_match("/^0/", $BusinessPhone1)) or
($BusinessPhone2 !== null and preg_match("/^0/", $BusinessPhone2)) or
($BusinessPhone3 !== 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
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...
3
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...
3
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...
7
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...
3
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...
3
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...
8
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...
0
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...
18
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
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,...
0
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
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
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...

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.