w3resource
Java Programming Exercises

PHP Basic - Exercises, Practice, Solution

PHP basic [102 exercises with solution]

1. Write a PHP script to get the PHP version and configuration information. Go to the editor
Click me to see the solution

2. Write a PHP script to display the following strings. Go to the editor
Sample String :
'Tomorrow I \'ll learn PHP global variables.'
'This is a bad command : del c:\\*.*'

Expected Output :
Tomorrow I 'll learn PHP global variables.
This is a bad command : del c:\*.* 
Click me to see the solution

3. $var = 'PHP Tutorial'. Put this variable into the title section, h3 tag and as an anchor text within an HTML document. Go to the editor
Sample Output :

PHP Tutorial
PHP, an acronym for Hypertext Preprocessor, is a widely-used open source general-purpose scripting language. It is a cross-platform, HTML embedded server-side scripting language and is especially suited for web development.
Go to the PHP Tutorial.

Click me to see the solution

4. Create a simple HTML form and accept the user name and display the name through PHP echo statement. Go to the editor

Sample output of the HTML form :

sample html form

Click me to see the solution

5. Write a PHP script to get the client IP address. Go to the editor
Click me to see the solution

6. Write a simple PHP browser detection script. Go to the editor
Sample Output : Your User-Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36
Click me to see the solution

7. Write a PHP script to get the current file name. Go to the editor
Click me to see the solution

8. Write a PHP script, which will return the following components of the url 'https://www.w3resource.com/php-exercises/php-basic-exercises.php'. Go to the editor

List of components : Scheme, Host, Path
Expected Output :
Scheme : http
Host : www.w3resource.com
Path : /php-exercises/php-basic-exercises.php
Click me to see the solution

9. Write a PHP script, which changes the color of the first character of a word. Go to the editor
Sample string : PHP Tutorial
Expected Output :
PHP Tutorial
Click me to see the solution

10. Write a PHP script, to check whether the page is called from 'https' or 'http'. Go to the editor
Click me to see the solution

11. Write a PHP script to redirect a user to a different page . Go to the editor
Expected output : Redirect the user to https://www.w3resource.com/
Click me to see the solution

12. Write a simple PHP program to check that emails are valid. Go to the editor
Hints : Use FILTER_VALIDATE_EMAIL filter that validates value as an e-mail address.
Note : The PHP documentation does not say that FILTER_VALIDATE_EMAIL should pass the RFC5321.
Click me to see the solution

13. Write a e PHP script to display string, values within a table. Go to the editor
Note : Use HTML table elements into echo.
Expected Output :
php table
Click me to see the solution

14. Write a PHP script to display source code of a webpage (e.g. "http://www.example.com/"). Go to the editor
Click me to see the solution

15. Write a PHP script to get last modified information of a file. Go to the editor
Sample filename : php-basic-exercises.php
Sample Output : Last modified Monday, 26th June, 2017, 12:43pm
Click me to see the solution

16. Write a PHP script to count number of lines in a file.
Note : Store a text file name into a variable and count the number of lines of text it has. Go to the editor
Click me to see the solution

17. Write a PHP script to print current PHP version. Go to the editor
Note : Do not use phpinfo() function.
Click me to see the solution

18. Write a PHP script to delay the program execution for the given number of seconds. Go to the editor
Click me to see the solution

19. Arithmetic operations on character variables : $d = 'A00'. Using this variable print the following numbers. Go to the editor
Sample Output :
A01
A02
A03
A04
A05
Click me to see the solution

20. Write a PHP script to get the last occurred error. Go to the editor
Click me to see the solution

21. Write a PHP function to test whether a number is greater than 30, 20 or 10 using ternary operator. Go to the editor
Click me to see the solution

22. Write a PHP script to get the full URL. Go to the editor
Click me to see the solution

23. Write a PHP script to compare the PHP version. Go to the editor
Note : Use version_compare() function and PHP_VERSION constant.
Click me to see the solution

