PHP Articles

Page 37 of 81

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 758 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 2K+ 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 207 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 890 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

PHP program to check if a string has a special character

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

In PHP, you can check if a string contains special characters using regular expressions with the preg_match() function. This is useful for input validation, password strength checking, or data sanitization. Example The following example demonstrates how to check for special characters in a string − Output String has special characters How It Works The check_string() function uses preg_match() to search for any special characters defined in the regular expression pattern. The pattern /[@_!#$%^&*()?\/|}{~:]/ matches common special characters including underscore, dollar sign, exclamation mark, and others. When preg_match() ...

Read More

PHP program to replace a word with a different symbol in a sentence

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 257 Views

To replace a word with a different symbol in a sentence, PHP provides the str_replace() function. This function searches for specified words and replaces them with new values. Syntax str_replace(search, replace, subject, count) Parameters The str_replace() function accepts the following parameters ? search − The value to search for (can be string or array) replace − The replacement value (can be string or array) subject − The string or array to search in count − Optional variable to store the number of replacements Example ...

Read More
Showing 361–370 of 802 articles
« Prev 1 35 36 37 38 39 81 Next »
Advertisements