logo CBCE Skill INDIA

Welcome to CBCE Skill INDIA. An ISO 9001:2015 Certified Autonomous Body | Best Quality Computer and Skills Training Provider Organization. Established Under Indian Trust Act 1882, Govt. of India. Identity No. - IV-190200628, and registered under NITI Aayog Govt. of India. Identity No. - WB/2023/0344555. Also registered under Ministry of Micro, Small & Medium Enterprises - MSME (Govt. of India). Registration Number - UDYAM-WB-06-0031863

Using die() function!


Using die() Function

The die() function in PHP is an alias for exit(). It is used to terminate the execution of a script and display a message. When die() is called, the script stops running, and the specified message (if provided) is sent to the browser.

 

Here's the basic syntax of the die() function:

die(string $message = '');
  • $message: Optional. The message to be displayed when the script terminates. Default is an empty string.

 

Here's an example of using die() to display an error message and stop the script execution:

<?php
// Some code that may cause an error
$number = 10 / 0;

// Check for a division by zero error
if (error_get_last()) {
    die("Error: Division by zero occurred.");
}

// Continue with the rest of the script
echo "This line will not be executed.";
?>

 

In this example, if the division by zero occurs, the die() function is called with an error message, and the script terminates at that point. The message is displayed to the user, and any subsequent code after the die() statement will not be executed.

It's worth noting that using die() or exit() to terminate script execution should be done judiciously. In many cases, a more structured approach to error handling, such as exceptions or custom error handlers, may be preferable for better code organization and maintainability. die() is often used for quick and simple debugging or error reporting in smaller scripts.

 

Thank you.

Popular Post:

Give us your feedback!

Your email address will not be published. Required fields are marked *
0 Comments Write Comment