Found 1060 Articles for PHP

PHP program to compare float values

AmitDiwan
Updated on 02-Jul-2020 06:45:05

149 Views

To compare float values in PHP, the code is as follows −Example Live DemoOutputThe values are not sameThree values are defined that are floating point numbers. The absolute values of these numbers are compared and relevant message is displayed on the console.

PHP program to sort dates given in the form of an array

AmitDiwan
Updated on 02-Jul-2020 06:43:22

160 Views

To sort dates given in the form of an array in PHP, the code is as follows −Example Live DemoOutputThe dates in sorted order is Array (    [0] => 2090-12-06    [1] => 2020-09-23    [2] => 2002-09-11    [3] => 2009-30-11 )A function named ‘compare_dates’ takes two time formats as parameters. If the first time format is greater than the second one, it returns -1. Otherwise, if the first time format is lesser than the second time, it returns 1 and if both the conditions are not true, the function returns 0. An array is defined that contains various ... Read More

PHP program to compare two dates

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

236 Views

To compare two dates in PHP, the code is as follows −Example Live DemoOutput2020-11-22 is later than 2011-11-22Two dates of ‘DateTime’ format are generated and checked to see which one is sooner or later. If the first date is later, relevant message is printed on the console. Otherwise, a message indicating that the first date is sooner than the second date is printed on the console.

PHP program to find number of days in every week between two given date ranges

AmitDiwan
Updated on 02-Jul-2020 06:40:19

630 Views

To find number of days in every week between two given date ranges in PHP, the code is as follows −Example Live DemoOutputThe number of days between the given range isArray (    [Monday] => 5    [Tuesday] => 5    [Wednesday] => 5    [Thursday] => 5    [Friday] => 4    [Saturday] => 4    [Sunday] => 4 )Two dates of ‘DateTime’ type is defined and an array of days of a week is defined, where initially the count of all days of a week are 0. The dates are converted to a time format and timestamp variable is ... Read More

PHP program to find the total number of date between any two given dates

AmitDiwan
Updated on 02-Jul-2020 06:38:28

214 Views

The ‘date_diff’ function can be used to get the difference between two dates. It is a built-in function that returns a DateInterval object if a specific number of days has been found, and returns False if the days is not found.Example Live DemoOutputThe day difference is: +60 daysTwo dates are defined using the ‘date_create’ function and the difference/ the number of days that are present between these two dates can be computed using the ‘date_diff’ function. It is assigned to a variable and printed on the console.

PHP program to print continuous character pattern triangle

AmitDiwan
Updated on 02-Jul-2020 06:37:21

429 Views

To print continuous character pattern triangle in PHP, the code is as follows −Example Live DemoOutputA B C D E F G H I J K L M N O P Q R S T UThis is similar to generating a star or a number pattern, the only difference being, continuous characters of English alphabets are generated instead of stars or numbers. The function ‘continuous_alphabets’ is defined that takes the limit as a parameter. The limit value is iterated over and the character is printed and relevant line breaks are also generated in between. The function is called by passing this ... Read More

PHP program to print continuous numbers in the form of triangle

AmitDiwan
Updated on 02-Jul-2020 06:35:25

299 Views

To print continuous numbers in the form of triangle in PHP, the code is as follows −Elements Live DemoOutput1 2 3 4 5 6 7 8 9 10This is similar to generating a star or a number pattern, the only difference being, continuous numbers are generated instead of stars or repeated numbers. The function ‘continuous_pattern’ is defined that takes the limit as a parameter. The limit value is iterated over and the number is printed and increment. The relevant line breaks are also generated in between. The function is called by passing this limit value and the relevant output is generated on the console.

PHP program to print the number pattern

AmitDiwan
Updated on 02-Jul-2020 06:34:08

2K+ Views

To print the number pattern in PHP, the code is as follows −Example Live DemoOutput1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6This is similar to generating a star pattern, the only difference being, numbers are generated instead of stars. The function ‘num_pattern’ is defined that takes the limit as a parameter. The limit value is iterated over and the number is printed and relevant line breaks are also generated in between. The function is called by passing this limit value and the relevant output is generated on the console.

PHP program to print a pattern of pyramid

AmitDiwan
Updated on 02-Jul-2020 06:32:21

2K+ Views

Let us see an example to print a pattern of pyramid in PHP −Example Live DemoOutput         *         * *        * * *       * * * *      * * * * *     * * * * * *    * * * * * * *A function named ‘print_pattern’ is defined that iterates through the number of rows through which the pattern needs to be generated. Relevant line breaks are also echoed using the ‘echo’ attribute. The function is called by passing the number of rows for which the pattern needs to be generated.

What are multidimensional associative arrays in PHP? How to retrieve values from them?

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

2K+ Views

Multidimensional arrays store multiple arrays whereas associative arrays store key-value pairs as data. Grouped relation between data can be stored in multidimensional associative arrays.Example Live DemoOutputJoe 20An array is defined that contains various attributes of an employee. The employee array is accessed based on the index names and the data is displayed on the console.

Advertisements