Are you sure $type is always being set? Your code will only work so long as $vars has a key in it named type
. If, for instance, $vars is an empty array, there will be no type
key and you will get the error you are getting.
You could also try setting a default:
$type = '';
foreach ( $vars as $key => $value) {
$value = is_array($value) ? $value: trim($value);
${$key} = ( !empty($value) ? $value: '' );
};
$fieldType = ( $type == 'money' ? 'number' : 'text' );
From some of your comments, it sounds like this is related to an API call. I would suggest that you have some code design problems here. Your code implicitly assumes that the data should contain a key named type
, and (as written), your program will encounter an error if that key is missing. You don't want that to happen. Rather, if this is a required parameter for an API call, you should be explicitly checking for its presence and then returning an API error if it is missing. It should not be possible for an input mistake to result in an actual error (or notice) in PHP. Always assume that someone is going to call your API wrong (or assume that the API you are calling will return data wrong). Never code under the assumption that everyone else always does things perfectly.
Another way to handle this would be to simply leave the data as an array. Is there a particular reason why you are trying to unpack this data array into variables? While PHP will allow you to work with variables by variable name, as you are doing (${$key}
), it tends to cause confusion because now there is no longer a clear connection between a given variable and when it was last initialized/updated. In the case of a required parameter especially, I think you are better of manually unpacking it:
$type = $vars['type'] ?? '';
if ( !$type )
// handle error here
Edit To add one more thought
This one is a bit of a stretch, but it's the only idea I have. I started this as a comment but moved it to my answer so that I would have more room.
I wonder if it is possible that you have an encoding error. The idea is that if the API was returning data in the wrong encoding, and you used that data to assign to a variable, it might be possible for a variable to be defined but to be effectively inaccessible for you because the character set in PHP's memory doesn't match the character set of your text editor. I've been bitten by this same basic bug in other contexts. It's the modern equivalent of typing a variable name with an uppercase 'o' when the actual name has a zero.
Again, it seems unlikely, but it is easy to check. What you should do is forget about your foreach loop. The key to good debugging is isolation anyway. Before you even get to your foreach loop, do a vardump on your $vars
variable and then do these two lines of code:
if ( !isset( $vars['type'] ) )
throw new \Exception( "Yup, encoding error (or other really weird bug)" );
If var_dump thinks the array has a type
key, but isset doesn't think the array has a type
key, then there is definitely something funky going on. If however isset()
agrees with var_dump()
, then I would say that you are dealing with a more run-of-the-mill error hiding in your code somewhere. In that case, I think your best next step is to ditch your foreach loop and use a more explicit array unpacking, as I discussed above.