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.
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)
Closed 4 years ago.
i had a problem in this line, because the error message tell me theres an undefined index:
$strSQL = " UPDATE place SET
rating_value = '".$_POST["rating_value"]."'
WHERE place_id = '".$_POST["place_id"]."'
";
pls help me if you have a solution. thanks
Do you check your variables before do the request like :
if( !empty($_POST["rating_value"]) && !empty($_POST["place_id"]) )
{
// your request
} else {
echo("error");
}
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 6 years ago.
What I want to do is echo the value of $test on my page, through the uri. I know I'm doing something wrong, but I can't figure out what.
Code:
<?php echo $test?>
URL:
localhost/testMap/test.php?test=hallo
You can get the value of url with a get, and store it in the variable $test and then echo that out.
$test = $_GET['test'];
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 6 years ago.
I am getting an undefined index error on this line of code:
switch ($_REQUEST["___p"])
I think I would need to declare the variable. What would I change the above line to?
Either make sure exists beforehand with isset() or just ignore it:
switch (#$_REQUEST["___p"])