php try catch exception handling code with example

Here we are try to find out how to add php try catch exception handling code in your PHP function .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, is so easy we have have PHP try catch exception handling code here we hvae 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 add google news feed in your websites using PHP

Here we are going to discus how to add google news feed in your websites using PHP ,we know that google is proving XML for news feed here is the link for that

https://news.google.com/rss/search?q=KEYWORD

Here KEYWORD is search query what we are going to add as news feed in your website

This link output xml formatted news feed from that latest websites we can use same xml in your site ,we can use PHP function to convert from one suitable format

Suppose we need to display Cricket World Cup news feed in your website you have to replace KEYWORD with ‘Cricket World Cup’ see example API link below

https://news.google.com/rss/search?q=Cricket+World+Cup&hl=en-IN&gl=IN&ceid=IN:en

Next Step to get the data from the XML

we need to use simplexml_load_file for that

See below code

 


$xml=simplexml_load_file("https://news.google.com/rss/search?q=Cricket+World+Cup&hl=en-IN&gl=IN&ceid=IN:en");
$arrayG = json_decode(json_encode((array)$xml), TRUE);

TO display latest 10 item we have to use

$arrayFeed=array_slice($arrayG['channel']['item'], 0,2);
foreach($arrayFeed as $key=>$value)
{

echo "</pre>
<div class="col-md-9 nfspara">".$value['description']."</div>
"; }

see demo out from the below link
News Feed

Out Put

add google news feed in your websites using PHP