dan_t Posted February 27, 2009 Share Posted February 27, 2009 Hi everyone, I've got another basic question. The "isset" means you have your variable defined right? When I use it, it seems to do the opposite of what I would expect it to do. Here's my example: <?php $name = "Danielson"; if (isset($_POST['$name'])) { echo ("<h2> Hi, my name is " .$name.".</h2> <br />"); } else { echo ("I'm confused!"); } ?> It echo's "I'm confused". But with "!isset" it echo's"Hi, my name is Danielson. Why? Shouldn't it be just the opposite? Quote Link to comment https://forums.phpfreaks.com/topic/147096-isset/ Share on other sites More sharing options...
phpdragon Posted February 27, 2009 Share Posted February 27, 2009 try this (isset($name)) you have already defined the variable, the POST method is for posting passed variable from another page Quote Link to comment https://forums.phpfreaks.com/topic/147096-isset/#findComment-772234 Share on other sites More sharing options...
dan_t Posted February 27, 2009 Author Share Posted February 27, 2009 Do you mean, remove the ['name'] part? Quote Link to comment https://forums.phpfreaks.com/topic/147096-isset/#findComment-772236 Share on other sites More sharing options...
samshel Posted February 27, 2009 Share Posted February 27, 2009 he means $name is different than $_POST['name']. $name is a variable u have defined, $_POST['name'] is a value which will come after u submit a form. Quote Link to comment https://forums.phpfreaks.com/topic/147096-isset/#findComment-772238 Share on other sites More sharing options...
opoohwan Posted February 27, 2009 Share Posted February 27, 2009 By asking if $_POST['$name'] is set you are asking for php to pull something from a form that you sent using a post variable. instead you are defining the variable on the same page. try this. <?php $name = "Danielson"; if (isset($name)) { echo ("<h2> Hi, my name is " .$name.".</h2> <br />"); } else { echo ("I'm confused!"); } *looks like phpdragon beat em to it and explained it better* Quote Link to comment https://forums.phpfreaks.com/topic/147096-isset/#findComment-772239 Share on other sites More sharing options...
dan_t Posted February 27, 2009 Author Share Posted February 27, 2009 That worked. If I used it with a form, would that work? Or do you ever isset $_POST? Quote Link to comment https://forums.phpfreaks.com/topic/147096-isset/#findComment-772247 Share on other sites More sharing options...
opoohwan Posted February 27, 2009 Share Posted February 27, 2009 You can define the variable through a $_POST. Say you had a form that collected the name of the user, and you wanted to call the user by name. you could pass the information from the form to the next php page using $_POST. Quote Link to comment https://forums.phpfreaks.com/topic/147096-isset/#findComment-772250 Share on other sites More sharing options...
phpdragon Posted February 27, 2009 Share Posted February 27, 2009 If you send the information from a form then you need to use $_POST, it doesnt matter if the form is on the same page and it simply reloads itself when submitted, $_POST is the method for retreiving this variable if you define a variable then you call it directly eg $name=$_POST['name']; would set $name to the name submitted in the form, you then retreive it by just calling $name in php tags. if you set $name='John'; then everywhere you echo $name John would appear Quote Link to comment https://forums.phpfreaks.com/topic/147096-isset/#findComment-772253 Share on other sites More sharing options...
Q695 Posted February 27, 2009 Share Posted February 27, 2009 Hasn't "if (isset($_POST['$name']))" been depreciated to "if ($_POST['$name'])" Quote Link to comment https://forums.phpfreaks.com/topic/147096-isset/#findComment-772375 Share on other sites More sharing options...
kenrbnsn Posted February 27, 2009 Share Posted February 27, 2009 No. Also, I wouldn't use <?php if ($_POST['field'])) ?> to see if a field has a value. This test can return a false negative if the value entered is 0 (zero). Run this example to see the results of the different tests: <?php if (isset($_POST['submit'])) { echo '<pre>' . print_r($_POST,true) . '</pre>'; if (isset($_POST['name'])) echo '$_POST[\'name\'] is set<br>'; if ($_POST['name']) echo '$_POST[\'name\'] is TRUE<br>'; if (strlen(trim($_POST['name'])) > 0) echo "\$_POST['name'] has a value of <span style='font-weight:bold;color:red'>" . $_POST['name'] . '</span><br><br>'; } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title></title> </head> <body> <form method="post" action=""> Name: <input name="name" type="text"><br> <input type="submit" name="submit" value="Test It"> </form> </body> </html> Ken Quote Link to comment https://forums.phpfreaks.com/topic/147096-isset/#findComment-772381 Share on other sites More sharing options...
haku Posted February 27, 2009 Share Posted February 27, 2009 Hasn't "if (isset($_POST['$name']))" been depreciated to "if ($_POST['$name'])" No. Because if it hasn't been set, it throws up an error. Quote Link to comment https://forums.phpfreaks.com/topic/147096-isset/#findComment-772423 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.