Found 1046 Articles for PHP

PHP program to find the maximum element in an array

AmitDiwan
Updated on 17-Aug-2020 12:21:27

2K+ Views

To find the maximum element in an array, the PHP code is as follows −Example Live DemoOutputThe highest value of the array is91A function named ‘get_max_value()’ is defined, that takes an array as parameter. Inside this function, the count function is used to find the number of elements in the array, and it is assigned to a variable −$n = count($my_array);The first element in the array is assigned to a variable, and the array is iterated over, and adjacent values in the array are compared, and the highest value amongst all of them is given as output −$max_val = $my_array[0]; for ... Read More

PHP program to check if a given number is present in an infinite series or not

AmitDiwan
Updated on 17-Aug-2020 12:17:49

79 Views

To check if a given number is present in an infinite series or not, the PHP code is as follows −Example Live DemoOutputThe number is not present in the infinite seriesAbove, three variables are defined, and the function is called by passing these three values −$m = 3; $n = 5; $o = 9;A function named ‘contains_val’ is defined, that takes these three variables. The first two variables are compared, and if they are equal, ‘true’ is returned. Otherwise, if the different between the first two variables with the product of the third number is greater than 0 and the different ... Read More

PHP program to find the first ‘n’ numbers that are missing in an array

AmitDiwan
Updated on 17-Aug-2020 12:15:07

160 Views

To find the first ‘n’ numbers that are missing in an array, the PHP code is as follows −Example Live DemoOutputThe missing values of the array are 1 2 3 4 5In the above code, a function named ‘missing_values´ is defined, that takes the array, length, and the first few numbers that are missing from the array.A variable is assigned to 0 and checked if the first few numbers that need to be found is 0 or more. If it is 0, it is incremented.A count is assigned to 0, and curr value is assigned to 1. Next, the count value ... Read More

PHP program to change the date format

AmitDiwan
Updated on 17-Aug-2020 12:10:00

129 Views

To change date format in PHP, the code is as follows −Example Live DemoOutput1970-01-01 00:00:00A function named ‘string_convert’ is used in PHP that takes a date as a parameter. The ‘strtotime’ function is used to convert English text timing into a UNIX timestamp −$sec = strtotime($my_date); $my_date = date("Y-m-d H:i", $sec); $my_date = $my_date . ":00";This date is printed on the screen. Outside the function, the date time is defined, and the function is called on this parameter and the output is displayed on the screen −echo $my_date;

PHP program to find the minimum element in an array

AmitDiwan
Updated on 17-Aug-2020 12:04:08

2K+ Views

To find the minimum element in an array, the PHP code is as follows −Example Live DemoOutputThe lowest value of the array is 0A function named ‘get_min_value()’ takes an array as parameter. Inside this function, the count function is used to find the number of elements in the array, and it is assigned to a variable −$n = count($my_array);The first element in the array is assigned to a variable, and the array is iterated over, and adjacent values in the array are compared, and the lowest value amongst all of them is given as output −$min_val = $my_array[0]; for ($i = ... Read More

PHP program to delete an element from the array using the unset function

AmitDiwan
Updated on 17-Aug-2020 12:01:57

286 Views

To delete an element from the array using the unset function, the PHP code is as follows −Example Live DemoOutputAfter deleting the element, the array isArray (    [0] => Joe [1] => Ben [2] => Mary [3] => Barun [5] =>    Mona )Above, an array is defined, with multiple strings −$my_array = array("Joe", "Ben", "Mary", "Barun", "Sona", "Mona");The unset function is used, by specifying the index of the element in the array that needs to be deleted. After the element is deleted, the output/array is printed on the screen −unset($my_array[4]); print_r("After deleting the element, the array is"); print_r ($my_array);

PHP program to find the length of the last word in the string

AmitDiwan
Updated on 17-Aug-2020 12:00:20

740 Views

To find the length of the last word in the string, the PHP code is as follows −Example Live DemoOutputThe length of the last word is 3 The length of the last word is 6A PHP function named ‘last_word_len’ is defined, that takes a string as the parameter −function last_word_len($my_string) { // }The first occurrence of the space inside another string is found by using the ‘strrpos’ function. If that position is present, it is assigned to 0. Otherwise, it is incremented by 1 −$position = strrpos($my_string, ' '); if(!$position){    $position = 0; } else{    $position ... Read More

PHP program to find the first word of a sentence

AmitDiwan
Updated on 17-Aug-2020 11:57:19

555 Views

To find the first word of a sentence, the PHP code is as follows −Example Live DemoOutputThe first word of the string is HiA string is defined at first −$my_string = 'Hi there, this is a sample statement';The ‘strtok’ function is an in-built function that is used to split a string into specified number of parts. Before the first space occurs, the string needs to be split. This split string’s first part is displayed as the output on the screen −echo "The first word of the string is ". strtok($my_string, " ");

PHP program to find the number of characters in the string

AmitDiwan
Updated on 17-Aug-2020 11:55:55

740 Views

To find the number of characters in the string, the PHP code is as follows −Example Live DemoOutputThe number of characters in the string is 27Above, a string is defined that has more than the required spaces in between −$my_string = "Hi there, this is a sample ";Next, the length of the string is computed using the ‘strlen’ function. This length is assigned to a variable. This will give the characters present in the string including the spaces as well −$len = mb_strlen($my_string);The length computed is then printed on the screen −print_r("The number of characters in the string is "); echo ... Read More

PHP program to sum the digits in a number

AmitDiwan
Updated on 17-Aug-2020 11:54:11

4K+ Views

To sum the digits in a number, the PHP code is as follows −Example Live DemoOutputThe sum of digits is 11Above, a function named ‘sum_of_digits’ is defined, that takes an integer as a parameter.$my_num = "65";It first declares the variable sum as 0 and then iterates through the length of the declared integer, and adds the element at the ‘i’th place to the sum. Once the iteration is complete, this function returns the sum as output. Outside the function, the value is declared and the function is called by passing this element as the parameter −function sum_of_digits($my_num){    $sum = 0; ... Read More

Advertisements