Sorts of Errors in PHP and Learn how to Deal with Them


On this article, I’m going to debate the Sorts of Errors in PHP and Learn how to Deal with Them.

There are 4 sorts of errors {that a} PHP Script can generate.

  • Syntax errors or compile errors
  • Runtime errors or deadly errors
  • Warnings
  • Discover Errors

Syntax Errors (Compile Errors)

Syntax errors are these errors that the compiler catches. These errors end result from unsuitable spelling, lacking semicolon, not ending the string correctly or some other typing mistake. When compiler encounters any such error, it reviews these errors as syntax errors. The next code exhibits syntax errors in lin2 and line 6.

<?php
$a="whats up";
$b='world!

echo $a;
echo '<br>';
ech $b;

?>

Output

A Parse Error Due to Missing apostrophe and semicolon
A Parse Error On account of Lacking apostrophe and semicolon

After we make correction in line 2 of the above program as $b=’world!’, we get one other error. This is because of spelling mistake in echo operate within the final line.

A Parse Error due to Spelling Mistake
A Parse Error because of Spelling Mistake

As soon as we appropriate each of those errors as follows, we get the next output.

<?php
$a="whats up";
$b='world!';

echo $a;
echo '<br>';
echo $b;

?>

Output

The Output After Removing Syntax Errors
The Output After Eradicating Syntax Errors

Discover Errors

In actual fact, a discover error could also be or will not be an errors. These are minor errors. Moreover, discover errors don’t cease script and produces the output. The commonest supply f discover errors are using undefined variable. As will be seen within the following code, the variables a, b, and c usually are not outlined. So, once we attempt to print these variables, a discover error happens.

<?php
 echo $a.' '.$b.' '.$c;
?>

Output

Notice Errors Due to the Use of Non-Existing variables
Discover Errors Because of the Use of Non-Present variables

Runtime Errors (Deadly Errors)

These errors make the script to crash. When a deadly error happens, the script stops executing. All these errors end result from numerous sources. For example, name to a non-existing operate, or calling a operate from an object that doesn’t exist.

Examples on Sorts of Errors in PHP and Learn how to Deal with Them

The next code exhibits that decision to a non-existing operate mysqli ends in a deadly error.

<?php
 // Attempt to join with a MySQL database
 $con1=mysqli("localhost", "root", "", "somedatabase");
?>

Output

Fatal Error Due to the Use of Undefined Function
Deadly Error Because of the Use of Undefined Operate

Equally the next error happens due to the undefined operate present().

<?php
 class MyClass
 {
   protected $a, $b;
 }
 class SubClass extends MyClass{
    operate __construct()
    {
       $this->a=1; $this->b=2;
       present();
     }
    operate present()
    {
	echo 'a=", $this->a;
        echo "<br>b = ", $this->b;
    }
  }
  $ob=new SubClass(20, 30);
  $ob->__construct();
?>

Output

Call to Undefined Function Results in a Fatal Error
Name to Undefined Operate Ends in a Deadly Error

Correcting the Above Error

In an effort to appropriate the above error, simply name the operate present() because the occasion methodology as proven beneath.

<?php
 class MyClass
 {
   protected $a, $b;
 }
 class SubClass extends MyClass{
    operate __construct()
    {
       $this->a=1; $this->b=2;
       $this->present();
     }
    operate present()
    {
	echo "a=", $this->a;
        echo "<br>b = ", $this->b,"<br>';
    }
  }
  $ob=new SubClass(20, 30);
  $ob->__construct();
?>

Output

An Example to Show the Types of Errors in PHP and How to Handle Them
An Instance to Present the Sorts of Errors in PHP and Learn how to Deal with Them

The next deadly error outcomes from the truth that the database desk ’emp’ doesn’t exist.

<?php
 // Attempt to join with a MySQL database
 $con1=new mysqli("localhost", "root", "", "d1");
 $r1=$con1->question('choose * from emp');
 $row = $r1->fetch_assoc()
?>

Output

Fatal Error Due to Non-availability of the Database Table
Deadly Error On account of Non-availability of the Database Desk

When a recursive operate fails to terminate, a runtime error happens. The next code demonstrates it.

<?php
 operate f1()
 {
  $a=$a++;
  echo $a;
  f1();
 }
 f1();
?>

Output

Fatal Error Due to a Non-Terminating Recursive Function Call
Deadly Error On account of a Non-Terminating Recursive Operate Name

Warnings

Warnings point out the presence of some points. Nevertheless, they don’t cease execution of script. The next code exhibits a warning generated when the database consumer isn’t capable of entry a desk because of invalid cresentials.

<?php
 // Attempt to join with a MySQL database
 $con1=new mysqli("localhost", "someuser", "123456", "somedatabase");
?>

Output

Warning Generated Due to Access Denied to a User
Warning Generated On account of Entry Denied to a Consumer

Equally, the next code generates a warning due to invalid database title.

<?php
 // Attempt to join with a MySQL database
 $con1=new mysqli("localhost", "root", "", "somedatabase");
?>

Output

Warning Generated Due to the Use of Unknown Database
Warning Generated Because of the Use of Unknown Database

Additional Studying

Overview of Imply Stack

Creating Single Web page Functions with Angular

Angular 10 Information Binding in Totally different Methods

Creating Some Angular Parts

Examples of Array Capabilities in PHP

Fundamental Packages in PHP

princites.com



Source_link

Leave a Reply

Your email address will not be published.