Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Evaluating php form variable existance - 0 value

Posted on 2003-11-17
1
223 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

829 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