AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 528 of 840

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

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 216 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 900 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 261 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

PHP program to calculate the sum of square of first n natural numbers

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 709 Views

To calculate the sum of square of first n natural numbers in PHP, we can use a simple loop to iterate through each number and add its square to the running total. Example Here's a PHP function that calculates the sum of squares ? Output The sum of square of first 5 natural numbers is 55 How It Works A function named sum_of_squares is defined that takes the limit up to which square of natural numbers needs to be found. In this function, a sum value is initialized ...

Read More

PHP program to calculate the repeated subtraction of two numbers

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 321 Views

Repeated subtraction is a method to find how many times one number can be subtracted from another until the result becomes less than the subtractor. This is mathematically equivalent to division and finding the quotient. Example Here's a PHP program that calculates repeated subtraction using a recursive approach ? The repeated subtraction results in 18 How It Works The function repeated_sub uses the Euclidean algorithm principle. It checks if the first value divides evenly by the second value. If yes, it returns the floor of the quotient. Otherwise, it ...

Read More

PHP program to find the sum of odd numbers within a given range

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

In PHP, you can find the sum of odd numbers within a given range using different approaches. Here's a mathematical approach that uses a formula to calculate the sum efficiently. Using Mathematical Formula This method calculates the sum of odd numbers using the mathematical property that the sum of first n odd numbers equals n²? The sum of odd natural numbers between 3 and 23 is 169 Using Loop Method A simpler approach using a loop to iterate through the range and add odd numbers? ...

Read More
Showing 5271–5280 of 8,392 articles
« Prev 1 526 527 528 529 530 840 Next »
Advertisements