This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”
Undefined index in PHP
Notice: Undefined index: NAME in C:\xampp\htdocs\pmsx\lib\config.php on line 107
That's the exact error. Here's my code on the file. Thanks for those will help.
here's my line 107:
//echo "<br>" . $cdata;
// create the proper tree node if NAME attribute is set
if ($this->attr["NAME"] != "")
$this->tags[count($this->tags) - 1] = $this->attr["NAME"];
Pastie link
Use isset() first, to check, whether the property exists at all.
if ( isset( $this->attr["NAME"] ) && ($this->attr["NAME"] != "") ) {
$this->tags[count($this->tags) - 1] = $this->attr["NAME"];
}
You error says, that the property NAME does not exist at all in the array attr!
NAME isn't a defined index. You probably want:
if( isset($this->attr['NAME']) && $this->attr['NAME'] != "" ) {
// ...
What you probably want is !empty($this->attr['NAME']) instead of $this->attr["NAME"]!="". Otherwise it errors out when NAME index is… well… undefined.
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 5 years ago.
I am getting error message :
undefined index:
in my code. Below is the function on the line
if($user['flagged'] == 1){
$flag = "Your last payment has been flagged as NOT RECEIVED.";
}
it says Undefined index: flagged
How can this be fixed
Just check if msg index of global array $_GET is set. For example
if(isset($_GET['msg']) && $_GET['msg']==="success")
I think it will solve your problem.
use isset() to check it first
if(isset($_GET['msg']) && $_GET['msg']==="success"){
$log_prompt = '<span style="color:red">You Have successfully registered. Login Now!</span>';
}
if the first condition isset($_GET['msg']) returned false, it will escape the second part of the if condition and will not print your message.
read more about Logical Operators
When you load the page the URL must me something like this:
localhost/your_page.php?msg=something_value
But your app mustn't return error when in URL there isn't ?msg=value... You have to use isset()
if(isset($_GET['msg']) && $_GET['msg'] == "success")
=== is recommended
if(isset($_GET['msg']) && $_GET['msg'] === "success")
More info: http://php.net/manual/en/language.operators.logical.php
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 always have error for first array in table.
foreach ($status_lines as $status_line) {
$xxx [] = $status_line -> status ;
}
if (count(array_unique($xxx)) == 1 && end($xxx) == 'REJECTED') { ?>
<b class="text-gray"> N / A </b>
<?php }
elseif (count(array_unique($xxx)) == 1 && end($xxx) == 'NOT APPROVED') { ?>
<b class="text-gray"> N / A </b>
<?php }
it resulting : Message: Undefined variable: xxx
but for the second line to the end in table is OK ...
Your variable $xxx has been defined within your foreach block. It is not defined anywhere else.
Define it outside the block as a global variable:
$xxx = array();
Then continue your foreach loop as follows:
foreach ($status_lines as $status_line) {
$xxx[] = $status_line -> status ;
}
...
Define it before use as
$xxx = array();
foreach ($status_lines as $status_line) {
$xxx[] = $status_line -> status ;
}
If you don't declare a new array, and the data that creates / updates the array fails for any reason, then any future code that tries to use the array will warning because the array doesn't exist.
For example, foreach() will throw an error if the array was not declared and no values were added to it. However, no errors will occur if the array is simply empty, as would be the case had you declared it.
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 have made an sms handler system, and everything is okey, working fine.
But I get this error: PHP Notice: Undefined offset: 1
Its in the 17. line $user is the 17. line, I know its just notice, but daily 20-30 "notice" is in my php error log, and I wanted to fix this.
I tryed many different method, but no changes.
Somebody can help to fix it? Thanks!
$conn = sqlsrv_connect($serverName, $connectionInfo);
$id = $_GET["id"];
$from = $_GET["from"];
$to = $_GET["to"];
$msg = explode(" ", $_GET['message']);
$user = substr(trim($msg[1]),0,10);
Viewing this code helps less to understand but still i would recommend you to place
if(isset($msg[1]) && $msg[1] != ''){
$user = substr(trim($msg[1]),0,10);
} else {
$user = '';
}
because it looks like in some cases $msg[1] does not exist. For example if $_GET['message'] = 'Hello';
Well, $_GET['message'] does not seem to contain a second element. Are you sure its set?
Your code should handle this nicely by having an if or sthg similar. Examples:
$user = "anonymous";
if (sizeof($msg)) > 1) {
$user = substr(trim($msg[1]),0,10);
}
how can i hide the php notice by solving the issue.
my code
if(empty($_GET['mode']) && !$_GET['page']) {
include($basepath.'/templates/template.home.php');
}
Notice: Undefined index: page in /var/www/public_html/index.php on line 1
i tried like this
if(empty($_GET['mode']) && !isset($_GET['page']) && !$_GET['page']) {
include($basepath.'/templates/template.home.php');
}
but still showing the
Notice: Undefined index: page in /var/www/public_html/index.php on line 1
how can i solve/fix it ?
The problem is that you use in both statements !$_GET['page'] when using && operator.
When you use !$_GET['page'] in both cases even if you added !isset($_GET['page'] this condition is checked in $_GET['mode'] is empty.
You simple should probably change your statement from:
if(empty($_GET['mode']) && !$_GET['page']) {
include($basepath.'/templates/template.home.php');
}
into
if(empty($_GET['mode']) && !isset($_GET['page'])) {
include($basepath.'/templates/template.home.php');
}
In this case simple if $_GET['page'] is not set (and of course mode is empty) you should include homepage template
Either mode or page isn't present in the array $_GET - so you need to test for it using array_key_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 9 years ago.
Receiving the following:
Notice: Undefined index: func in /Applications/XAMPP/xamppfiles/htdocs/select2/func.php on line 23
Notice: Undefined index: func in /Applications/XAMPP/xamppfiles/htdocs/select2/func.php on line 67
Notice: Undefined index: func in /Applications/XAMPP/xamppfiles/htdocs/select2/func.php on line 114
Here are the offending lines:
Line 23
if($_GET['func'] == "drop_1" && isset($_GET['func'])) {
drop_1($_GET['drop_var']);
}
function drop_1($drop_var)
{
Line 67
if($_GET['func'] == "drop_2" && isset($_GET['func'])) {
drop_2($_GET['drop_var']);
}
function drop_2($drop_var)
{
Line 114
if($_GET['func'] == "drop_3" && isset($_GET['func'])) {
drop_3($_GET['drop_var']);
}
function drop_3($drop_var)
{
I looked in the existing questions and didn't find an answer where an if statement was attached with the operator &&.
Thanks,
Change the order of your condition to this:
if(isset($_GET['func']) && $_GET['func'] == "drop_1" )
Now you are checking if the 'func' index is set first before using it.
You have the order of your logical operators backwards. First you need to check to see if the variable is set and then you need to check it's value. If the first check is false then the second one will never happen due to short circuiting which prevents the error message you see.
if($_GET['func'] == "drop_1" && isset($_GET['func'])) {
should be:
if(isset($_GET['func']) && $_GET['func'] == "drop_1") {