Jump to content

Notice: Undefined index: test


guybrown

Recommended Posts

Hi all

 

I am doing a video tutorial on GET but I get a 'notice' that the tutor doesn't:

 

Notice: Undefined index: test in C:\xampp\htdocs\database\vartest2.php on line 2

 

Can I switch notifications like this off?

 

Here is the php code:

 

<?php
$var = $_GET["test"];
$len = strlen($var);
 
if ($len > 0)
{
echo $len;
}
else
{
echo "error: no input";
}
?>
<form action="vartest2.php" method="GET">
<input type="text" name="test">
<input type="submit" value="Submit">
</form>

 

This is what the tutor and I are entering into our browsers:

 

http://localhost/database/vartest2.php

 

(If I put ?test=anything at the end of the above, I don't get the notification but can't you just not specify ?test=... at the end?)

 

Displayed in my browser:

 

Notice: Undefined index: test in C:\xampp\htdocs\database\vartest2.php on line 2
error: no input

  

 

Displayed on the tutor's browser:

 

error: no input

  

 

I'm using Google Chrome Version 27.0.1453.94 m

 

Any help appreciated.

 

Regards,

 

Guy Brown

Link to comment
https://forums.phpfreaks.com/topic/278600-notice-undefined-index-test/
Share on other sites

you can turn notices off, but you shouldn't, especially when learning php as you will also miss things like typo's and using wrong variable names. you will end up wasting hours of your time trying to find simple mistakes in your code if you hide any of the types of errors.

 

the correct coding for a variable that might not exist is to test if it isset() before using the value. using the short form of an if(){}else{} statement the code would be -

$var = isset($_GET["test"]) ? $_GET["test"] : '';

 

you can turn notices off, but you shouldn't, especially when learning php as you will also miss things like typo's and using wrong variable names. you will end up wasting hours of your time trying to find simple mistakes in your code if you hide any of the types of errors.

 

the correct coding for a variable that might not exist is to test if it isset() before using the value. using the short form of an if(){}else{} statement the code would be -

$var = isset($_GET["test"]) ? $_GET["test"] : '';

Thanks Mac_gyver! I'll try this.

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.