Handling "Undefined Index" in PHP with SYmfony
Solution 1
An option would be to use set_error_handler()
in order to, somehow, simulate exceptions. An example usage would be the following, although I'm sure you can adjust this to your specific use case:
function my_error_handler($errno,$errstr)
{
/* handle the issue */
return true; // if you want to bypass php's default handler
}
$test = array();
set_error_handler('my_error_handler');
$use_it=$test["323"]; // Undefined index error!
restore_error_handler();
You can see that we "wrap" our "critical" piece of code around set_error_handler()
and restore_error_handler()
. The code in question can be as little as a line, to as large as your whole script. Of course, the larger the critical section, the more "intelligent" the error handler has to be.
Solution 2
Both:
if(isset($test["323"])){
//Good
}
and
if(array_key_exists('123', $test)){
//Good
}
Will allow you to check if an array index is defined before attempting to use it. This is not a Symfony-specific error. Its a common PHP warning that occurs whenever you attempt to access an array element that doesn't exist.
$val = isset($test["323"]) ? $test["323"] : null;
Solution 3
use array_key_exists(), like
if (array_key_exists('123', $test)) {
echo "it exists";
}
Enrique Moreno Tent
I am a full-stack web developer. Born in Spain, living in Germany currently. Always interested in learning more and improving the quality of my work. Also, very glad to be a part of this community where we all help each other. Let's break some eggs!
Updated on June 13, 2022Comments
-
Enrique Moreno Tent almost 2 years
I am building an app with Symfony 2, and I am wondering, how could I handle errors when I try to read a index in an array that doesn't exist? Sadly this kind of errors don't throw exceptions, so I can't really use a try-catch block.
Example:
$test = array(); $test["323"]; // Undefined index error!
Please, ideas how to handle this errors?
Update: I have seen many solutions with
isset
. The problem with this is that I would have to do it with every single access to an array index. Can anyone offer me a more DRY solution? -
Enrique Moreno Tent over 10 yearsThis look much more like what I wanted! But I would like to know how to handle the issue...
-
Wayne Whitty over 10 years@Dbugger Most of the time, you should know what is in your array.
-
geomagas over 10 years@Dbugger: Well, I guess that would depend on the application logic. What would you like to happen in a situation like this? My guess is that you'd want some custom failover (plan-B) procedure to take over and sort things out.
-
Enrique Moreno Tent over 10 yearsBut if I am loading a yaml or INI file, which my customer can customize, it could break stuff. Still not the point of the question
-
Enrique Moreno Tent over 10 yearsOh, you mean that what is inside of "my_error_handler" is the equivalent to the "catch" block? I thought it would be where the "throw" is executed. Could you please show me an example, with whatever error handling you prefer? Also, how does this error_handler play with, when inside a try-catch block?
-
geomagas over 10 yearsI think the link I provided has better examples that I could possibly provide here. There's also a good explanation of how these functions work and what other parameters can be passed.
-
Wayne Whitty over 10 years@Dbugger On page load, would you not load the ini file via a loop? And have defaults for each setting, in case they don't exist in the ini file?
-
Enrique Moreno Tent over 10 yearsPossible solution, but might not work for every case