24. Write a PHP script to get the name of the owner of the current PHP script. Go to the editor
Click me to see the solution

25. Write a PHP script to get the document root directory under which the current script is executing, as defined in the server's configuration file. Go to the editor
Click me to see the solution

26. Write a PHP script to get the information about the operating system PHP is running on. Go to the editor
Click me to see the solution

27. Write a PHP script to print out all the credits for PHP. Go to the editor
Click me to see the solution

28. Write a PHP script to get the directory path used for temporary files. Go to the editor
Click me to see the solution

29. Write a PHP script to get the names of the functions of a module. Go to the editor
Note : Find XML, JSON functions etc.
Click me to see the solution

30. Write a PHP script to get the time of the last modification of the current page Go to the editor
Click me to see the solution

31. Write a PHP program to swap two variables. Go to the editor
Click me to see the solution

32. Write a PHP program to check if a number is an Armstrong number or not. Return true if the number is Armstrong otherwise return false. Go to the editor
Note: An Armstrong number of three digits is an integer so that the sum of the cubes of its digits is equal to the number itself. For example, 153 is an Armstrong number since 1**3 + 5**3 + 3**3 = 153
Click me to see the solution

33. Write a PHP program to convert word to digit. Go to the editor
Input: zero;three;five;six;eight;one
Output: 035681
Click me to see the solution

34. Write a PHP program to check the bits of the two given positions of a number are same or not. Go to the editor
112 - > 01110000
Test 2nd and 3rd position
Result: True
Test 4th and 5th position
Result: False
Click me to see the solution

35. Write a PHP program to remove duplicates from a sorted list. Go to the editor
Input: (1,1,2,2,3,4,5,5)
Output: (1,2,3,4,5)
Click me to see the solution

36. Write a PHP program to test if a given string occurs at the end of another given string. Go to the editor
Click me to see the solution

37. Write a PHP program to compute the sum of the prime numbers less than 100. Go to the editor
Note: There are 25 prime numbers are there in less than 100.
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 and sum of all these numbers is 1060.
Click me to see the solution

38. Write a PHP program to valid an email address. Go to the editor
Click me to see the solution

39. Write a PHP program to get the size of a file. Go to the editor
Click me to see the solution

40. Write a PHP program to calculate the mod of two given integers without using any inbuilt modulus operator. Go to the editor
Click me to see the solution

41. Write a PHP program to print out the multiplication table upto 6*6. Go to the editor
Output:

 1   2   3   4   5   6                                                                    
 2   4   6   8  10  12                                                                        
 3   6   9  12  15  18                                                                      
 4   8  12  16  20  24                                                                             
 5  10  15  20  25  30                                                                     
 6  12  18  24  30  36 
Click me to see the solution

42. Write a PHP program to find the first non-repeated character in a given string. Go to the editor
Input: Green
Output: G
Input: abcdea
Output: b
Click me to see the solution

43. Write a PHP program that multiplies corresponding elements of two given lists. Go to the editor
Click me to see the solution

44. Write a PHP program to print out the sum of pairs of numbers of a given sorted array of positive integers which is equal to a given number. Go to the editor
Click me to see the solution

45. Write a PHP program to compute the sum of the digits of a number. Go to the editor
Click me to see the solution

46. Write a PHP program to find heights of the top three building in descending order from eight given buildings. Go to the editor
Input:
0 = height of building (integer) = 10,000
Output:

Heights of the top three buildings:
45
25
24
Click me to see the solution

47. Write a PHP program to compute the digit number of sum of two given integers. Go to the editor
Input:
Each test case consists of two non-negative integers x and y which are separated by a space in a line.
0 = x, y = 1,000,000
Output:

Digit number of sum of two given integers: 2
Click me to see the solution

48. Write a PHP program to check whether three given lengths (integers) of three sides form a right triangle. Print "Yes" if the given sides form a right triangle otherwise print "No". Go to the editor
Input:
Integers separated by a single space.
1 = length of the side = 1,000
Output:

