This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 8 years ago.
with this code:
$tstUsername = getValue($_POST['tstUsername']);
$tstPassword = getValue($_POST['tstPassword']);
if ($tstUsername !== false && $tstPassword !== false) {
echo "New string added to database!";
haydayshops_mysql_query($conn,"INSERT INTO table_accounts (username, password) VALUES >('$tstUsername','".sha1($tstPassword)."')");
}
The code works fine, but when i put the error mode on, i see this nasty errors:
Notice: Undefined index: tstUsername in ...
Undefined index: checkPassword in ...
You get these errors because you're trying to access to key of an array which doesn't exists. Here, you access to $_POST['tstUsername'] but it doesn't seems to exists in the $_POST data. Same case with a key named checkPassword somewhere else in your code.
To avoid that, use isset() function to check if they are declared before trying to access to the value.
Related
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 3 years ago.
I am looking at my 'WP_DEBUG' errors and the following two errors/notices pop up in a number of PHP files:
Notice: Undefined variable: defaultUI in...
Notice: Undefined variable: compileShortcodeUI in...
I have checked all of the the PHP files and lines specifically and every single one of these refers to this same bit of code:
$compileShortcodeUI .= "<div class='whatInsert whatInsert_".$shortcodeName."'>".$defaultUI."</div>";
What do I need to change to remove these errors?
You have to define the variable initially.
$compileShortcodeUI = ''; // null or any default value
$defaultUI = ''; // null or any default value
$compileShortcodeUI .= "<div class='whatInsert whatInsert_".$shortcodeName."'>".$defaultUI."</div>";
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
PHP check value against multiple values with OR-operator
(3 answers)
Closed 4 years ago.
Hi,
I have this code:
if($_GET['s']=="page1" || $_GET['s']=="page2" || $_GET['s']=="page3") {
dosomething();
}
and I get this error: : Undefined index: s in
Which I can dismiss only by adding this line:
$_GET['s']="";
but then this wont execute the code correctly since $_GET['s'] is not supposed to have any initial value. How do I fix this other than disabling the notices and errors?
Thank you.
You can check your $_GET['s']
if(isset($_GET['s'])) {
// your code goes here...
}
isset() is used to check if the index exists.
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 4 years ago.
I've been getting this error. Any fix??
PHP Notice: Undefined offset: 1 in line 402
if ($argv[1] == '--cover')
{
$Modules = new Modules;
$Modules->cover();
die();
}
The line is if ($argv[1] == '--cover')
An offset is undefined if it doesn't exist in the array.
Try this:
if(isset($argv[1]) && $argv[1] == '--cover'){
$Modules = new Modules;
$Modules->cover();
die();
This error means that the $argv[1] is undefined or null. Make sure this variable is properly assign or have a value before making a condition.
using print_r($argv); before making a condition will help you if the variable have a value or not.
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
I want to check in my If-Statement if the status is 1(true) or 0(false). The value is stored in a mySQL DB with tinyint.
The following code returns an error:
if($zeile['STATUS']=='1')
{
echo "<td align='center'>true;</td>";
}
else{
echo "<td align='center'>false;</td>";
}
The error is:
Notice: Undefined index: STATUS in /home/u123210707/public_html/index.php on line 49'. line 49
is my If-statement
if($zeile['STATUS'] > 0)
{
/* whatever */
}
In your $zeile array there is no element like STATUS, that's why its give this error,
i think before if condition you must have to check array using print_r and check there is an element called STATUS.
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 8 years ago.
When I run
if (filter_var($_GET['abc'], FILTER_VALIDATE_URL)) { ... }
and I don't give a ?abc= extension to my .php then I get notice:
Notice: Undefined index: abc in line...
Is there a way to bypass this notice and say if $_GET exists and then is abc then...
Thanks a lot
You have to test if key abc in GET exists, so:
if (isset($_GET['abc']) && filter_var($_GET['abc'], FILTER_VALIDATE_URL)) { ... }
you have to check whether the index is set using isset
if (isset($_GET['abc']) && filter_var($_GET['abc'], FILTER_VALIDATE_URL))
if (isset($_GET['abc']) && filter_var($_GET['abc'], FILTER_VALIDATE_URL)) { ... }
This way it checks if that exist and you won't get a notice , hope this help :)