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.
For this, you can use CHAR_LENGTH(). Use RAND() for random records. Let us first create a table −mysql> create table DemoTable (Subject text); Query OK, 0 rows affected (0.61 sec)ExampleInsert some records in the table using insert command −mysql> insert into DemoTable values('C'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('MySQL'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Java'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('MongoDB'); Query OK, 1 row affected (0.59 sec) mysql> insert into DemoTable values('RubyOnRails'); Query OK, 1 row affected (0.25 sec) mysql> insert ... Read More
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
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.
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
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.
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
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.
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.
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.