YES
Click me to see the solution

49. Write a PHP program which solve the equation: Go to the editor
ax+by=c
dx+ey=f
Print the values of x, y where a, b, c, d, e and f are given.
Input:
a,b,c,d,e,f separated by a single space.
(-1,000 = a,b,c,d,e,f = 1,000)
Output:

Values of x and y:
-1.684 2.737
Click me to see the solution

50. Write a PHP program to compute the amount of the debt in n months. The borrowing amount is $100,000 and the loan adds 5% interest of the debt and rounds it to the nearest 1,000 above month by month. Go to the editor
Input:
An integer n (0 = n = 100) .
Output:

Amount of debt: 137000
Click me to see the solution

51. Write a PHP program which reads an integer n and find the number of combinations. of a,b,c and d (0 ≤ a,b,c,d ≤ 9) where (a + b + c + d) will be equal to n. Go to the editor
Input:
n (1 = n = 50)
Output:

Number of combinations of a,b,c and d: 56
Click me to see the solution

52. Write a PHP program to print the number of prime numbers which are less than or equal to a given integer. Go to the editor
Input:
n (1 = n = 999,999)
Output:

Number of prime numbers which are less than or equal to n: 618
Click me to see the solution

53. Write a PHP program to compute the radius and the central coordinate (x, y) of a circle which is constructed by three given points on the plane surface. Go to the editor
Input:
x1, y1, x2, y2, x3, y3 separated by a single space.
Output:

Central coordinate(x,y) and radius of the circle:
(1.000 1.000) 1.414
Click me to see the solution

54. Write a PHP program to compute and print sum of two given integers (more than or equal to zero). If given integers or the sum have more than 80 digits, print "overflow". Go to the editor
Output:

46
overflow
overflow
...
overflow
overflow
Click me to see the solution

55. Write a PHP program that accepts six numbers as input and sorts them in descending order. Go to the editor
Input: Input consists of six numbers n1, n2, n3, n4, n5, n6 (-100000 = n1, n2, n3, n4, n5, n6 = 100000). The six numbers are separated by a space.
Output:

After sorting the said integers:
9 8 7 6 4 2
Click me to see the solution

56. Write a PHP program to test whether two lines PQ and RS are parallel. Go to the editor
The four points are P(x1, y1), Q(x2, y2), R(x3, y3), S(x4, y4).
Input:
2
1.0 0.0 3.0 2.0 2.0 2.0 0.0 0.0
4.0 3.0 10.0 7.0 14.0 5.0 8.0 10.0
Output:

PQ and RS are parallel.
PQ and RS are not parallel.
Click me to see the solution

57. Write a PHP program to find the maximum sum of a contiguous subsequence from a given sequence of numbers a1, a2, a3, ... an. A subsequence of one element is also a continuous subsequence. Go to the editor
You can assume that 1 = n = 5000 and -100000 = ai = 100000.
Input numbers are separated by a space.
Input 0 to exit.
Sample Input:6
-4
-2
5
3
8
Sample Output:

16
Click me to see the solution

58. There are two circles C1 with radius r1, central coordinate (x1, y1) and C2 with radius r2 and central coordinate (x2, y2) Go to the editor
Input numbers (real numbers) are separated by a space.
Write a PHP program to test the followings -
"C2 is in C1" if C2 is in C1
"C1 is in C2" if C1 is in C2
"Circumference of C1 and C2 intersect" if circumference of C1 and C2 intersect, and
"C1 and C2 do not overlap" if C1 and C2 do not overlap.
Sample Input:
2
0.0 0.0 6.0 0.0 0.0 5.0
0.0 0.0 3.0 5.1 0.0 3.0
Sample Output:

C2  is in C1.
Circumference of C1  and C2  intersect.
Click me to see the solution

