Articles on Trending Technologies

Technical articles with clear explanations and examples

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 267 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

Swap two variables in one line in C/C++, Python, PHP and Java

Hafeezul Kareem
Hafeezul Kareem
Updated on 15-Mar-2026 690 Views

In this tutorial, we are going to learn how to swap two variables in different languages. Swapping means interchanging the values of two variables. Let's see an example. Input a = 3 b = 5 Output a = 5 b = 3 Let's see how to achieve this in different programming languages. Python We can swap variables with one line of code in Python using tuple unpacking − Example # initializing the variables a, b = 3, 5 # printing before swapping print("Before swapping:-", a, b) ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 717 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 340 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

PHP program to find the sum of first n natural numbers that are divisible by a number 'x' or a number 'y'

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 234 Views

To find the sum of first n natural numbers that are divisible by a number 'x' or a number 'y', we use the inclusion-exclusion principle. This mathematical approach counts numbers divisible by x, adds numbers divisible by y, then subtracts numbers divisible by both (LCM of x and y) to avoid double counting. Example The following code demonstrates how to calculate this sum − Output The sum of first 11 natural numbers divisible by 2 or 5 is 50 How It Works The function uses the arithmetic progression ...

Read More

PHP program to find the numbers within a given array that are missing

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 656 Views

To find missing numbers from a range of positive integers, we can compare an existing array with an expected sequence. Here's a PHP program that finds the first few missing positive numbers from a given array. Example Output The missing numbers in the array are: 1 2 3 4 5 How It Works The missing_nums function first sorts the input array to process numbers in ascending order. It skips any negative numbers or zeros since we're looking for missing positive integers. The function then compares each expected number ...

Read More

PHP program to find the first natural number whose factorial can be divided by a number 'x'

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 267 Views

To find the first natural number whose factorial can be divided by a number 'x', we need to calculate factorials incrementally and check divisibility until we find a match. Example The following PHP program finds the first natural number whose factorial is divisible by a given number ? Output The first natural number whose factorial can be divided by 16 is 4 How It Works The function factorial_num() starts with $fact_num = 1 and multiplies it by consecutive natural numbers (1, 2, 3, ...). At each step, it ...

Read More

PHP program to find if a number is present in a given sequence of numbers

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 414 Views

In PHP, you can check if a specific number exists in an arithmetic sequence by comparing the target number with the sequence parameters. This involves checking if the number can be reached by adding a common difference repeatedly to a starting number. Example The following code demonstrates how to find if a number is present in a given arithmetic sequence − Is the number present in the sequence? Yes, it is present in the sequence How It Works The contains_in_sequence() function checks if a target number exists in an ...

Read More
Showing 22341–22350 of 61,297 articles
Advertisements