468,272 Members | 2,043 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,272 developers. It's quick & easy.

if statement

I am trying to remember how you write an if statement to check if any value
has been entered. For example seeing if a username has been entered in form.
I have seen the following:

if ($username == '')
{
print ("You've pressed submit without entering your username");
}

However is there another method which uses an exclamation mark? I am sure I
have seen something for where $username has no value but I can't think what
it was.

Cheers

Phil


Nov 6 '06 #1
7 1118
ph********@f-in-stupid.co.uk says...
I am trying to remember how you write an if statement to check if any value
has been entered. For example seeing if a username has been entered in form.
I have seen the following:

if ($username == '')
{
print ("You've pressed submit without entering your username");
}

However is there another method which uses an exclamation mark? I am sure I
have seen something for where $username has no value but I can't think what
it was.
isSet ??

if (!isSet($username)) { // do this if $username is not set

GM

Nov 6 '06 #2
>I am trying to remember how you write an if statement to check if any value
>has been entered. For example seeing if a username has been entered in form.
The variable $_GET['username'] or $_POST['username'] is where the
input from a form element appears in a modern version of PHP, not $username.
>I have seen the following:

if ($username == '')
{
print ("You've pressed submit without entering your username");
}

However is there another method which uses an exclamation mark? I am sure I
have seen something for where $username has no value but I can't think what
it was.
You can use isset($_GET['username']) to determine if the value was given
at all.
Nov 6 '06 #3
You can use isset($_GET['username']) to determine if the value was given
at all.
Yep. That was it. Thanks.

Cheers

Phil
Nov 6 '06 #4
isSet ??
>
if (!isSet($username)) { // do this if $username is not set

GM
Yep. That was it. Thanks.

Cheers

Phil
Nov 6 '06 #5
"Geoff Muldoon" <ge***********@trap.gmail.comwrote in message
news:MP************************@news.readfreenews. net...
ph********@f-in-stupid.co.uk says...
>I am trying to remember how you write an if statement to check if any
value
has been entered. For example seeing if a username has been entered in
form.
I have seen the following:

if ($username == '')
{
print ("You've pressed submit without entering your username");
}

However is there another method which uses an exclamation mark? I am sure
I
have seen something for where $username has no value but I can't think
what
it was.

isSet ??

if (!isSet($username)) { // do this if $username is not set

If this is form validation, then isset won't do any good. The form field
will be set, but it still may be empty. Better test that strlen() is greater
than zero, or use empty(). And Of course, there's always the $username vs.
$_POST['username'] issue. Seems like someone is using register_globals, for
shame...

--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi.net | rot13(xv***@bhgbyrzcv.arg)
Nov 6 '06 #6
If this is form validation, then isset won't do any good. The form field
will be set, but it still may be empty. Better test that strlen() is
greater than zero, or use empty(). And Of course, there's always the
$username vs. $_POST['username'] issue. Seems like someone is using
register_globals, for shame...
Not using register_globals but wish I had noticed your post earlier
regarding the form validation issue. I assumed isset meant that it checked
to see if it is actually set to something (had a value), not that it merely
existed. Oh well.. it's all good learning.

Cheers

Phil
Nov 7 '06 #7
>Not using register_globals but wish I had noticed your post earlier
>regarding the form validation issue. I assumed isset meant that it checked
to see if it is actually set to something (had a value), not that it merely
existed. Oh well.. it's all good learning.
A zero-length string is a real value. Whether or not you consider
it to be an error, an unspecified value, or an explicitly specified
value (the value when displaying the form might NOT be an empty
string, it might be 'No Middle Initial' or the original value from
the database record you're editing) depends on the variable,
what it means, and the application.
Nov 7 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

28 posts views Thread by Fábio Mendes | last post: by
13 posts views Thread by eman1000 | last post: by
37 posts views Thread by Steven Bethard | last post: by
18 posts views Thread by Steven Bethard | last post: by
28 posts views Thread by Steven Bethard | last post: by
7 posts views Thread by Steven Bethard | last post: by
18 posts views Thread by dspfun | last post: by
23 posts views Thread by florian.loitsch | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.