59. Write a PHP program to that reads a date (from 2016/1/1 to 2016/12/31) and prints the day of the date. Jan. 1, 2016, is Friday. Note that 2016 is a leap year. Go to the editor
Two integers m and d separated by a single space in a line, m ,d represent the month and the day.
Sample Input:
9 15
Sample Output:

The day is: Thursday
Click me to see the solution

60. Write a PHP program to print mode values from a given a sequence of integers. The mode value is the element which occurs most frequently. If there are several mode values, print them in ascending order. Go to the editor
A sequence of integer's ai (1 = ai = 100). The number of integers is less than or equals to 100.

Sample Input:
6
7
4
6
9
8
6
4
10
8
4
5
Sample Output:

Mode values (in ascending order):
4
6
Click me to see the solution

61. Write a PHP program which reads a text (only alphabetical characters and spaces.) and prints two words. The first one is the word which is arise most frequently in the text. The second one is the word which has the maximum number of letters. Go to the editor
Note: A word is a sequence of letters which is separated by the spaces.
Input:
A text is given in a line with following condition:
a. The number of letters in the text is less than or equal to 1000.
b. The number of letters in a word is less than or equal to 32.
c. There is only one word which is arise most frequently in given text.
d. There is only one word which has the maximum number of letters in given text. Sample Input:Thank you for your comment and your participation.
Sample Output:

your participation.
Click me to see the solution

62. Write a PHP program which reads the two adjoined sides and the diagonal of a parallelogram and check whether the parallelogram is a rectangle or a rhombus. Go to the editor
According to Wikipedia-
parallelograms: In Euclidean geometry, a parallelogram is a simple (non-self-intersecting) quadrilateral with two pairs of parallel sides. The opposite or facing sides of a parallelogram are of equal length and the opposite angles of a parallelogram are of equal measure.
rectangles: In Euclidean plane geometry, a rectangle is a quadrilateral with four right angles. It can also be defined as an equiangular quadrilateral, since equiangular means that all of its angles are equal (360o/4 = 90o). It can also be defined as a parallelogram containing a right angle.
rhombus: In plane Euclidean geometry, a rhombus (plural rhombi or rhombuses) is a simple (non-self-intersecting) quadrilateral whose four sides all have the same length. Another name is equilateral quadrilateral, since equilateral means that all of its sides are equal in length. The rhombus is often called a diamond, after the diamonds suit in playing cards which resembles the projection of an octahedral diamond, or a lozenge, though the former sometimes refers specifically to a rhombus with a 60o angle (see Polyiamond), and the latter sometimes refers specifically to a rhombus with a 45o angle.
Input:Two adjoined sides and the diagonal.
1 = ai, bi, ci = 1000, ai + bi > ci
Sample Input:
4,5,6
6,6,9
Sample Output:

This is rectangles.
Click me to see the solution

63. Write a PHP program to replace a string "Python" with "PHP" and "Python" with "PHP" in a given string. Go to the editor
Input:English letters (including single byte alphanumeric characters, blanks, symbols) are given on one line. The length of the input character string is 1000 or less.
Sample Input:
PHP is popular than Python
Sample Output:

Python is popular than PHP.
Click me to see the solution

64. Write a PHP program to find the difference between the largest integer and the smallest integer which are created by 8 numbers from 0 to 9. The number that can be rearranged shall start with 0 as in 00135668. Go to the editor
Input: The difference between the largest integer and the smallest integer.
Sample Input:
1
34567829
Sample Output:

Difference between the largest integer and the smallest integer:
75308643
Click me to see the solution

65. Write a PHP program to compute the sum of first n given prime numbers. Go to the editor
Input: n ( n = 10000). Input 0 to exit the program.
Sample Input:
25
0
Sample Output:

Sum of first 25 prime numbers:1060
Click me to see the solution

