Jump to content

Notice: Undefined index: test


guybrown
Go to solution Solved by 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
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"] : '';
Link to comment
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"] : '';

Thanks Mac_gyver! I'll try this.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.