Although I use isset, I get an undefined index error - php

I know it asks too many time. But the isset function is not solve my problem.
$get = (isset($this->settings[$set['id']])) ? $this->settings[$set['id']] : '';
Notice: Undefined index: id in \public_html\settings.php on line 419

Try to check if the variable is set before using it as an argument.
$get = isset( $set['id']) ? $this->settings[$set['id']] : '';

Maybe, $set['id'] must check, like this:
$set_ = isset($set['id']) ? $set['id'] : '';
$value = isset($this->settings[$set_]) ? $this->settings[$set['id']] : '';

I would simply add it to the isset call
$get = isset( $set['id'],$this->settings[$set['id']]) ? $this->settings[$set['id']] : '';
You can use multiple arguments in isset. This is roughly equivalent to doing this:
$get = isset($set['id']) && isset($this->settings[$set['id']]) ? $this->settings[$set['id']] : '';
This can be easily tested with this code:
$array = ['foo' => 'bar'];
$set = []; //not set
#$set = ['id' => 'foo']; //uncomment to test if set
#using [] to add an element to a string not an array
$get = isset($set['id'],$array[$set['id']]) ? $array[$set['id']] : '';
echo $get;
When $set = ['id' => 'foo'] the output is bar if you leave that commented then the output is an empty string.
Sandbox

Related

$_POST better writing quality

I have this code :
if(isset($_POST['prenom2'])){
$t['prenom2'] = $_POST['prenom2'];
}else{
$t['prenom2'] = '';
}
if(isset($_POST['nom2'])){
$t['nom2'] = $_POST['nom2'];
}else{
$t['nom2'] = '';
}
if(isset($_POST['prenom3'])){
$t['prenom3'] = $_POST['prenom3'];
}else{
$t['prenom3'] = '';
}
etc (there are 5 or 6 fields I need to test).
There must be a better way of doing this, like if a given index of POST isn't set, that index is...
Thanks
You can use foreach.
$indexes = array('prenom2', 'nom2', ...);
$t = array();
foreach ($indexes as $i) {
$t[$i] = isset($_POST[$i]) ? $_POST[$i] : '';
}
print_r($t);
If you don't want to use if..else.. condition then you can use ternary : operator as
$t['prenom2'] = (isset($_POST['prenom2'])) ? $_POST['prenom2'] : '';
$t['nom2'] = (isset($_POST['nom2'])) ? $_POST['nom2'] : '';
Basically the same as the answer from Matei but moved in a function to reduce duplicate code.
Parameter $t is your final array and $key is a string representing the array index. The final $t array is also returned so there is no need for passing a reference.
function setT($t, $key)
{
$t[$key] = isset($_POST[$key]) ? $_POST[$key] : '';
return $t;
}
$t = setT($t, 'prenom1');
$t = setT($t, 'prenom2');
$t = setT($t, 'prenom3');
Based on your real problem, you may choose one of these:
for($i=1; $i<6; $i++){
$t['prenom'.$i] = (isset($_POST['prenom'.$i])) ? $_POST['prenom'.$i] : '';
$t['nom'.$i] = (isset($_POST['nom'.$i])) ? $_POST['nom'.$i] : '';
}
or
$indexes = array('prenom2'=>'', 'nom2'=>'', ...);
$t = array_merge($indexes,$_POST);

array_shift shows warning if the variable has a null or empty value

I'm working on a MVC project and i'm on the part to get the URL values, to get each param i use array_shift() and the documentation says this:
Returns the shifted value, or NULL if array is empty or is not an
array.
In my code i have these lines:
$arrParams = isset($_GET["params"]) ? explode("/", $_GET["params"]) : "";
$controller = array_shift($arrParams);
$action = array_shift($arrParams);
$params = array_shift($arrParams);
If i access to mvc-project.local and i don't pass any param to the URL appears this message:
Warning: array_shift() expects parameter 1 to be array, string given in ... on line 12
Where is the problem?
Try this -
$arrParams = isset($_GET["params"]) ? explode("/", $_GET["params"]) : array();
Or
(array) $arrParams = isset($_GET["params"]) ? explode("/", $_GET["params"]) : "";
Or
$controller = array_shift((array)$arrParams);
$action = array_shift((array)$arrParams);
$params = array_shift((array)$arrParams);
You are defaulting $arrParams to an empty string, so that is why you get a warning (note not an error) about it being passed a string. Just make it an empty array:
$arrParams = isset($_GET["params"]) ? explode("/", $_GET["params"]) : array();
Or a not so good solution is to suppress the warning with #:
$controller = #array_shift($arrParams);
$action = #array_shift($arrParams);
$params = #array_shift($arrParams);