66. Write a PHP program that accept a even number (n should be greater than or equal to 4 and less than or equal to 50000, Goldbach number) from the user and create a combinations that express the given number as a sum of two prime numbers. Print the number of combinations. Go to the editor
Goldbach number: A Goldbach number is a positive even integer that can be expressed as the sum of two odd primes.[4] Since four is the only even number greater than two that requires the even prime 2 in order to be written as the sum of two primes, another form of the statement of Goldbach's conjecture is that all even integers greater than 4 are Goldbach numbers.
The expression of a given even number as a sum of two primes is called a Goldbach partition of that number. The following are examples of Goldbach partitions for some even numbers:
6 = 3 + 3
8 = 3 + 5
10 = 3 + 7 = 5 + 5
12 = 7 + 5
...
100 = 3 + 97 = 11 + 89 = 17 + 83 = 29 + 71 = 41 + 59 = 47 + 53
Sample Input:
100
0
Sample Output:

Number of combinations: 6
Click me to see the solution

67. if you draw a straight line on a plane, the plane is divided into two regions. For example, if you pull two straight lines in parallel, you get three areas, and if you draw vertically one to the other you get 4 areas. Write a PHP program to create maximum number of regions obtained by drawing n given straight lines. Go to the editor
Input: (1 = n = 10,000)
Sample Input:
5
Sample Output:

Maximum number of regions: 16
Click me to see the solution

68. There are four different points on a plane, P(xp,yp), Q(xq, yq), R(xr, yr) and S(xs, ys). Write a PHP program to test whether AB and CD are orthogonal or not. Go to the editor
xp,yp, xq, yq, xr, yr, xs and ys are -100 to 100 respectively and each value can be up to 5 digits after the decimal point It is given as a real number including the number of.
Sample Input:
5
Sample Output:

Maximum number of regions: 16
Click me to see the solution

69. Write a PHP program to sum of all numerical values (positive integers) embedded in a sentence. Go to the editor
Sentences with positive integers are given over multiple lines. Each line is a character string containing one-byte alphanumeric characters, symbols, spaces, or an empty line. However the input is 80 characters or less per line and the sum is 10,000 or less.
Sample Input:
5 apple and 10 orange are rotten in the basket
Sample Output:

Maximum number of regions: 16
Click me to see the solution

70. Record section of ABC company records the customer number and the trading date for each month. Write a PHP program to find the customer number that has traded for the second consecutive for the second consecutive month from last month and the number of transactions. Go to the editor
The data of this month and the data of last month are separated by a blank line of one line and given.
Transaction format:
c1 , d1
c2 , d2
...
...
ci (1 = ci = 1,000) is an integer represents the customer number, di (1 = di = 31) is an integer represents the trading date.
Sample Input:
125,10
55,12
34,14

125,3
55,4
125,5
Sample Output:

Sum of the numeric values: 15
Click me to see the solution

71. There are 10 vertical and horizontal squares on a plane. Each square is painted blue and green. Blue represents the sea, and green represents the land. When two green squares are in contact with the top and bottom, or right and left, they are said to be ground. The area created by only one green square is called "island". For example, there are five islands in the figure below.
Write a PHP program to read the mass data and find the number of islands. Go to the editor
A single data set is represented by 10 rows of 10 numbers representing green squares as 1 and blue squares as zeros.
Sample Input:
1100000111
1000000111
0000000111
0010001000
0000011100
0000111110
0001111111
1000111110
1100011100
1110001000
Sample Output:

Number of islands: 
5
Click me to see the solution

72. When character are consecutive in a string , it is possible to shorten the character string by replacing the character with a certain rule. For example, in the case of the character string YYYYY, if it is expressed as # 5 Y, it is compressed by one character.
Write a PHP program to restore the original string by entering the compressed string with this rule. However, the # character does not appear in the restored character string. Go to the editor
Note: The original sentences are uppercase letters, lowercase letters, numbers, symbols, less than 100 letters, and consecutive letters are not more than 9 letters.
Input:
Multiple character strings are given. One string is given per line
Sample Output:

88888888 + 1 = 100000000
Click me to see the solution

