I have a very simple script using isset() and the logic just does not work using PHP 5.3.1 and 5.2.9 on my localhost and website respectively.
Whatever I do, isset() always seems to return true.
I am new to PHP programming but otherwise not a beginning programmer but I presume I must be making a beginners mistake though am following an example from a book on PHP.
Can anyone help?
The html code and php script are:
<form action="form.php" method="post">
<fieldset><legend>Enter Name:</legend>
<p><b>Name:</b>
<input type="text" name="name" size="20" maxlength="40" /></p>
</fieldset>
<input type="submit" name="submit" value="Submit" />
</form>
// file form.php
<?php
$name = NULL;
$name = $_REQUEST['name'];
if (isset($name))
{
echo '<p>isset was true for $name</p>';
}
else
{
echo '<p>isset was false for $name</p>';
}
?>
isset() driving me crazy
Moderator: General Moderators
Re: isset() driving me crazy
two things to try
1. instead of setting to null
try
unset($name);
2. if that still doesn't work
try instead of isset
if ($name != "")
1. instead of setting to null
try
unset($name);
2. if that still doesn't work
try instead of isset
if ($name != "")
- DigitalMind
- Forum Contributor
- Posts: 152
- Joined: Mon Sep 27, 2010 2:27 am
- Location: Ukraine, Kharkov
Re: isset() driving me crazy
replace $_REQUEST with $_POST
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: isset() driving me crazy
If you submit that form then $_POST['name'] will always be set, so if you do $name = $_POST['name'] then $name is set. If you set $name = NULL then $name is set. Are you trying to see if it is empty? If so use !empty($name).
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: isset() driving me crazy
If you are setting the variable just prior to checking whether or not it's set, of course it's going to return true.
The behavior your looking for is in empty()
The behavior your looking for is in empty()
- DigitalMind
- Forum Contributor
- Posts: 152
- Joined: Mon Sep 27, 2010 2:27 am
- Location: Ukraine, Kharkov
Re: isset() driving me crazy
BTW I would use isset($_POST['name']) instead of isset($name) in this case
Re: isset() driving me crazy
Thanks for all the replies.
I decided to use strlen() in the end since I am familiar with C and it seems that PHP uses similar methods from what I have seen so far.
I decided to use strlen() in the end since I am familiar with C and it seems that PHP uses similar methods from what I have seen so far.
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: isset() driving me crazy
Then I guess you don't care if someone just puts a bunch of spaces in the field? If you do care then you'd have to trim() it first, might as well just use empty()seadog wrote:Thanks for all the replies.
I decided to use strlen() in the end since I am familiar with C and it seems that PHP uses similar methods from what I have seen so far.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.