Solved

Evaluating php form variable existance - 0 value

Posted on 2003-11-17
1
219 Views
Last Modified: 2008-03-06
Hi,

I have just created a form such as a questionaire that emails a response to someone.
On this form I have several fields that are compulsory to fill in such as forename.

Currently I am checking to see if forename does not exists by using:

if (!forename) {
$ErrorNo = 1;
}

This code should set $ErrorNo to 1 when the forename text field of a form is left blank.

This works absolutly fine, but in the event someone puts the value 0 instead of a string into the forename text box on the form, the code above assigns $ErrorNo to equal 1.

In visual basic I think I would get round this by converting the variable to a string (toString) so integer 0 is recognised as the string 0. ('0')

I tried to create another variable txtforename to equal the following:
$txtforename = (string)  "" . $forename;

I hoped that I could then evalueate this variable using:
if (!txtforename) {
$ErrorNo = 1;
}

but I still get the same problem when $forename = 0

I know some of you may be thinking I shouldn't allow 0 as a name any way, but on my form I have fields that should allow this value and also I would like to solve this problem for future reference.

I would be greatful if you can help out with my problem - I've been stuck on it for quite a while now :(

Cheers,
Si.



0
Comment
Question by:BigSi
1 Comment
 
LVL 13

Accepted Solution

by:
lozloz earned 60 total points
ID: 9763235
hi,

there are probably lots of ways to do this, you could either check whether the value == 0, or if it's an integer first

if (empty($forename)) {
  if($forename != 0) {
    $ErrorNo = 1;
  }
}

or

if (empty($forename)) {
  if(gettype($forename) != "integer") {
    $ErrorNo = 1;
  }
}

loz
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now