73. A convex polygon is a simple polygon in which no line segment between two points on the boundary ever goes outside the polygon. Equivalently, it is a simple polygon whose interior is a convex set. In a convex polygon, all interior angles are less than or equal to 180 degrees, while in a strictly convex polygon all interior angles are strictly less than 180 degrees.
Write a PHP program that compute the area of the polygon . The vertices have the names vertex 1, vertex 2, vertex 3, ... vertex n according to the order of edge connections. However, n is 3 or more and 20 or less. You can also use the following formula to calculate the area S from the lengths a, b, and c of the triangle's three sides.
Go to the editor
Input:
1.0, 0.0
0.0, 0.0
1.0, 1.0
2.0, 0.0
-1.0, 1.0
Sample Output:

Area of the polygon:
1.5
Click me to see the solution

74. Internet search engine giant, such as Google accepts web pages around the world and classify them, creating a huge database. The search engines also analyze the search keywords entered by the user and create inquiries for database search. In both cases, complicated processing is carried out in order to realize efficient retrieval, but basics are all cutting out words from sentences.
Write a PHP program to cut out words of 3 to 6 characters length from a given sentence not more than 1024 characters. Go to the editor
Input:
English sentences consisting of delimiters and alphanumeric characters are given on one line.
Sample Input:
The quick brown fox
Sample Output:

Original string: The quick brown fox
Words  of 3 to 6 characters length: The quick brown fox
Click me to see the solution

75. Arrange integers (0 to 99) as narrow hilltop, as illustrated in Figure 1. Reading such data representing huge, when starting from the top and proceeding according to the next rule to the bottom. Write a PHP program that compute the maximum value of the sum of the passing integers. Go to the editor
Input:
A series of integers separated by commas are given in diamonds. No spaces are included in each line. The input example corresponds to Figure 1. The number of lines of data is less than 100 lines.
Sample Input:
8
4, 9
9, 2, 1
3, 8, 5, 5
5, 6, 3, 7, 6
3, 8, 5, 5
9, 2, 1
4, 9
8
Sample Output:

64
Click me to see the solution

76. Your task is to develop a small part of spreadsheet software.
Write a PHP program which adds up columns and rows of given table as shown in the following figure: Go to the editor
Input:
n (the size of row and column of the given table)
1st row of the table
2nd row of the table
:
:
n th row of the table
The input ends with a line consisting of a single 0.
Sample Input:
4
25 69 51 26
68 35 29 54
54 57 45 63
61 68 47 59
0
Sample Output:

The table with sum of rows and columns:
   25   69   51   26  171
   68   35   29   54  186
   54   57   45   63  219
   61   68   47   59  235
  208  229  172  202  811
Click me to see the solution

77. Write a PHP program which reads a list of pairs of a word and a page number, and prints the word and a list of the corresponding page numbers. Go to the editor
The number of pairs of a word and a page number is less than or equal to 1000. A word never appear in a page more than once. The words should be printed in alphabetical order and the page numbers should be printed in ascending order.
Sample Input:
apple 5
banana 6
Sample Output:

The word and a list of the corresponding page numbers:
apple
5
banana
6
Click me to see the solution

78. Write a PHP program to create a function that returns true for all elements of an array, false otherwise. Go to the editor
Sample Output:

1
0
0
Click me to see the solution

79. Write a PHP program to deep flatten an given array. Go to the editor
Sample Output:

(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
)
Click me to see the solution

80.Write a PHP program to create  a new array with n elements removed from the left. Go to the editor

Array
(
    [0] => 2
    [1] => 3
)
Array
(
    [0] => 3
    [1] => 4
)
Click me to see the solution

81. Write a PHP program to get the last element for which the given function returns a truth value. Go to the editor
Sample Output:

3
4
Click me to see the solution

82. Write a PHP program to get the index of the last element for which the given function returns a truth value. Go to the editor
Sample Output:

2
3
Click me to see the solution

