Friday, 30 January 2015

Errors In Php

There are three basics errors in php:

Notices:

These are non-critical errors means, if accessing a variable which never defined php show notice error for it.

<?php

echo $myvaiable; 
// print output
//Notice: Undefined variable: myvariable

?>

Warnings:

These are not critical but these are serious errors mean if user include file but it has some problem in  loading so php show warning errors.

<?php

// including file
include 'file_name.php'

 
// this will generate a warning or E_WARNING because file loading is fail 
//print output 
//Warning: include(filname.php): failed to open stream: No such file or directory
//Warning: include(): Failed opening 'filname.php' for inclusion 

?>

Fatal:

These are critial and important errors means if user call a function which not exits then its show fatal error and stop the script

<?php

// call a non-existent function
// this will generate a fatal error (E_ERROR)
callmyfunction(); 

/print output
//Fatal error: Call to undefined function callmyfunction() 

?>

Include Vs Require

The only difference in Include & Require:


  • Include show warning if any problem in file loading and script will continue.
  • Require show fatal error and if any problem in file loading its stop the script.

Tips:

  • Use Include when file is not necessary in application so its continue script if there is any problem in file.
  • Use Require when file is important for an application so its stop the script if there is any problem in file.

Require Methods

Two different method for require

Require():

Require() method use for including multiple files,its takes all content of file and copies it in the specified.
syntax for require() method as below:

<?php
require 'file_name.php';
?>

file_name.php means path of file




Require_once():

Require_once is similar as require() method ,only different is that if the code from a file has already been included, it will not be included again
syntax for require_once method as below:

<?php
require_once 'file_name.php';
?>



Include Methods



Two different methods for including files:



Include():


In php include() method use for including multiple files,its takes all content of file and copies it in the specified.

syntax for include() method as below:

<?php

include 'file_name.php';

?>


file_name.php means path of file



Include_once():


Include_once is similar as include() method ,only different is that if the code from a file has already been included, it will not be included again

syntax for include_once method as below:

<?php

include_once 'file_name.php';

?>