
Best 150 PHP Basic Programming Questions | Practice Now
Q1. Write a PHP program to display "Hello, World!".
Input: No input is needed.
Expected Output:
Hello, World!
Q2. Write a PHP program to add two numbers and display the result.
Input:
$a = 5;
$b = 10;
Expected Output:
Sum: 15
Q3. Write a PHP program to check if a number is even or odd.
Input:
$n = 7;
Expected Output:
7 is Odd
Q4. Write a PHP program to print numbers from 1 to 10 using a loop.
Input: No input is needed.
Expected Output:
1 2 3 4 5 6 7 8 9 10
Q5. Write a PHP program to perform addition, subtraction, multiplication, and division based on user input.
Input:
$a = 20;
$b = 4;
$operation = "multiply";
Expected Output:
Result: 80
Q6. Write a PHP program to reverse a string.
Input:
$string = "PHP";
Expected Output:
Reversed String: PHP
Q7. Write a PHP program to check if a year is a leap year or not.
Input:
$year = 2024;
Expected Output:
2024 is a Leap Year
Q8. Write a PHP program to find the factorial of a number.
Input:
$n = 5;
Expected Output:
Factorial of 5 is 120
Q9. Write a PHP program to count the number of characters in a string.
Input:
$string = "OpenAI";
Expected Output:
Number of characters: 6
Q10. Write a PHP program to find the largest number among three numbers.
Input:
$a = 12;
$b = 25;
$c = 9;
Expected Output:
Largest Number: 25
Q11. Write a PHP program to calculate the sum of all elements in an array.
Input:
$array = [1, 2, 3, 4, 5];
Expected Output:
Sum of elements: 15
Q12. Write a PHP program to check if a number is a prime number.
Input:
$n = 13;
Expected Output:
13 is a Prime Number
Q13. Write a PHP program to convert a string to uppercase.
Input:
$string = "php programming";
Expected Output:
Uppercase String: PHP PROGRAMMING
Q14. Write a PHP program to generate the first 10 numbers in a Fibonacci series.
Input: No input is needed.
Expected Output:
0 1 1 2 3 5 8 13 21 34
Q15. Write a PHP program to find the length of an array.
Input:
$array = [10, 20, 30, 40];
Expected Output:
Length of the array: 4
Q16. Write a PHP program to swap two numbers without using a third variable.
Input:
$a = 5;
$b = 10;
Expected Output:
After swapping: a = 10, b = 5
Q17. Write a PHP program to count the number of vowels in a string.
Input:
$string = "education";
Expected Output:
Number of vowels: 5
Q18. Write a PHP program to reverse an array.
Input:
$array = [1, 2, 3, 4, 5];
Expected Output:
Reversed Array: [5, 4, 3, 2, 1]
Q19. Write a PHP program to check if a string is a palindrome.
Input:
$string = "madam";
Expected Output:
madam is a Palindrome
Q20. Write a PHP program to check if a number is divisible by both 3 and 5.
Input:
$n = 15;
Expected Output:
15 is divisible by both 3 and 5
Q21. Write a PHP program to convert a temperature from Celsius to Fahrenheit.
Input:
$celsius = 25;
Expected Output:
Temperature in Fahrenheit: 77
Q22. Write a PHP program to find the smallest number among three numbers.
Input:
$a = 15;
$b = 8;
$c = 20;
Expected Output:
Smallest Number: 8
Q23. Write a PHP program to calculate the sum of the digits of a number.
Input:
$number = 1234;
Expected Output:
Sum of digits: 10
Q24. Write a PHP program to sort an array in ascending order.
Input:
$array = [4, 2, 8, 5, 1];
Expected Output:
Sorted Array: [1, 2, 4, 5, 8]
Q25. Write a PHP program to find the maximum value in an array.
Input:
$array = [10, 20, 5, 8, 50];
Expected Output:
Maximum Value: 50
Q26. Write a PHP program to print the multiplication table of a given number.
Input:
$n = 5;
Expected Output:
5 x 1 = 5
5 x 2 = 10
...
5 x 10 = 50
Q27. Write a PHP program to calculate the average of elements in an array.
Input:
$array = [10, 20, 30, 40];
Expected Output:
Average: 25
Q28. Write a PHP program to find the GCD (Greatest Common Divisor) of two numbers.
Input:
$a = 12;
$b = 15;
Expected Output:
GCD: 3
Q29. Write a PHP program to check if a number is an Armstrong number.
Input:
$number = 153;
Expected Output:
153 is an Armstrong Number
Q30. Write a PHP program to find all odd numbers in an array.
Input:
$array = [1, 2, 3, 4, 5, 6];
Expected Output:
Odd Numbers: [1, 3, 5]
Q31. Write a PHP program to find all even numbers in an array.
Input:
$array = [1, 2, 3, 4, 5, 6];
Expected Output:
Even Numbers: [2, 4, 6]
Q32. Write a PHP program to convert a string to lowercase.
Input:
$string = "WELCOME TO PHP";
Expected Output:
Lowercase String: welcome to php
Q33. Write a PHP program to remove all whitespaces from a string.
Input:
$string = " Hello World ";
Expected Output:
String without Whitespaces: HelloWorld
Q34. Write a PHP program to calculate the sum of the squares of array elements.
Input:
$array = [1, 2, 3];
Expected Output:
Sum of Squares: 14
Q35. Write a PHP program to check if a number is a palindrome.
Input:
$number = 121;
Expected Output:
121 is a Palindrome Number
Q36. Write a PHP program to count the number of words in a string.
Input:
$string = "PHP is amazing";
Expected Output:
Number of Words: 3
Q37. Write a PHP program to check if a string contains a given substring.
Input:
$string = "PHP programming";
$substring = "PHP";
Expected Output:
Contains Substring: Yes
Q38. Write a PHP program to calculate the power of a number using the `pow()` function.
Input:
$base = 2;
$exponent = 3;
Expected Output:
Result: 8
Q39. Write a PHP program to replace a substring in a string.
Input:
$string = "I love Java";
$search = "Java";
$replace = "PHP";
Expected Output:
Modified String: I love PHP
Q40. Write a PHP program to find the second largest number in an array.
Input:
$array = [10, 20, 5, 8, 50];
Expected Output:
Second Largest Number: 20
Q41. Write a PHP program to find the sum of the first N natural numbers.
Input:
php
$n = 10;
Expected Output:
Sum: 55
Q42. Write a PHP program to remove duplicate values from an array.
Input:
$array = [1, 2, 2, 3, 4, 4, 5];
Expected Output:
Array without Duplicates: [1, 2, 3, 4, 5]
Q43. Write a PHP program to check if a number is positive, negative, or zero.
Input:
$n = -5;
Expected Output:
-5 is Negative
Q44. Write a PHP program to reverse the digits of a number.
Input:
$number = 12345;
Expected Output:
Reversed Number: 54321
Q45. Write a PHP program to concatenate two strings.
Input:
$string1 = "Hello";
$string2 = "World";
Expected Output:
Concatenated String: HelloWorld
Q46. Write a PHP program to find the average of two numbers.
Input:
$a = 15;
$b = 25;
Expected Output:
Average: 20
Q47. Write a PHP program to check if an array is empty.
Input:
$array = [];
Expected Output:
Array is Empty
Q48. Write a PHP program to convert a binary number to a decimal number.
Input:
$binary = "1010";
Expected Output:
Decimal: 10
Q49. Write a PHP program to convert a decimal number to binary.
Input:
$decimal = 10;
Expected Output:
Binary: 1010
Q50. Write a PHP program to find elements in an array greater than a given number.
Input:
$array = [10, 20, 30, 40, 50];
$threshold = 25;
Expected Output:
Elements Greater than 25: [30, 40, 50]
Q51. Write a PHP program to find the factorial of a number using recursion.
Input:
$n = 5;
Expected Output:
Factorial of 5 is 120
Q52. Write a PHP program to calculate the sum of the first N odd numbers.
Input:
$n = 5;
Expected Output:
Sum of first 5 odd numbers: 25
Q53. Write a PHP program to print a triangle pattern using stars (`*`).
Input:
$n = 5;
Expected Output:
*
**
***
****
*****
Q54. Write a PHP program to convert a string into an array of characters.
Input:
$string = "PHP";
Expected Output:
Array: ['P', 'H', 'P']
Q55. Write a PHP program to find the length of a string without using built-in functions.
Input:
$string = "Hello";
Expected Output:
Length of the string: 5
Q56. Write a PHP program to create an associative array with keys and values.
Input:
$array = ['name' => 'John', 'age' => 25];
Expected Output:
Array: [name => John, age => 25]
Q57. Write a PHP program to count the number of even and odd numbers in an array.
Input:
$array = [1, 2, 3, 4, 5, 6];
Expected Output:
Even Numbers: 3
Odd Numbers: 3
Q58. Write a PHP program to find the difference between two dates.
Input:
$date1 = "2024-01-01";
$date2 = "2024-12-31";
Expected Output:
Difference: 365 days
Q59. Write a PHP program to convert a string into an integer.
Input:
$string = "1234";
Expected Output:
Integer: 1234
Q60. Write a PHP program to check if a number is divisible by 7.
Input:
$n = 35;
Expected Output:
35 is divisible by 7
Q61. Write a PHP program to reverse the words in a string.
Input:
$string = "PHP is fun";
Expected Output:
Reversed Words: fun is PHP
Q62. Write a PHP program to check if a given year is a leap year.
Input:
$year = 2024;
Expected Output:
2024 is a Leap Year
Q63. Write a PHP program to remove a specified element from an array.
Input:
$array = [1, 2, 3, 4, 5];
$element = 3;
Expected Output:
Array after removal: [1, 2, 4, 5]
Q64. Write a PHP program to split a string into an array using the `explode()` function.
Input:
$string = "apple,banana,orange";
Expected Output:
Array: ['apple', 'banana', 'orange']
Q65. Write a PHP program to generate a random number between two values.
Input:
$min = 10;
$max = 50;
Expected Output:
Random Number: (a random number between 10 and 50)
Q66. Write a PHP program to merge two arrays.
Input:
$array1 = [1, 2, 3];
$array2 = [4, 5, 6];
Expected Output:
Merged Array: [1, 2, 3, 4, 5, 6]
Q67. Write a PHP program to find the sum of the first N even numbers.
Input:
$n = 5;
Expected Output:
Sum of first 5 even numbers: 30
Q68. Write a PHP program to count how many times a specific value appears in an array.
Input:
$array = [1, 2, 2, 3, 4, 2];
$value = 2;
Expected Output:
Occurrence of 2: 3
Q69. Write a PHP program to create a simple calculator that performs addition, subtraction, multiplication, and division.
Input:
$a = 10;
$b = 5;
$operation = 'addition';
Expected Output:
Result: 15
Q70. Write a PHP program to find the product of all elements in an array.
Input:
$array = [2, 3, 4];
Expected Output:
Product of elements: 24
Q71. Write a PHP program to check if a string starts with a specific substring.
Input:
$string = "Hello, World!";
$substring = "Hello";
Expected Output:
String starts with "Hello": Yes
Q72. Write a PHP program to check if an array contains a specific value.
Input:
$array = [10, 20, 30, 40];
$value = 30;
Expected Output:
Array contains 30: Yes
Q73. Write a PHP program to sort an array in ascending order.
Input:
$array = [5, 2, 8, 1, 3];
Expected Output:
Sorted Array: [1, 2, 3, 5, 8]
Q74. Write a PHP program to generate the first N numbers of the Fibonacci sequence.
Input:
$n = 7;
Expected Output:
Fibonacci Sequence: 0, 1, 1, 2, 3, 5, 8
Q75. Write a PHP program to swap two variables without using a third variable.
Input:
$a = 10;
$b = 20;
Expected Output:
Swapped Values: a = 20, b = 10
Q76. Write a PHP program to remove all numeric characters from a string.
Input:
$string = "Hello123";
Expected Output:
String without numbers: Hello
Q77. Write a PHP program to find the maximum value in an array.
Input:
$array = [10, 25, 40, 5];
Expected Output:
Maximum Value: 40
Q78. Write a PHP program to calculate simple interest.
Input:
$principal = 1000;
$rate = 5;
$time = 2;
Expected Output:
Simple Interest: 100
Q79. Write a PHP program to convert temperature from Celsius to Fahrenheit.
Input:
$celsius = 25;
Expected Output:
Fahrenheit: 77
Q80. Write a PHP program to find the minimum value in an array.
Input:
$array = [10, 25, 40, 5];
Expected Output:
Minimum Value: 5
Q81. Write a PHP program to check if a string is a palindrome (reads the same forward and backward).
Input:
$string = "madam";
Expected Output:
madam is a Palindrome
Q82. Write a PHP program to convert a string to uppercase.
Input:
$string = "hello";
Expected Output:
HELLO
Q83. Write a PHP program to create an array of the first N even numbers.
Input:
$n = 5;
Expected Output:
Even Numbers: [2, 4, 6, 8, 10]
Q84. Write a PHP program to find the count of words in a string.
Input:
$string = "Hello, how are you?";
Expected Output:
Word Count: 4
Q85. Write a PHP program to print numbers from 1 to N.
Input:
$n = 5;
Expected Output:
1
2
3
4
5
Q86. Write a PHP program to find the length of an array.
Input:
$array = [1, 2, 3, 4, 5];
Expected Output:
Array Length: 5
Q87. Write a PHP program to convert hours into minutes.
Input:
$hours = 3;
Expected Output:
Minutes: 180
Q88. Write a PHP program to find common elements in two arrays.
Input:
$array1 = [1, 2, 3, 4];
$array2 = [3, 4, 5, 6];
Expected Output:
Common Elements: [3, 4]
Q89. Write a PHP program to find the sum of even numbers in an array.
Input:
$array = [1, 2, 3, 4, 5];
Expected Output:
Sum of Even Numbers: 6
Q90. Write a PHP program to swap two variables using a third variable.
Input:
$a = 10;
$b = 20;
Expected Output:
Swapped Values: a = 20, b = 10
Q91. Write a PHP program to check if a number is prime.
Input:
$n = 7;
Expected Output:
7 is a Prime Number
Q92. Write a PHP program to convert a string to lowercase.
Input:
$string = "HELLO";
Expected Output:
hello
Q93. Write a PHP program to find the greatest common divisor (GCD) of two numbers.
Input:
$a = 36;
$b = 60;
Expected Output:
GCD of 36 and 60: 12
Q94. Write a PHP program to reverse an array.
Input:
$array = [1, 2, 3, 4, 5];
Expected Output:
Reversed Array: [5, 4, 3, 2, 1]
Q95. Write a PHP program to check if a number is an Armstrong number (the sum of its digits raised to the power of the number of digits is equal to the number).
Input:
$n = 153;
Expected Output:
153 is an Armstrong Number
Q96. Write a PHP program to count the number of vowels in a string.
Input:
$string = "hello";
Expected Output:
Number of Vowels: 2
Q97. Write a PHP program to remove all white spaces from a string.
Input:
$string = "Hello World";
Expected Output:
String without spaces: HelloWorld
Q98. Write a PHP program to find the sum of digits of a number.
Input:
$n = 123;
Expected Output:
Sum of Digits: 6
Q99. Write a PHP program to check if a string contains a specific word.
Input:
$string = "Hello, welcome to PHP!";
$word = "PHP";
Expected Output:
The string contains "PHP": Yes
Q100. Write a PHP program to calculate the area of a circle given its radius.
Input:
$radius = 7;
Expected Output:
Area of Circle: 153.94
Q101. Write a PHP program to merge two associative arrays.
Input:
$array1 = ["a" => 1, "b" => 2];
$array2 = ["c" => 3, "d" => 4];
Expected Output:
Merged Array: ["a" => 1, "b" => 2, "c" => 3, "d" => 4]
Q102. Write a PHP program to check if a number is even or odd.
Input:
$n = 6;
Expected Output:
6 is an Even Number
Q103. Write a PHP program to find the largest number in an array.
Input:
$array = [5, 10, 20, 15];
Expected Output:
Largest Number: 20
Q104. Write a PHP program to get the day of the week for a given date.
Input:
$date = "2024-12-28";
Expected Output:
Day of the Week: Saturday
Q105. Write a PHP program to check if a string contains only digits.
Input:
$string = "123456";
Expected Output:
The string contains only digits: Yes
Q106. Write a PHP program to add leading zeros to a number.
Input:
$number = 42;
$length = 5;
Expected Output:
042
Q107. Write a PHP program to create a simple timer that counts down from a given number of seconds.
Input:
$seconds = 5;
Expected Output:
Timer Countdown: 5 4 3 2 1 0
Q108. Write a PHP program to find the factorial of a number.
Input:
$n = 5;
Expected Output:
Factorial: 120
Q109. Write a PHP program to check if a string is numeric.
Input:
$string = "12345";
Expected Output:
The string is numeric: Yes
Q110. Write a PHP program to remove duplicate values from an array.
Input:
$array = [1, 2, 2, 3, 4, 4];
Expected Output:
Array without duplicates: [1, 2, 3, 4]
Q111. Write a PHP program to find the square of a number.
Input:
$n = 5;
Expected Output:
Square of 5: 25
Q112. Write a PHP program to reverse a string.
Input:
$string = "hello";
Expected Output:
Reversed String: olleh
Q113. Write a PHP program to calculate the area of a rectangle.
Input:
$length = 10;
$width = 5;
Expected Output:
Area of Rectangle: 50
Q114. Write a PHP program to print the multiplication table of a given number.
Input:
$n = 7;
Expected Output:
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
Q115. Write a PHP program to find the length of a string.
Input:
$string = "hello";
Expected Output:
Length of String: 5
Q116. Write a PHP program to remove a specific element from an array.
Input:
$array = [1, 2, 3, 4, 5];
$element = 3;
Expected Output:
Array after removing 3: [1, 2, 4, 5]
Q117. Write a PHP program to generate a random number between 1 and 100.
Expected Output:
Random Number: 47 (This will change on each run)
Q118. Write a PHP program to check if a given year is a leap year.
Input:
$year = 2024;
Expected Output:
2024 is a Leap Year
Q119. Write a PHP program to get the last character of a string.
Input:
$string = "hello";
Expected Output:
Last Character: o
Q120. Write a PHP program to check if an array is empty.
Input:
$array = [1, 2, 3];
Expected Output:
Array is empty: No
Q121. Write a PHP program to create a simple calculator that performs addition, subtraction, multiplication, and division.
Input:
$a = 10;
$b = 5;
$operation = "addition";
Expected Output:
Addition: 15
Q122. Write a PHP program to find the smallest number in an array.
Input:
$array = [12, 5, 3, 8, 15];
Expected Output:
Smallest Number: 3
Q123. Write a PHP program to merge two indexed arrays.
Input:
$array1 = [1, 2, 3];
$array2 = [4, 5, 6];
Expected Output:
Merged Array: [1, 2, 3, 4, 5, 6]
Q124. Write a PHP program to find the ASCII value of a given character.
Input:
$char = 'A';
Expected Output:
ASCII Value of 'A': 65
Q125. Write a PHP program to remove all white spaces from a string.
Input:
$string = " Hello World ";
Expected Output:
String without spaces: HelloWorld
Q126. Write a PHP program to convert a decimal number to binary.
Input:
$decimal = 10;
Expected Output:
Binary: 1010
Q127. Write a PHP program to calculate the perimeter of a triangle given the lengths of its sides.
Input:
$a = 5;
$b = 6;
$c = 7;
Expected Output:
Perimeter of Triangle: 18
Q128. Write a PHP program to find the square root of a number.
Input:
$n = 16;
Expected Output:
Square Root of 16: 4
Q129. Write a PHP program to find the cube of a number.
Input:
$n = 3;
Expected Output:
Cube of 3: 27
Q130. Write a PHP program to remove all elements from an array.
Input:
$array = [1, 2, 3, 4, 5];
Expected Output:
Array after removal: []
Q131. Write a PHP program to count the number of elements in an array.
Input:
$array = [1, 2, 3, 4, 5];
Expected Output:
Number of Elements: 5
Q132. Write a PHP program to get the first character of a string.
Input:
$string = "hello";
Expected Output:
First Character: h
Q133. Write a PHP program to check if a number is positive, negative, or zero.
Input:
$n = -5;
Expected Output:
-5 is a Negative Number
Q134. Write a PHP program to convert a string into an array of characters.
Input:
$string = "hello";
Expected Output:
Array: ["h", "e", "l", "l", "o"]
Q135. Write a PHP program to remove a specific character from a string.
Input:
$string = "hello";
$char = "l";
Expected Output:
String after removal: heo
Q136. Write a PHP program to generate a random string of 8 characters.
Expected Output:
Random String: jFgH7pQw (This will change on each run)
Q137. Write a PHP program to calculate the area of a triangle given its base and height.
Input:
$base = 10;
$height = 5;
Expected Output:
Area of Triangle: 25
Q138. Write a PHP program to find the least common multiple (LCM) of two numbers.
Input:
$a = 4;
$b = 6;
Expected Output:
LCM of 4 and 6: 12
Q139. Write a PHP program to check if a string is a palindrome.
Input:
$string = "madam";
Expected Output:
madam is a Palindrome
Q140. Write a PHP program to find the frequency of each character in a string.
Input:
$string = "hello";
Expected Output:
h: 1
e: 1
l: 2
o: 1
Q141. Write a PHP program to convert a string to uppercase.
Input:
$string = "hello";
Expected Output:
HELLO
Q142. Write a PHP program to get the current date and time.
Expected Output:
Current Date and Time: 2024-12-28 12:34:56
Q143. Write a PHP program to check if a string contains a specific substring.
Input:
$string = "hello world";
$substring = "world";
Expected Output:
The string contains the substring: Yes
Q144. Write a PHP program to replace all occurrences of a character in a string.
Input:
$string = "hello world";
$old_char = "o";
$new_char = "a";
Expected Output:
String after replacement: hella warld
Q145. Write a PHP program to check if an array contains a specific value.
Input:
$array = [1, 2, 3, 4, 5];
$value = 3;
Expected Output:
The array contains 3: Yes
Q146. Write a PHP program to find the greatest common divisor (GCD) of two numbers.
Input:
$a = 12;
$b = 18;
Expected Output:
GCD of 12 and 18: 6
Q147. Write a PHP program to check if a string starts with a specific character.
Input:
$string = "hello";
$char = "h";
Expected Output:
The string starts with 'h': Yes
Q148. Write a PHP program to count the number of vowels in a string.
Input:
$string = "hello world";
Expected Output:
Number of vowels: 3
Q149. Write a PHP program to remove all non-alphanumeric characters from a string.
Input:
$string = "hello!@# world123";
Expected Output:
String after removal: helloworld123
Q150. Write a PHP program to split a string into an array of words.
Input:
$string = "hello world";
Expected Output:
Array: ["hello", "world"]