Found 1060 Articles for PHP

PHP program to check if the total number of divisors of a number is even or odd

AmitDiwan
Updated on 02-Jul-2020 06:58:31

231 Views

To check if the total number of divisors of a number is even or odd, the code is as follows −Example Live DemoOutputIt is an odd numberA function named ‘divisor_count’ is defined that gives the number of divisors of a given number that is passed as a parameter to the function. Now, each of these divisors is checked to see if it can be completely divided by 2, if yes, it is an even divisor, and otherwise, it is an odd divisor. Relevant message is displayed on the console.

PHP program to check if a year is leap year or not

AmitDiwan
Updated on 20-Dec-2021 12:29:09

13K+ Views

To check if a year is leap year or not in PHP, the code is as follows −Example Live DemoOutputIt is not a leap yearA function named ‘year_check’ is defined that takes a year as a parameter. It checks to see if the year can be divided by 400 or 4 completely, if yes, it means it is a leap year. Otherwise, it is a leap year. The value for year is defined outside the function, and the function is called by passing this year as a parameter to it. Relevant output is displayed on the console.

PHP program to check if a number is prime or not

AmitDiwan
Updated on 13-Sep-2023 14:35:19

41K+ Views

To check if a number is prime or not, the code is as follows −Example Live DemoOutputIt is a prime numberA function named check_prime() is used to check if the number is prime or not. A number that needs to be checked for being prime is passed as a parameter to the function. The number is defined and this function is called by passing the number as parameter. Relevant message is displayed on the console.

PHP program to split a given comma delimited string into an array of values

AmitDiwan
Updated on 02-Jul-2020 06:54:26

2K+ Views

To split a given comma delimited string into an array of values, the PHP code is as follows −Example Live DemoOutputThe array is Array (    [0] => 456    [1] => 789    [2] => 23    [3] => 4    [4] => 019 ) The array is Array (    [0] => 00    [1] => 876    [2] => 5432    [3] => 1234    [4] => 0 )A string of numbers is defined and the ‘preg_split’ function is used to separate the numbers based on the occurance of ‘/’ or ‘.’. The split array is now printed. Another method to split the given number in the form of a string is to use the explode function. It splits the array based on the occurance of a comma.

PHP program to remove non-alphanumeric characters from string

AmitDiwan
Updated on 02-Jul-2020 06:53:14

998 Views

To remove non-alphanumeric characters from string, the code is as follows −Example Live DemoOutputThe non-alphanumeric characters removed gives the string as ThisissampleonlyThe function ‘preg_replace’ is used to remove alphanumeric characters from the string. A regular expression is used to filter out the alphanumeric characters. The string is previously defined and the function ‘preg_replace’ is called on this and the reformed string is displayed on the console.Example Live DemoOutputThe non-alphanumeric characters removed gives the string as Thisisasample_onlyThe only difference here is that a different regular expression is used. It means the same as the previous regular expression, but written in a different fashion.Read More

PHP program to compute the execution time of a PHP script

AmitDiwan
Updated on 02-Jul-2020 06:51:46

683 Views

To compute the execution time of a PHP script, the code is as follows −Example Live DemoOutputThe execution time of the PHP script is : 1.69 secThe ‘microtime’ function can be used to check the time taken by a PHP script to execute completely. When the code begins execution, the time is recorded, and once the code is completed, another timestamp is generated and the difference between the end and star time is the time taken by the script to complete its execution.

PHP program to convert a given timestamp into time ago

AmitDiwan
Updated on 02-Jul-2020 06:50:46

950 Views

To convert a given timestamp into time ago, the code is as follows −Example Live DemoOutputThe timestamp to time ago conversion is 10 minutes agoA function named ‘to_time_ago’ is defined that checks the difference between the time passed as parameter to the function and the time function. If this difference is found to be less than 1, it returns that the time passed just a second ago. Otherwise, the year, month, day, hour, minute, and second is generated in an array. A ‘foreach’ loop is used to iterate over the array previously generated. The time difference is computed and printed on ... Read More

PHP program to calculate the total time given an array of times

AmitDiwan
Updated on 02-Jul-2020 06:48:50

2K+ Views

The ‘strtotime’ function can be used to convert the given string into a time format. Let us see an example −Example Live DemoOutputThe total time is :-441915:-12:-58An array that contains time format data is defined and the ‘strtotime´function is used to convert the string into a time format. The ‘foreach’ loop is used to iterate over the elements in the time format array and the elements are added.The hours is calculated by dividing the value computed by 3600. The minutes is calculated by dividing the value computed by product of hours calculated and 3600. The seconds is calculated by dividing the ... Read More

PHP program different ways to generate a random string

AmitDiwan
Updated on 02-Jul-2020 06:47:41

375 Views

Using bin2hex functionExample Live DemoOutputThe randomly generated string is : f1db16115fa93b98493d388bA number is defined and the bin2hex function is called on this number. Inside the bin2hex function the ‘random_bytes’ function is called on this number. The generated random string is printed on the console.Using hashing functionExample Live DemoOutputThe randomly generated string using hashing function sha1 is :9a4a73c35ac034832332977f3d5accd8eace5260A number is defined by calling the ‘rand’ function. The sha1 hashing function is called on this randomly generated number. The generated random string is printed on the console.Using built-in function uniqidExample Live DemoOutputThe randomly generated string using uniqid function is : 5ed4b884cef34A number is defined by ... Read More

PHP program to generate a numeric one-time password

AmitDiwan
Updated on 02-Jul-2020 06:47:16

1K+ Views

To generate a numeric one-time password in PHP, the code is as follows −Example Live DemoOutputThe one time password generated is :52471609A function named ‘generate_otp’ is defined that takes the length as a parameter. This is the length of the password that needs to be generated. A number that contains 0 to 9 numbers is defined and the length is iterated over and a random number that contains these 0 to 9 numbers randomly is generated. The length is defined and the function is called on this length. This generates a numeric password and displays it on the console.

Advertisements