Jump to content

isset


dan_t

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/147096-isset/
Share on other sites

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*

Link to comment
https://forums.phpfreaks.com/topic/147096-isset/#findComment-772239
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/147096-isset/#findComment-772253
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/147096-isset/#findComment-772381
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.