Articles on Trending Technologies

Technical articles with clear explanations and examples

PHP Interaction between finally and return

Malhar Lathkar
Malhar Lathkar
Updated on 15-Mar-2026 2K+ Views

In PHP, there is a peculiar behavior of the finally block when either the try block or catch block contains a return statement. Normally, a return statement causes program control to go back to the calling position. However, in functions with try/catch blocks containing return statements, the finally block is always executed first before returning. Example In the following example, the div() function has a try-catch-finally construct. The try block returns the result of division when no exception occurs. In case of an exception, the catch block returns an error message. However, in either case, the statement in ...

Read More

PHP Exception Handling with finally

Malhar Lathkar
Malhar Lathkar
Updated on 15-Mar-2026 441 Views

In PHP, the finally block is used with try-catch statements to ensure that certain code always executes, regardless of whether an exception occurs or not. This block appears after the catch block or can be used instead of a catch block. Syntax The finally block can be used in two ways − try { // Code that may throw an exception } catch (Exception $e) { // Handle exception } finally { // Always executed } // OR without catch block try { ...

Read More

PHP Extending Exceptions

Malhar Lathkar
Malhar Lathkar
Updated on 15-Mar-2026 1K+ Views

Exception class implements Throwable interface and is base class for all Exception classes, predefined exceptions as well as user defined exceptions. The Exception class defines some final (non-overridable) methods to implement those from Throwable interface, and __toString() method that can be overridden to return a string representation of Exception object. Exception Class Methods Method Description final public function getMessage() message of exception final public function getCode() code of exception final public function getFile() source filename final public function getLine() source line final public ...

Read More

PHP declare Statement

Malhar Lathkar
Malhar Lathkar
Updated on 15-Mar-2026 782 Views

The PHP declare statement allows you to control the execution environment of a script block. Its syntax is similar to other control structures like while, for, and foreach loops. Syntax declare (directive) { statement1; statement2; // ... } The behavior of the block is defined by the type of directive. Three types of directives can be used: ticks, encoding, and strict_types. Ticks Directive A tick is a special event that occurs after a certain number of tickable statements are executed. You ...

Read More

PHP program to find the maximum element in an array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 3K+ Views

In PHP, finding the maximum element in an array can be accomplished using a custom function that iterates through the array or by using PHP's built-in max() function. Here are different approaches to find the maximum value. Using Custom Function We can create a custom function that compares each element to find the maximum value ? The highest value of the array is 91 Using Built-in max() Function PHP provides the max() function that directly returns the highest value from an array ? ...

Read More

PHP program to check if a given number is present in an infinite series or not

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 224 Views

To check if a given number is present in an infinite arithmetic series, we need to determine if the number can be reached by starting from a base value and adding a common difference repeatedly. The PHP code below demonstrates this logic − Example Output The number is not present in the infinite series How It Works The function contains_val takes three parameters − $start − The starting number of the series $target − The number we want to check $diff − The common difference in the ...

Read More

PHP program to find the minimum element in an array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 3K+ Views

To find the minimum element in an array in PHP, you can use a custom function or built−in functions. Here are different approaches to accomplish this task. Using Custom Function The manual approach involves iterating through the array and comparing each element ? The lowest value of the array is 0 Using min() Function PHP provides a built−in min() function that directly returns the minimum value ? The minimum value is: 0 How It Works The custom function get_min_value() works ...

Read More

PHP program to find the length of the last word in the string

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

In PHP, you can find the length of the last word in a string by locating the last space character and extracting the substring that follows it. This approach uses built-in string functions like strrpos() and substr(). Example Here's a PHP function that finds the length of the last word in a string ? The length of the last word is 3 The length of the last word is 6 How It Works The function last_word_len() takes a string as parameter and follows these steps ? $position ...

Read More

PHP program to find the first word of a sentence

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 907 Views

To find the first word of a sentence in PHP, you can use the built-in strtok() function. This function splits a string based on a delimiter and returns the first token. Example Here's how to extract the first word from a sentence ? Output The first word of the string is Hi How It Works The strtok() function splits the string at the first occurrence of the specified delimiter (space in this case). It returns only the first token, which is the first word before any space character. ...

Read More

PHP program to sum the digits in a number

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 4K+ Views

To sum the digits in a number, we can treat the number as a string and iterate through each character, converting it back to an integer and adding to our sum. Example Output The sum of digits is 11 How It Works The function sum_of_digits takes a number as a parameter. It initializes a variable $sum to 0, then iterates through each character of the number using strlen() to get the length. Each character is automatically converted to an integer when added to the sum. For the number ...

Read More
Showing 22331–22340 of 61,297 articles
Advertisements