Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by AmitDiwan
Page 530 of 840
PHP program to check if the total number of divisors of a number is even or odd
To check if the total number of divisors of a number is even or odd, we need to count all divisors and determine if the count itself is even or odd. Algorithm The efficient approach is to iterate from 1 to the square root of the number. For each divisor i found, we get two divisors: i and number/i. However, if i equals number/i (perfect square case), we count it only once. Example Output Number: 100 Total divisors: 9 The count of divisors is odd Number: 16 Total divisors: ...
Read MorePHP program to check if a year is leap year or not
To check if a year is leap year or not in PHP, we need to apply the leap year rules. A year is a leap year if it is divisible by 400, or divisible by 4 but not by 100. Leap Year Rules The rules for determining a leap year are ? If a year is divisible by 400, it is a leap year If a year is divisible by 100 (but not 400), it is not a leap year If a year is divisible by 4 (but not 100), it is a leap year Otherwise, ...
Read MorePHP program to check if a number is prime or not
A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. To check if a number is prime in PHP, we can create a function that tests for divisibility by all numbers from 2 up to half of the given number. Example The following PHP function checks if a number is prime − Output It is a prime number How It Works The check_prime() function works by − First checking if the number is 1, which is ...
Read MorePHP program to split a given comma delimited string into an array of values
To split a given comma delimited string into an array of values, PHP provides several built-in functions like explode() and preg_split(). Both methods effectively convert a comma-separated string into an array. Using explode() Function The explode() function is the most straightforward method to split a string by a specific delimiter ? The array is Array ( [0] => 456 [1] => 789 [2] => 23 [3] => 4 [4] => 019 ) ...
Read MorePHP program to remove non-alphanumeric characters from string
To remove non-alphanumeric characters from a string in PHP, you can use the preg_replace() function with regular expressions. This function searches for a pattern and replaces it with specified content. Method 1: Using [^a-z0-9] Pattern The most common approach uses a negated character class to match anything that's not alphanumeric − The non-alphanumeric characters removed gives the string as Thisissampleonly The pattern /[^a-z0-9]/i means: [^a-z0-9] − Match any character that is NOT a lowercase letter or digit i flag − Makes the pattern case-insensitive Method 2: ...
Read MorePHP program to convert a given timestamp into time ago
In PHP, converting a timestamp to a human-readable "time ago" format is useful for displaying relative time in applications like social media feeds or comment systems. This can be achieved using a custom function that calculates the time difference and formats it appropriately. Example The following example demonstrates how to convert a timestamp into a "time ago" format ? The timestamp to time ago conversion is 10 minutes ago 1 hour ago 1 day ago How It Works The to_time_ago() function calculates the difference between the current time and ...
Read MorePHP program to calculate the total time given an array of times
In PHP, you can calculate the total time from an array of time strings by converting each time to seconds, summing them up, and converting back to HH:MM:SS format. The key is to parse time strings correctly and handle the arithmetic properly. Example Here's how to sum an array of time strings ? Total time: 24:38:54 How It Works The solution breaks down each time string using explode(':') to separate hours, minutes, and seconds. Each component is converted to seconds and added to the running total. Finally, the total ...
Read MorePHP program to generate a numeric one-time password
To generate a numeric one-time password in PHP, you can use the rand() function to randomly select digits from a string of numbers. This method ensures each digit is chosen randomly for better security ? Example Output The one time password generated is: 52471609 How It Works The generate_otp() function takes the desired length as a parameter. It defines a string containing digits 0-9 in a shuffled order. The function then iterates through the specified length, randomly selecting one digit at a time using rand() and substr(). Each selected ...
Read MorePHP program to compare float values
Comparing float values in PHP requires special attention due to floating-point precision issues. Direct equality comparison (==) can fail with floats, so we use epsilon-based comparison to check if values are "close enough". Basic Float Comparison The most reliable method uses abs() function with a small tolerance value (epsilon) ? The values are not same Why Direct Comparison Fails This example demonstrates why == comparison can be unreliable with floats ? Not equal using == Equal using epsilon method Comparison Function ...
Read MorePHP program to sort dates given in the form of an array
To sort dates given in the form of an array in PHP, you can use the usort() function with a custom comparison function. This approach leverages strtotime() to convert date strings into timestamps for accurate comparison. Example Output The dates in sorted order: Array ( [0] => 2090-12-06 [1] => 2020-09-23 [2] => 2009-11-30 [3] => 2002-09-11 ) How It Works The function compare_dates takes two date parameters and uses strtotime() to convert ...
Read More