Friday, 28 February 2014

Die and Exit Functions in Php

die and exit function in php
Die and Exit Functions in PHP Programming Languages. This tutorial is for Die & Exit Functions. They are very useful functions that you should get to use into your program so you can cover the script at any point where you made fatal error and which you don’t want to execute . Let see how it works:-

Code for die function (Screenshot1):-

die function php
Screenshot1
<?php

echo 'Hello';

die();

 echo ' World';

?>



Output Screenshot2
Die Function php
Screenshot2
In this example I had echo out “Hello” and “World”. But in output its just echoing out me “Hello” because of using Die Function. I have kill the code after die()
keyword with brackets,when you write the function from that point it will kill rest of the page.

Code for exit function(Screenshot3):-

Screenshot3
<?php

echo 'Hello';

exit();

 echo ' World';

?>

Output (Screenshot4):-

die exit function php
Screenshot4
In this example doing same thing using exit() with brackets.

Code for die or exit function(Screenshot5):-

die or exit functions php
Screenshot5

<?php

echo 'Hello';

die('ERROR. PAGE HAS ENDED.');

echo ' World';

?>

Output (Screenshot6):-

die exit function
Screenshot6
In this example you can also echo out string or error in die functions keyword. Similarly you can do it with exit function as well.
Die and Exit functions are used in database connections. Let see it in example for connecting it with mysql database where ” mysql_connect(); ” function is used.

Code for Connect Mysql database (Screenshot7)

connect mysql database
Screenshot7
<?php



mysql_connect('localhost','root','') or die ('Could not connect to database.'');



echo 'connected!';



?>

output (Screenshot8):-

code for connection to mysql database
Screenshot8

In this example I have connected mysql database with my localhost of xampp. Now you don’t worry about connections of database which we will see it later on. Here we use function name ” mysql_connect(‘localhost’,'username’,'password’) ” where it depends on server my server which is localhost ,my username is root and my password is blank. Now in this example I have used die function if any of my variables like username, server , or password mismatch or database is not connected successfully it will show me error “Could not connect to database”.But here it is connected.

Code for connect mysql database with die or exit functions (Screenshot9):-

Code for connect mysql database with die or exit functions
Screenshot9

<?php



mysql_connect('localhost','alex','') or die ('Could not connect to database.'');



echo 'connected!';



?>

Output Screenshot10
connection failed mysql database
Screenshot10
In this example my username is mismatched because its “root” and I have changed it to “alex”, thus its not going to connect with database. You can see output with warning error or we can also echo out our own error i.e. “Could not connect to database”.

Code for connect mysql database with die or exit functions (Screenshot11):-

Screenshot11

<?php



@mysql_connect('localhost','alex','') or die ('Could not connect to database.'');



echo 'connected!';



?>


output (Screenshot12):-

connection with database
Screenshot12

In this example we just output our own error for that we just popin @ before function. So, This is user-friendly way to connecting to database or the killing page. In this we can also use exit function instead of die function. It will exactly to the same thing which we done with die functions.


No comments:

Post a Comment

Thanks For Comment Will get you Soon..

About Me

Welcome to Extra Tutorials! My name is Mohammed and I am the 22 year writer, website developer, and photographer behind the blog. Thanks for visiting! Tutorials Jackpot… In addition to Developer, I love to develop websites and I love to write. Starting a php Blog was inevitable for me. What began as a simple way to share all of my Tutorials with friends and family has developed into my Part time job.

Mohammed Padela

WHAT IS PHP PROGRAMMING

WHAT IS PHP PROGRAMMING
WHAT IS PHP PROGRAMMING

Follow Us

Popular Posts

Designed ByBlogger Templates