83. Write a PHP program to group the elements of an array based on the given function. Go to the editor
Sample Output:

Array
(
    [3] => Array
        (
            [0] => one
            [1] => two
        )

    [5] => Array
        (
            [0] => three
        )

    [4] => Array
        (
            [0] => four
        )

)
Click me to see the solution

84. Write a PHP program to check a flat list for duplicate values. Returns true if duplicate values exists and false if values are all unique. Go to the editor
Sample Output:

1
0
Click me to see the solution

85. Write a PHP program to get the head of a given list. Go to the editor
Sample Output:

1
2
Click me to see the solution

86. Write a PHP program to get the last element of a given list. Go to the editor
Sample Output:

3
2
Click me to see the solution

87. Write a PHP program to retrieve all of the values for a given key. Go to the editor
Sample Output:

Array
(
    [0] => Computer
    [1] => Laptop
)
Click me to see the solution

88. Write a PHP program to mutate the original array to filter out the values specified. Go to the editor
Sample Output:

Array
(
    [0] => b
    [1] => b
)
Click me to see the solution

89. Write a PHP program to filter the collection using the given callback. Go to the editor
Sample Output:

Array
(
    [0] => Red
)
Click me to see the solution

90. Write a PHP program to return all elements in an given array except for the first one. Go to the editor
Sample Output:

Array
(
    [0] => 2
    [1] => 3
)
Click me to see the solution

91. Write a PHP program to get an array with n elements removed from the beginning of a given array. Go to the editor
Sample Output:

Array
(
    [0] => 1
)

Array
(
    [0] => 1
    [1] => 2
)
Click me to see the solution

92. Write a PHP program to filter out the elements of an given array, that have one of the specified values. Go to the editor
Sample Output:

Array
(
    [0] => 3
)
Click me to see the solution

93. Write a PHP program to sort a collection of given arrays or objects by key. Go to the editor

Sample Output:

Array
(
    [0] => Array
        (
            [id] => 3
            [name] => Black
        )

    [1] => Array
        (
            [id] => 2
            [name] => Red
        )

    [2] => Array
        (
            [id] => 1
            [name] => Green
        )

)
Click me to see the solution

94. Write a PHP program to check if two numbers are approximately equal to each other. Go to the editor
Note: Use abs() to compare the absolute difference of the two values to epsilon. Omit the third parameter, epsilon, to use a default value of 0.001.

Sample Output:

1
0
Click me to see the solution

95. Write a PHP program to check if a given string starts with a given substring. Go to the editor

Sample Output:

1
Click me to see the solution

96. Write a PHP program to count number of vowels in a given string. Go to the editor
Note: Use a regular expression to count the number of vowels (A, E, I, O, U) in a string.

Sample Output:

4
Click me to see the solution

97. Write a PHP program to decapitalize the first letter of the string and then adds it with rest of the string. Go to the editor

Sample Output:

python
Click me to see the solution

98. Write a PHP program to create a new function that composes multiple functions into a single callable. Go to the editor

Sample Output:

16
20
Click me to see the solution

99. Write a PHP program to memoize a given function results in memory. Go to the editor
Note: In computing, memoization or memoisation is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again.

Sample Output:

array(2) {
  ["result"]=>
  int(15)
  ["cached"]=>
  bool(false)
}
array(2) {
  ["result"]=>
  int(16)
  ["cached"]=>
  bool(false)
}
array(2) {
  ["result"]=>
  int(15)
  ["cached"]=>
  bool(true)
}
Click me to see the solution

100. Write a PHP program to curry a function to take arguments in multiple calls. Go to the editor

Sample Output:

int(25)
Click me to see the solution

101. Write a PHP program to call a given function only once. Go to the editor

Sample Output:

int(15)
NULL
Click me to see the solution

102. Write a PHP program to capture a variable number of arguments to a given function. Go to the editor

Sample Output:

int(3)
int(10)
Click me to see the solution

PHP Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.