isset() driving me crazy

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
seadog
Forum Newbie
Posts: 2
Joined: Thu Sep 30, 2010 6:07 am

isset() driving me crazy

Post by seadog »

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>';
}
?>
Ketrel
Forum Newbie
Posts: 6
Joined: Tue Sep 28, 2010 4:13 am

Re: isset() driving me crazy

Post by Ketrel »

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 != "")
User avatar
DigitalMind
Forum Contributor
Posts: 152
Joined: Mon Sep 27, 2010 2:27 am
Location: Ukraine, Kharkov

Re: isset() driving me crazy

Post by DigitalMind »

replace $_REQUEST with $_POST
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: isset() driving me crazy

Post by AbraCadaver »

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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: isset() driving me crazy

Post by John Cartwright »

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()
User avatar
DigitalMind
Forum Contributor
Posts: 152
Joined: Mon Sep 27, 2010 2:27 am
Location: Ukraine, Kharkov

Re: isset() driving me crazy

Post by DigitalMind »

BTW I would use isset($_POST['name']) instead of isset($name) in this case
seadog
Forum Newbie
Posts: 2
Joined: Thu Sep 30, 2010 6:07 am

Re: isset() driving me crazy

Post by seadog »

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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: isset() driving me crazy

Post by AbraCadaver »

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.
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() :)
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.
Post Reply