Tuesday, 23 July 2019

File handling and Exception in PHP

File handling in PHP
File handling is needed for any application. For some tasks to be done file needs to be processed. File handling in PHP is similar as file handling is done by using any programming language like C. PHP has many functions to work with normal files. Those functions are:-
  1. fopen() – PHP fopen() function is used to open a file. First parameter of fopen() contains name of the file which is to be opened and second parameter tells about mode in which file needs to be opened.
Eg:- <?php 
$file = fopen(“demo.txt”,'w'); 
?>
Note:-
  • “w” – Opens a file for write only. If file not exist then new file is created and if file already exists then contents of file is erased.
  • “r” – File is opened for read only.
  • “a” – File is opened for write only. File pointer points to end of file. Existing data in file is preserved.
  • “w+” – Opens file for read and write. If file not exist then new file is created and if file already exists then contents of file is erased.
  • “r+” – File is opened for read/write.
  • “a+” – File is opened for write/read. File pointer points to end of file. Existing data in file is preserved. If file is not there then new file is created.
  • “x” – New file is created for write only.
2. fread() –– After file is opened using fopen() the contents of data are read using fread(). It takes two arguments. One is file pointer and another is file size in bytes.
Eg: - <?php 
$filename = "demo.txt"; 
$file = fopen( $filename, 'r' ); 
$size = filesize( $filename ); 
$filedata = fread( $file, $size ); 
?>
3. fwrite() – New file can be created or text can be appended to an existing file using fwrite() function. Arguments for fwrite() function are file pointer and text that is to written to file. It can contain optional third argument where length of text to written is specified.
Eg:- <?php 
$file = fopen("demo.txt", 'w'); 
$text = "Hello world\n"; 
fwrite($file, $text); 
?>

4. fclose() – file is closed using fclose() function. Its argument is file which needs to be closed
eg:-<?php 
$file = fopen("demo.txt", 'r'); 
//some code to be executed 
fclose($file); 
?>

What is an Exception?
Exception handling is used to change the normal flow of the code execution if a specified error (exceptional) condition occurs. This condition is called an exception. This is what normally happens when an exception is triggered:
  • The current code state is saved
  • The code execution will switch to a predefined (custom) exception handler function
  • Depending on the situation, the handler may then resume the execution from the saved code state, terminate the script execution or continue the script from a different location in the code
We will show different error handling methods:
  • Basic use of Exceptions
  • Creating a custom exception handler
  • Multiple exceptions
  • Re-throwing an exception
  • Setting a top level exception handler
Note: Exceptions should only be used with error conditions, and should not be used to jump to another place in the code at a specified point.
Eg:- <?php
//create function with an exception
function checkNum($number) 
{
  if($number>1)
 {
    throw new Exception("Value must be 1 or below");
  }
  return true;
}
//trigger exception
checkNum(2);
?>

Try, throw and catch
Proper exception code should include:
  1. try - A function using an exception should be in a "try" block. If the exception does not trigger, the code will continue as normal. However if the exception triggers, an exception is "thrown".
  2. throw - This is how you trigger an exception. Each "throw" must have at least one "catch".
  3. catch - A "catch" block retrieves an exception and creates an object containing the exception information
eg:- <?php
//create function with an exception
function checkNum($number)
 {
  if($number>1) 
{
    throw new Exception("Value must be 1 or below");
  }
  return true;
}
//trigger exception in a "try" block
try {
  checkNum(2);
  //If the exception is thrown; this text will not be shown
  echo 'If you see this, the number is 1 or below';
}
//catch exception
catch(Exception $e) {
  echo 'Message: ' .$e->getMessage();
}
?>
Output:-
Message: Value must be 1 or below
https://www.youtube.com/channel/UCKLRUr6U5OFeu7FLOpQ-FSw/videos

0 comments

Post a Comment