# The fopen() function is used to open files in PHP.<?php$file=fopen("welcome.txt","r") or exit("Unable to open file!");?>
# The fclose() function is used to close an open file:<?php$file = fopen("test.txt","r");//some code to be executedfclose($file);?>
# The feof() function checks if the "end-of-file" (EOF) has been reached.<? ?>
# The fgets() function is used to read a single line from a file.Note: After a call to this function the file pointer has moved to the nextline.<?php $file = fopen("welcome.txt", "r") or exit("Unable to open file!"); //Output a line of the file until the end is reached while(!feof($file)) { echo fgets($file). "<br>"; } fclose($file); ?>
# The fgetc() function is used to read a single character from a file.Note: After a call to this function the file pointer moves to the next character.<?php $file=fopen("welcome.txt","r") or exit("Unable to open file!"); while (!feof($file)) { echo fgetc($file);
} fclose($file); ?>
<?phpif(!file_exists("welcome.txt")) {die("File not found");} else {$file=fopen("welcome.txt","r");}?>
<?phperror_function(error_level,error_message,error_file,error_line,error_context);?># error_level^ : Specifies the error report level for the user-defined error.# error_message^ : Specifies the error message for the user-defined error# error_file : Specifies the file-name in which the error occurred# error_line : Specifies the line number in which the error occurred# error_context : Specifies an array containing every variable, and their values, in use when the error occurred
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
# Basic<?php//create function with an exceptionfunction checkNum($number) {if($number>1) {throw new Exception("Value must be 1 or below");}return true;}//trigger exceptioncheckNum(2);?>
# Try , Throw and Catch<?php//create function with an exceptionfunction checkNum($number) {if($number>1) {throw new Exception("Value must be 1 or below");}return true;}//trigger exception in a "try" blocktry {checkNum(2);//If the exception is thrown, this text will not be shownecho 'If you see this, the number is 1 or below';}//catch exceptioncatch(Exception $e) {echo 'Message: ' .$e->getMessage();}?>
# header() is used to send a raw HTTP header.<?phpheader('Location: http://www.example.com/');exit;?>