In the previous part of the course, we have learnt a bit about errors in PHP. In this part of the course, we will dig deeper into various types of errors and error reporting in PHP. We will also learn about decision making statements in PHP and how we can use them to dynamically perform various actions depending upon various test conditions.
Error Reporting in PHP
Here we will specifically talk about syntax errors only and how we can manage and report them. We are not going to deal with semantic errors here as that is something which we cannot discuss in just a few words and needs continuous discussion as we progress with our learning process.
Let us illustrate this with an example,
[php] <?php
echo ‘Hello!’
?> [/php]
This code runs perfectly and outputs “Hello!” but we can clearly see that we have made a mistake in the syntax by not including semi-colon at the end of line 2. The reason for this is that PHP works on line-by-line basis and as there are no lines following the echo statement, no errors are produced.
Now, let us add another line after it,
[php] <?php
echo ‘Hello!’
echo ‘ and Welcome to corePHP’;
?> [/php]
Now we get a blank while page with no output at all but we also do not get any error messages. I have actually turned off error reporting in my PHP installation because of which no error messages have been produces. But we can turn it on anytime we need to see what our actual error messages are.
Practically, when you are developing PHP applications you should always have your error reporting turned on, but when you have completed development and have put your application in a server for other people to use it, you should turn it off as you do not want to scare your users with errors which they are never going understand.
Errors are specifically for the developers like us to use when we are developing or debugging our code. Now, let us learn how we can enable or disable the error reporting feature in PHP.
Do you remember about the php.ini configuration file of your PHP installation which we have talked about in the third part of the course? If you don’t then it’s better to revisit the third part and check out the discussion once again. We can easily enable or disable the error reporting feature in our PHP installation from the php.ini file.
In the Error Handling and Logging section of the php.ini file, you will find a detailed description of what this feature means and does in PHP. You will also have various examples to make you understand all the options available for you in a better way.
There are basically four types of errors in PHP,
I) Parse Error – These are the simple errors which result from making simple mistakes while writing the syntax of your code. For example, missing the semi-colon at the end of a line statement.
II) Notices – These are the most trivial and non-critical errors in your script that the interpreter encounters while executing your code. For example, if you are trying to access a variable which you haven’t defined yet.
III) Warnings – These are the errors which are a bit more serious than the notices. For example, if you are trying to include() a file in your script which doesn’t exist.
IV) Fatal Errors – These are the most serious and critical errors which can immediately break the script from executing. For example, calling a non-existent function.
In the php.ini file, you can combine various options using the operators (AND, OR, NOT) and can customize the way errors are reported to you. The basic recommendations are –
Default Value: E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
Development Value: E_ALL
Production Value: E_ALL & ~E_DEPRECATED & ~E_STRICT
Don’t forget to save the php.ini file and restart Apache and MySQL from the XAMPP Control Panel to bring the new changes into effect.
Decision Making Statements
Now we are going to learn about the capabilities of PHP to make various actions depending on certain conditions. If a condition (or a set of conditions) are true, then a particular action (or a set of actions) will be performed, and if not then another action (or set of actions) will be performed.
There are various types of conditional statements available in PHP –
i) if statement
ii) if-else statement
iii) if-elseif-else statement
iv) switch statement
Let us illustrate each of them with intuitive real world examples,
i) The if Statement
The syntax of the if statement is something like this,
if(condition){ //statements to be executed if the condition is true }
Suppose, it’s a weekday and you need to go to your school to attend a seminar. But if it rains then you are not going to go to your school, stay at home and play video games.
[php] <?php
$isRaining = true;
if($isRaining){
echo ‘I will not go to school today. Stay at home and play video games.’;
}
?> [/php]
ii) The if-else Statement
The syntax is something like this,
if(condition){ //Statements to be executed if the condition is true } else{ //Statements to be executed if the condition is false }
Take the same example, but add another set of action to be performed if it does not rain.
[php] <?php
$isRaining = true;
if($isRaining){
echo ‘I will not go to school today, stay at home and play video games.’;
}
else{
echo ‘I will go to school and attend the seminar.’;
}
?> [/php]
iii) The if-elseif-else Statement
The syntax is,
if(condition){ //Statements to be executed if the condition is true } elseif(another_condiiton){ //Statements to be executed if the other condition is true } else{ //Statements to be executed if none of the conditions are true. }
Let us take the same example and modify it a bit,
[php] <?php
$isRaining = true;
$isRoadBroken = true;
if($isRaining){
echo ‘I will not go to school today, stay at home and play video games.’;
}
elseif ($isRoadBroken){
echo ‘I will go to school an hour later.’;
}
else{
echo ‘I will go to school and attend the seminar.’;
}
?> [/php]
iv) The switch Statement
Switch statement proves to be a very useful decision-making statement if we have multiple expressions to be evaluated. It is much simpler and quicker to write codes using switch statement rather than a long set of if-elseif-else statements.
The syntax for the switch statement is a bit different from the others –
switch (n) { case tag1: code to be executed if n=tag1; break; case tag2: code to be executed if n=tag2; break; case tag3: code to be executed if n=tag3; break; ... default: code to be executed if n is different from all tags; }
Let us take a different example to illustrate the switch statement. Suppose you visit a market and have bought a particular product and you get a message at the time of checkout. Let us see how the code can look like.
[php] <?php
$choice = "Coffee";
switch($choice){
case "Beans":
echo ‘Thank you for buying Beans.’;
break;
case "Olive":
echo "Thank you for buying Olive.";
break;
case "Coffee":
echo "Thank you for buying Coffee.";
break;
case "Egg":
echo "Thank you for buying Egg.";
break;
default:
echo "You haven’t purchased anything this time. Thank you for visiting.";
}
?> [/php]
In this part of the series, we have learned a lot of interesting and informative things on error reporting in PHP. We have learned about the various ways of reporting errors using the php.ini configuration file. We have also learned about various decision making statements which we can use to make dynamic decisions and actions in PHP.
In the next part of the course, we are going to learn almost everything about operators in PHP. It is surely going to be a very essential and useful episode which you can never afford to miss. So see you in the next part of the course.