-1

How do the following two function calls compare to check if a variable exists for a GET call or a POST call ?

if(isset($_GET['var']))
if($_GET['var'])

Both do their job but sometimes without isset() i get a warning :

PHP Notice: Undefined index: var on at Line XX

Update It was a $_GET not $GET. Also i know it has nothing to do with GET/POST. All i am discussing is variable check instead of HTTP method. Thanks.

CodeMonkey
  • 2,265
  • 9
  • 48
  • 94

1 Answers1

1

$_GET[''] is usually retrieved from the browser, so if you had something like the following: www.hello.com/index.php?var=hello

You could use:

if(isset($_GET['var'])) {
echo $_GET['var']; // would print out hello
}

Using isset() will stop the undefined index error, so if you just had www.hello.com/index.php for example then you would not see the error even though the var is not set.

Posts are usually when one page posts information to another, the method is the same but using $_POST[''] instead of $_GET['']. Example you have a form posting information:

<form method="post" action="anotherpage.php">
    <label></label>
    <input type="text" name="text">
</form>

In anotherpage.php to get the information would be something like:

$text = isset($_POST['text']);
echo $text; // would echo what ever you input into the text field on the other page

In a nut shell, just putting $_GET['name'] if name is not set you will get an error. Using isset($_GET['name']) will check is the var name has any value before continuing.

Not sure if this is what you were after, but from the question this is my best guess at an answer

Adam
  • 455
  • 1
  • 3
  • 18
  • I was looking for isset vs not using isset. GET vs POST was not a part of the question at all. :) Thansk for answering. – CodeMonkey Aug 31 '15 at 00:28