How to Stop a Php File From Continuing

Using function for stop executing scriptThe PHP die function halts the current script execution. However, it lets you decide if you want to leave a message or simply set a status code that won't be visible to your users. So, in this post, you'll get to know about the purpose of the die function along with its usage in different situations.

Don't skip reading to see when the die() function gives leniency and where it abruptly breaks the script execution.

Contents

    • What Is PHP Die Function?
  • Coding Example for Ending the Script By Using Die()
    • – Using the Function With Objects
    • – Using Die() With Try, Catch, and Finally
    • – Using the Function for Securing Scripts
    • – Calling PHP die() Inside Destructor
  • FAQ
    • – How To Use the PHP Die Function?
    • – Does Die in PHP Skip the Cleanup Routine?
    • – Does die() in PHP Allow "Finally" To Run?
    • – Does PHP Die() Help in Securing Scripts?
    • – Can You Call the PHP Die Function Inside __destruct()?
  • Conclusion

What Is PHP Die Function?

The PHP die function is a built-in function of PHP that is often mistaken as PHP die command. Well, the stated function is used to stop the execution of the current script as it is an alias of the exit statement. So, whenever you want to end your program, simply call the PHP die function.

Coding Example for Ending the Script By Using Die()

For instance: you have an array of numbers. Now, you want to end the script when a particular number is reached while iterating through the given array. So, you'll specify the required condition and execute the die() function when the condition is fulfilled.

The coding representation attached here depicts the same example:

<?php
// creating an array of numbers
$numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
// iterating through the array
foreach($numbers as $num){
// printing numbers
echo "$num <br>";
// checking if the 6 number is reached
if($num == 6){
// executing the die() function
die("Sorry! Can't Continue Further!");
}
}
?>

See the output of the above code snippet here:

1
2
3
4
5
6
Sorry! Can't Continue Further!

– Using the Function With Objects

Suppose you have created a class and its object. Next, you've called one of the methods of the class. Now, you want to call another method and stop the current script after executing the same. Once you do this, you'll see that the destructor will be called even after executing the PHP die() function.

Here is a piece of code that supports the above example:

<?php
// creating a class
class Example
{
// defining the destructor
public function __destruct()
{
echo "Cleaning! The destructor has been called! <br>";
}
// defining a function
public function show()
{
echo "The show() function is running! <br>";
}
// defining another function
public function add($n1, $n2)
{
$res = $n1 + $n2;
echo "Result: $res <br>";
}
}
// creatin an object of the Example class
$obj = new Example();
// executing the methods
$obj->show();
$obj->add(4,7);
// executing the die() function
die("Time to end the script. Bye! <br>");
?>

The output will be similar to the one shared here:

The show() function is running!
Result: 11
Time to end the script. Bye!
Cleaning! The destructor has been called!

– Using Die() With Try, Catch, and Finally

For instance, you have created a function that throws an exception when the number passed to it is zero. So, you prefer to execute it in the try block. Next, you have created the catch and finally blocks as well. Now, you want to ensure that the logic explained above is true. Hence, you'll execute the die() statement in the try block that passes zero to the given function.

Lastly, you'll create another similar series of try, catch, and finally blocks. But here, you'll implement the function correctly and call the die() function.

The following block of code will help you create a situation like above and allow you to check the results:

<?php
// defining a custom function
function divide($num){
if($num == 0){
throw new Exception('Division by zero.');
}
return 2/$num;
}
// creating the first try, catch, and finally blocks
try{
// passing zero to the custom function
echo divide(0) . "<br>";
die("That's it.");
}
catch(Exception $e){
echo "Message: " . $e->getMessage() . "<br>";
}
finally{
echo "Finally Block! <br>";
}
// Creating the second series of try, catch, and finally blocks
try{
// passing six to the custom function
echo divide(6) . "<br>";
die("That's it.");
}
catch(Exception $e){
echo "Message: " . $e->getMessage() . "<br>";
}
finally{
echo "Finally Block! <br>";
}
// the output:
// Message: Division by zero.
// Finally Block!
// 0.33333333333333
// That's it.
?>