PHP shorthand to skip a variable assignment

I am using this shorthand in a function to set a variable to NULL if it is not set. How can I simply not set it?
function createArray($rows){
$array = array();
$array['id'] = isset($rows['UnitId']) ? $rows['UnitId'] : NULL ;
}
Would this be too simple for you?
if(isset($rows['UnitId'])) {
$array['id'] = $rows['UnitId'];
}
Just don't set it in the first place
function createArray($rows){
$array = array();
if(isset($rows['UnitId'])) $array['id'] = $rows['UnitId'];
}

Automatically apply isset() each time $arr['KEY'] is called

Not sure if this is possible. For example:
$color = isset($product['color']) ? $product['color'] : '';
$size = isset($product['size']) ? $product['size'] : '';
$qty = isset($product['qty']) ? $product['qty'] : '';
***
***
I use isset() pretty much everywhere I need to get a value from array that I am not sure if its set. I am using my own custom framwork so I was wondering if I can add some kind of way to check automatically if its set or not.
So I want something like this:
//if its not set, do not throw warning message just return blank
$color = $product['color'];
$size = $product['size'];
$qty = $product['qty'];
Basically similar to what PHP magic function __isset does for the class but I need it for any array that is not a class.
I added a function to my BaseController that is used in every controller but it is still an extra function call:
$color = $this->isSet($product, 'color', '');
$size = $this->isSet($product, 'size', '');
$qty = $this->isSet($product, 'qty', '');
and function:
public function isSet(array $data, $key, $return) {
return isset($data[$key]) ? $data[$key] : $return;
}
My question is, is there a better way of doing this without functions?
$color = #$product['color'];
$size = #$product['size'];
$qty = #$product['qty'];

htmlspecialchar to multiple variables

I was wondering if there's a simpler way of doing this
$admsquarecmail = #$_POST['squarecmail'];
$admsquarecmail = htmlspecialchars($admsquarecmail, ENT_COMPAT);
$admsquarecsubject = #$_POST['squarecsubject'];
$admsquarecsubject = htmlspecialchars($admsquarecsubject, ENT_COMPAT);
$admsquarectymessage = #$_POST['squarectymessage'];
$admsquarectymessage = htmlspecialchars($admsquarectymessage, ENT_COMPAT);
$admsquarecontagain = #$_POST['squarecontagain'];
$admsquarecontagain = htmlspecialchars($admsquarecontagain, ENT_COMPAT);
The idea is not to type
htmlspecialchars($var, ENT_COMPAT);
every time a variable is added.
Method 1:
You may apply htmlspecialchars to all elements of $_POST with array_map:
$arr = array_map("htmlspecialchars", $_POST, array_fill(0, sizeof($_POST), ENT_COMPAT));
Then:
$admsquarecmail = isset($arr['squarecmail']) ? $arr['squarecmail'] : "";
$admsquarecsubject = isset($arr['squarecsubject']) ? $arr['squarecsubject'] : "";
$admsquarectymessage = isset($arr['squarectymessage']) ? $arr['squarectymessage'] : "";
...and so on.
Method 2:
You may apply htmlspecialchars to the elements of $_POST one by one. In this method you don’t need an array apart from $_POST itself:
$admsquarecmail = isset($_POST['squarecmail']) ? htmlspecialchars($_POST['squarecmail'], ENT_COMPAT) : "";
$admsquarecsubject = isset($_POST['squarecsubject']) ? htmlspecialchars($_POST['squarecsubject'], ENT_COMPAT) : "";
$admsquarectymessage = isset($_POST['squarectymessage']) ? htmlspecialchars($_POST['squarectymessage'], ENT_COMPAT) : "";
...and so on.
Method 3:
You may create a small function like the following:
function obtain_POST_value($key){
if(array_key_exists($key, $_POST)) return htmlspecialchars($_POST[$key], ENT_COMPAT);
return "";
}
Then:
$admsquarecmail = obtain_POST_value('squarecmail');
$admsquarecsubject = obtain_POST_value('squarecsubject');
$admsquarectymessage = obtain_POST_value('squarectymessage');
...and so on.

Categories