PHP finally Keyword

PHP fn Keyword
PHP final Keyword

In this article, you will learn about finally keyword in PHP. The finally keyword in PHP is used in try…finally and try…catch…finally structures to run a block of code whether or not an exception occurred.

examples of the FINALLY keyword

Example 1. In this example, we run some code regardless of whether an exception was thrown.

<?php
echo "Starting the process.";
try {
  // Select randomly between 0 and 1, throw an exception if 1 is selected.
  $random = rand(0, 1);
  if($random == 1) {
    throw new Exception("Exception");
  }
} finally {
  echo "Process complete";
}
?>

PHP fn Keyword
PHP final Keyword
en English
X
Scroll to Top