– Using the Function for Securing Scripts

Imagine that you have created some scripts for including the same in the main script. So, you don't want your users to execute the mini scripts without the main script. Therefore, you'll define a constant at runtime by using the define() function in the main script and include the mini scripts one after the other.

On the other hand, you'll check if the given constant has been defined in the main script. If the result comes false, you'll call the die() function with an appropriate message. In the other case, you'll allow PHP to load the given mini script inside the main script.

Please use the below piece of code to secure your scripts by using the PHP die() function:

<?php
// add this code at the beginning of the mini scripts
if(!defined('signal')) {
die("This script contains sensitive information. Meet us at Home Page.");
}
?>
<?php
// add this code anywhere in the main script
define("signal", true);
?>

– Calling PHP die() Inside Destructor

Please check the below coding block example that supports the above statement:

<?php
// creating a class
class Example1
{
// defining the destructor
public function __destruct()
{
echo "The first destructor has been called! <br>";
}
}
// creating another class
class Example2
{
// defining the destructor
public function __destruct()
{
echo "The second destructor has been called! <br>";
die("The script ends here.");
}
}
// creating objects of both classe
$obj = new Example1();
$obj2 = new Example2();
// output:
// The second destructor has been called!
// The script ends here.
?>

FAQ

– How To Use the PHP Die Function?

You can pass an informative message or a status code to the die function. The syntax of the PHP die function looks like this: die(message). But remember that the message will be printed on the screen before ending the script while the status code won't display anything.

– Does Die in PHP Skip the Cleanup Routine?

No, the PHP die() function doesn't skip the cleanup routine offered by the class destructors. So, if you create an object of a class, execute any of the methods, and call the die() function, the call to the destructor won't be skipped.

Therefore, you don't need to worry about getting the cleanup routine skipped in case you choose to execute the die() function.

– Does die() in PHP Allow "Finally" To Run?

If you are calling the PHP die() function in the try block then the results depend on the exception. It means that if an exception is thrown in the try block, the control will be passed to the catch block leading to the "finally" block while ignoring PHP die().

However, if an exception isn't thrown in the try block then the lines of code are executed sequentially. Therefore, the script will stop executing while ignoring even the "finally" block. So, it's all about the program control. If your program is designed in such a way that the control never reaches the die() function then know that your script is unstoppable.

– Does PHP Die() Help in Securing Scripts?

The PHP die() function helps in securing the scripts that aren't allowed to be called alone. Firstly, you'll need to define a condition based on the main script in the scripts that need protection. Next, you'll execute the die function if the condition isn't fulfilled.

Also, you'll be able to inform your users about the sensitivity of the data and even request them to visit another page of your website. It will ensure that your scripts remain secure while you gain the trust of your audience.

– Can You Call the PHP Die Function Inside __destruct()?

Yes, you can call the PHP die() function inside the destructor. But remember that it won't allow the other destructors to execute that follow the destructor calling the die() function. Consequently, you'll miss the cleanup routine for the rest of the objects.

Conclusion

Indeed, the PHP die function allows you to stop the script execution while keeping your users informed or without letting anyone know about the issue. Now, read the details listed below to summarize the facts that you have discovered and learned from the above article:

  • The PHP die() function halts the execution of your current script and can be called inside a destructor
  • The PHP die function is an alias of the exit statement
  • The PHP die() function accepts either a message string or an integer status code as an argument
  • The message passed to the die() function will be printed on the screen while the status code won't be visible to the users
  • The PHP die function can be used to protect the PHP scripts

What is php die functionIt is not recommended to use the die() function at the end of every PHP script. However, it would be good to use the same if it enhances the overall efficiency of your program or script.

  • Author
  • Recent Posts

Position is Everything

testaferamplon.blogspot.com

Source: https://www.positioniseverything.net/php-die/

0 Response to "How to Stop a Php File From Continuing"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel