Notice: Undefined index - Solving with isset? - php

Notice: Undefined index: extension in /var/www/.. on line 187
//185 - $f_name = $this->filename;
//186 - $path_parts = pathinfo($f_name);
//187 - $file_ext = $path_parts['extension'];
After some googleing I've seen alot of Isset being used. But I'm in doubt, about how to use it, in this case? Is it even going to solve the problem?

Yes, you can use isset in this case. You first check if the key is set before trying to access it. I would use the ternary operator to set a default value if you need to.
$file_ext = isset($path_parts['extension']) ? $path_parts['extension'] : null;

Since php 7.0, You can use null coalescing operator
$file_ext = $path_parts['extension'] ?? null;
https://www.php.net/manual/en/migration70.new-features.php

pathinfo will only return the "extension" index if the path has an extension, otherwise it will not return this index.
A simple check should be used to determine whether the path has an index, such as:
if(!empty($path_parts['extension'])) {
// Extension exists
}

$f_name has no extension, so $path_parts['extension'] is not set.
pathinfo, look Example #2

Related

PHP - Undefined index solving returns warining

I have a function within my PHP project which has some specific params..
$user_sequence = $user['user_sequence'];
if ($cached || !$user_sequence) {
return;
}
Notice: Undefined index: user_sequence
I have fixed that with:
if (isset($user['user_sequence'])) {
$user_sequence = $user['user_sequence'];
}
But now my second if() clause get's a warning:
Variable '$user_sequence' is probably undefined
Would setting it to null before if() be the best approach to solve it? Thanks
Spelled out in full, you need to specify what the value should be if $user['user_sequence'] is not set. In your second if statement, you are checking !$user_sequence, so a logical default value would be boolean false:
if (isset($user['user_sequence'])) {
$user_sequence = $user['user_sequence'];
}
else {
$user_sequence = false;
}
Luckily, PHP doesn't make use write out that whole if statement, because we can use the null coalescing operator, and shorten it to this:
$user_sequence = $user['user_sequence'] ?? false;
In other situations, you might want to default to an empty array ($something ?? []) or even just null ($something ?? null).

Function to Avoid Variable Undefined

I am trying to write a function to avoid getting variable undefined error. Right now, i have a code like this:
function check($str){
if(isset($str)){
$s = $str;
} else {
$s = "";
}
}
check($_GET['var']);
The get var is not set. I am getting a variable undefined error on my screen. How do i alter my function to not throw this error and just return "" if it is not set? I don't want to have to code 100 if statements to avoid getting variable undefined. Thanks.
We already have in PHP a construct to check that. It is called isset(). With it you can check whether a variable exists. If you would like to create it with some default values if it doesn't exist yet, we also have syntax for it. It's null-coalescing operator.
$_GET['var'] = $_GET['var'] ?? '';
// or since PHP 7.4
$_GET['var'] ??= '';
Although I'm not sure if it is the right way of doing it, for the sake of providing an answer you can pass the variable by reference, this allows you to get away with passing undefined variables and check if it is set inside the function..
function check(&$str){
if(!isset($str)){
$str = "not set";
}
}
check($_GET['var']);
echo $_GET['var'];

getopt -> return PHP Notice: Undefined index:

I'm using getopt to pass variables to my script, but I get the message:
PHP Notice: Undefined index:
Here's my code:
$MyOptions = getopt("c:hp:");
if ($MyOptions['c']) { //line wher error show up!
$MyClub = $MyOptions['c'];
} else {
$MyClub = '53';
}
What did I miss?
The problem is the index c in your $MyOptions array does not exist, you can avoid this in a number of ways, but my preferred option in this case would be to replace the entire if-else statement with;
$MyClub = $MyOptions['c'] ?? '53';
The ?? is the Null Coalescing operator.
The null coalescing operator (??) has been added as syntactic sugar for the common case of needing to use a ternary in conjunction with isset(). It returns its first operand if it exists and is not NULL; otherwise it returns its second operand.
Please be aware this is only available from PHP 7, otherwise you will have to use an isset() to check if the index exists.

i want to take the last word and use end( ) function i get an error

$allowed=array('jpg','jpqg','gif','png','JPG','JPEG','GIF','PNG');
$file_name=$_FILES['profile']['name'];
$file_extn= end(explode('.',$file_name));
$file_temp=$_FILES['profile']['tmp_name'];
if(in_array($file_extn,$allowed) == true) {
//change_profile_image($_SESSION['id'],$file_temp);
}
else {
echo'incorrect file type .Allowed:';
echo implode(', ',$allowed);
}
The error is:
Strict standards: Only variables should be passed by reference in
When I use end function get this error
To get the file extension use like below:
$ext = pathinfo($file_name, PATHINFO_EXTENSION);
The warning message says it all. You must store explode result in a variable before using end
for example:
$pieces = explode('.',$file_name);
$file_extn= end($pieces);
~ Edit:
The end function takes the argument as reference. It means the funcion uses the memory address instead of the value. Thats why we must store the return from explode in a variable.
You can read more about passing by reference in this link: http://php.net/manual/en/language.references.pass.php
And here is the end doc: http://php.net/manual/en/function.end.php

PHP, easier way to get array values without warnings

Dudes,
is there a more concise way to write the statement below? If I don't check if an array key exists I get a PHP warning. However, the below is a bit too, ehm, wordy.
Thanks!
$display_flag = false;
if (array_key_exists('display_flag',$pref_array) {
$display_flag = $pref_array['display_flag'];
}
Since PHP 7 you can use the new null coalescing operator.
$display_flag = $pref_array['display_flag'] ?? false;
If $display_flag is a boolean:
$display_flag = isset($pref_array['display_flag']) && $pref_array['display_flag'];
If it's a string:
$display_flag = isset($pref_array['display_flag']) ? $pref_array['display_flag'] : false;
// Get the $pref_array from wherever
$default_prefs = array(
"display_flag" => false,
);
$pref_array = array_merge($default_prefs, $pref_array);
// Now you know it's always defined with default values
The way you have it is fine, as you should validate if the value actually exists, but you could also do a ternary op:
$display_flag = (isset($pref_array['display_flag'])) ? (bool) $pref_array['display_flag'] : false;
I typecast the contents of display_flag to bool if it is set, so you are ensured a boolean value in either case.
Also you could (but I don't recommend it), squelch the warning with the #:
$display_flag = #$pref_array['display_flag'];
Another simpler option is:
array_get($variable, 'keyName', null)
The third argument is the default value.

Categories