php try catch exception handling code with example

Here we are trying to find out how to add php try catch exception handling code in your PHP .we know that like every programming language php also as its on error handling method, Exception handling in PHP ,new object oriented way of dealing with errors, we have have PHP try catch exception handling code
Here is the syntax

//trigger exception in a "try" block
try {

}
//catch exception
catch(Exception $e) {
  echo 'Error: ' .$e->getMessage();
}

See PHP try catch exception handling code with example


try {
          $status = 1;

            $cont = $this->conn;
            $sql = "INSERTED INTO menu(display_name, menu_link,  status,sort_order,menu_slug) VALUES(:menu_title,:menu_link,:status,:sort_order,:menu_slug)";
            $new_db = $cont->prepare($sql);

            $new_db->bindParam(':menu_title', $data['menu_title']);
            $new_db->bindParam(':menu_link', $data['menu_link']);

            $new_db->bindParam(':sort_order', $data['sort_order']);
            $new_db->bindParam(':status', $status);
            if($data['menu_slug']!='')
            {
                $slug_value=$data['menu_slug'];
            }
            else
            {
                $slug_value=$data['menu_title'];
            }
            $slug = $this->generate_sub_menu_slug($extra_string='',$slug_value);
            $new_db->bindParam(':menu_slug', $slug);


            $new_db->execute();
        } catch (PDOException $e) {
            echo "Error: " . $e->getMessage();
        }

In the above example MySQL INSERT syntax is wrong so this php code through an Exception error that says SQL state error

 try { 
          $status = 1;

            $cont = $this->conn;
            $sql = "INSERT INTO menu(display_name, menu_link,  status,sort_order,menu_slug) VALUES(:menu_title,:menu_link,:status,:sort_order,:menu_slug)";
            $new_db = $cont->prepare($sql);

            $new_db->bindParam(':menu_title', $data['menu_title']);
            $new_db->bindParam(':menu_link', $data['menu_link']);

            $new_db->bindParam(':sort_order', $data['sort_order']);
            $new_db->bindParam(':status', $status);
            if($data['menu_slug']!='')
            {
                $slug_value=$data['menu_slug'];
            }
            else
            {
                $slug_value=$data['menu_title'];
            }
            $slug = $this->generate_sub_menu_slug($extra_string='',$slug_value);
            $new_db->bindParam(':menu_slug', $slug);


            $new_db->execute();
        } catch (PDOException $e) {
            echo "Error: " . $e->getMessage();
        }

How to read a file content line by line using PHP code

Suppose we have file named file.txt we have read a file content line by line using PHP , here our objective is to retrieve a file data line by line using PHP.we know we have most famous php function fopen(“File name”,”r”),it will read entire content from the files and store it in the defined variable
see example

 $f_data = fopen("file.txt","r");
 

Now our question how to read line by line using PHP
Now we know the entire data is their in $f_data,next we have to read line by line using PHP , with the help of while loop we can do that ,for line by line read we use one more function called feof($f_data) which find out the end of the file see full example

 
 $f_data = fopen("file.txt","r");
  
  while(! feof($f_data))  {
	$result = fgets($f_data);
	echo $result."
"; echo "Good"."
"; } fclose($f_data);

e.g
file.txt

Sunday is weekend day
Monday is weekstart day

Out Put

Sunday is weekend day
Good
Monday is weekstart day
Good

How to find length of array using php

we are luck enough to find the length of an array using php, we have two build in function to find the count of an array using php.Lets see in details that function

1) sizeof() function

2) count() function

First let we check out the syntax of sizeof() function

 sizeof($Array);
example

$colors=array("RED","BLUE","GREEN");
echo sizeof($colors);

Output

3


let we check out the syntax of count() function

 count($Array);
example

$colors=array("RED","BLUE","GREEN");
echo count($colors);

Output

3

How to use html encode function in php to prevent browsers from using it as an HTML

In this section we are going check the usage of html encode in PHP here our question is how to use html encode function in php to prevent browsers from using it as an HTML .sometimes we have to display html element in the front end as it is without coveting it.for example we need to display below line

<h2>Hello</h2> <p>Hello here here we are disusing the usage of html encode function php to prevent browsers from using it as an HTML   </p>

but we know that normally it display like below

use html encode function php to prevent browsers from using it as an HTML

we are lucky enough to handle this situation we have a html encode function in php it prevent browsers from using it as an HTML lets see the function htmlentities

htmlentities(string,flags,character-set,double_encode)
htmlentities ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = ini_get("default_charset") [, bool $double_encode = TRUE ]]] ) 

default
htmlentities($htmlcode);

See example how it is working

 $htmlcode='<h2>Hello</h2>
Hello here here we are disusing the usage of html encode function php to prevent browsers from using it as an HTML';
echo htmlentities($htmlcode);

Output

Hello

Hello here here we are disusing the usage of html encode function php to prevent browsers from using it as an HTML