php null is undefined value? [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”
I got error:
Notice: Undefined variable: null
Line in code is:
$input = new $class($company, null, $sablonas, $null, $value);
Class constructor is:
public function __construct($company, $document, $sablonas, $options, $value = null) {
How I can pass a null value?

$input = new $class($company, null, $sablonas, $null, $value);
// ^ ^
// (1) (2)
It's talking about (2), not (1). You have a typo with $null.
The notice message "Undefined variable: null" is a little misleading here, but consider the following case:
<?php
error_reporting(E_ALL | E_NOTICE);
echo $lol;
// Output: "Notice: Undefined variable: lol in /t.php on line 3"
?>
You can see that the $ isn't included in the name that the notice message gives you, so if you follow this logic back you arrive at the conclusion I made at the top of this answer.

You have $null as a variable:
$input = new $class($company, null, $sablonas, $null, $value);
//------------------------------------------^^^^^^^^^^
// Guessing that's supposed to be
$input = new $class($company, null, $sablonas, null, $value);
//-------------------------------------------^^^^^^^^

$null = NULL;

Related

Undefined index: client_id" on line 1001 [duplicate]

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 2 years ago.
any help you offer on this will be useful. Am working on my school project and this error code keeps popping up on the application "Undefined index: client_id" on line 1001"
Here is the code
public function onNewclientaddress(){
$addShipmentForm = Settings::get('addShipmentForm',true);
$data = post();
= \Spot\Shipment\Models\Address::where('user_id', $data['client_id'])->update(['default' => 0]);
if ( $addShipmentForm == "add_form_normal"){
$subitem = new \Spot\Shipment\Models\Address;
$subitem->name = htmlspecialchars($data['street_addr']);
$subitem->user_id = htmlspecialchars($data['client_id']);
$subitem->street = htmlspecialchars($data['street_addr']);
$subitem->city = htmlspecialchars($data['city_id']);
$subitem->zipcode = htmlspecialchars($data['postal_code']);
$subitem->country = htmlspecialchars($data['country_id']);
$subitem->default = 1;
$subitem->created_at = \Carbon\Carbon::now();
$subitem->updated_at = \Carbon\Carbon::now();
$subitem->save();
}
else{
line 1001 is the first line of code in the post. please pardon my English
This error indicates that $data['client_id'] is not being set. You will need to ensure that whatever form is providing this data, is passing client_id correctly.
You can see what is currently in $data with a line like:
die(var_dump($data));
This will output the data on the page.
Ensure your client_id field in your form has a name attribute:
name="client_id"

CSV Import using PHP - Undefined Offset using explode

I have a script which imports data from csv and creates variables from the data which are imported into sql tables. It all works fine but the error log goes crazy with Undefined Offset errors.
From what I have read it is because it only finds index [0] so any others throw up the error.
I've looked at the following and can't seem to get the suggestions to work:
PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"
PHP Notice: Undefined offset error when importing from .csv
and many more...
Here is the code:
$filename='XXXXX_'.$today.'.csv';
$url=$proto.$user.':'.$pass.'#'.$server.$path.$filename;
$csv_data=file_get_contents($url);
foreach(preg_split("/((\r?\n)|(\r\n?))/", $csv_data) as $line){
list($type, $id, $ref) = explode(',', $line);
The error is generated on the 'list' and 'explode' line of code.
Apologies, quite new to this, have read multiple articles on the 'why' just not found a fix, it is the only error type i have populating the errorlog. Any help would be much appreciated.
you explode must return least 3 output , if in the line exist less than 3 item , error will be show. For this case you can use:
$lineData = explode(','$line);
$type = isset($lineData[0]) ? $lineData[0] : null;
$id = isset($lineData[1]) ? $lineData[1] : null;
$ref = isset($lineData[2]) ? $lineData[2] : null;
Or use:
#list($type, $id, $ref) = explode(',', $line);

php foreach error first line [duplicate]

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.

PHP Notice: Undefined offset: 1 [duplicate]

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);
}

Undefined index in PHP errors [duplicate]

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.

Categories