Common Errors in PHP
The article overviews common errors in PHP. Consider reading articles based on these common PHP errors for a more in-depth review.
- How to fix PHP Notice: Undefined Variable
- How to fix undefined index in PHP
- Fix array to string conversion in PHP
Introduction
PHP issues a notice or warning on errors at the runtime. PHP is an interpreted language, but it also goes through a compilation phase where the code boils down to an intermediate bytecode that PHP interprets in the runtime.
If you are unsure about compilation and interpretation, consider reading interpreted vs. compiled programming languages.
Let’s go through these common errors and see how to fix them.
1 – Common errors in PHP – Undefined Variable
PHP issues an undefined variable warning when accessing an undeclared or uninitialized variable. One of the recommended fixes for this error involves the PHP isset function and ternary operator. Here’s how it works.
<?php
$name = "Edward";
$name = isset($name) ? $name : 'N/A';
$percentage = isset($percentage) ? $percentage : 'N/A';
echo $name. ' has scored '.$percentage.'%';
?>
The example uses a ternary operator to check a variable. It assigns ‘N/A’ to undeclared or undefined variables.
Check out How to fix PHP Notice: Undefined Variable to learn more about other fixes and some important caveats of how PHP infers a value of an undeclared or uninitialized variable.
2 – Common errors in PHP – Undefined Index
The undefined index or undefined array key in PHP is a warning for accessing an array key that doesn’t exist. One of the recommended fixes uses PHP isset with ternary operators to check if the key exists before accessing it.
<?php
$arr = [
"Name" => "Selina",
"Job Title" => "Software Developer",
];
$name = isset($arr["Name"]) ? $arr["Name"] : "N/A";
$title = isset($arr["Job Title"]) ? $arr["Job Title"] : "N/A";
$experience = isset($arr["Experience"]) ? $arr["Experience"] : "N/A";
echo "Hi, this is ".$name.PHP_EOL;
echo "I am a ".$title.PHP_EOL;
echo "I have ".$experience." years of experience".PHP_EOL; //Key doesn't exist.
/*
OUTPUT
Hi, this is Selina
I am a Software Developer
I have N/A years of experience
*/
?>
That’s just one way to fix this warning. Check out How to fix undefined index in PHP to see other ways to fix one of these common PHP errors.
3 – Common errors in PHP – Array to String Conversion in PHP
Array to string conversion occurs when trying to convert an array to string implicitly. Here’s one of the recommended fixes using the PHP json_encode function.
<?php
$array = [
"a" => ["b" => ["c" => "d"]],
"e" => "f",
"g" => ["h", "i"=>["j","k"]]
];
echo 'The array is: '.json_encode($array); //The array is: {"a":{"b":{"c":"d"}},"e":"f","g":{"0":"h","i":["j","k"]}}
?>
The great thing about this fix is that it can deal with multidimensional arrays too. Read more about this warning and how to fix it in the article How to fix array to string conversion in PHP.
Wrap Up
This article overviews common PHP errors. Firstly, it reviews undefined variable warning in PHP followed by undefined index error. Finally, it includes a common error related to array and string conversion. All these errors are discussed in more depth in other articles at FuelingPHP.
Hopefully, you’ve enjoyed the article. Stay tuned for more articles like these at FuelingPHP.
Want to learn more about PHP?
We have many fun articles related to PHP. You can explore these to learn more about PHP.