Q1. What are the basic arithmetic operators in PHP?
a) +, -, *, /
b) &, |, ~, ^
c) +=, -=, *=, /=
d) +, -, , /, %
Show Answer
Answer : Option (A)
Q2.What is the order of precedence for arithmetic operators in PHP?
a) +, -, *, /, %
b) *, /, +, -, %
c) +, -, %, *, /
d) , /, %, +, -
Show Answer
Answer : Option (B)
Q3. What is the purpose of the switch statement in PHP?
a) To compare two strings
b) To execute code based on the result of
multiple conditions
c) To create a loop
d) To define a function
Show Answer
Answer : Option (A)
Q4. What is a while loop in PHP?
a) A loop that runs for a specific number of
iterations
b) A loop that runs until a condition is met
c) A loop that runs only once
d) A loop that runs backward
Show Answer
Answer : Option (B)
Q5. What is the purpose of the do-while loop in PHP?
a) To run a loop for a specific number of iterations
b) To run a loop until a condition is met
c) To ensure that a loop runs at least once,
regardless of the condition
d) To create an infinite loop
Show Answer
Answer : Option (C)
Q6. What is the purpose of the foreach loop in PHP?
a) To create a loop that runs for a specific number of iterations
b) To create a loop that runs until a condition is met
c) To iterate over the elements of an array or collection
d) To create a loop that runs only once
Show Answer
Answer : Option (C)
Q7. How do you loop through an array using foreach?
a) foreach ($element as $array)
b) for ($i = 0; $i < count($array); $i++)
c) foreach ($array as $element)
d) for each ($array as $element)
Show Answer
Answer : Option (C)
Q8. How do you exit a loop prematurely in PHP?
a) Using the exit keyword
b) Using the return statement
c) Using the break statement
d) Using the continue statement
Show Answer
Answer : Option (C)
Q9. How do you access elements of an indexed array in PHP?
a) $myArray{1}
b) $myArray(1)
c) $myArray[1]
d) $myArray->1
Show Answer
Answer : Option (C)
Q 10. How do you add an element to the end of an indexed array in PHP?
a) array_add($myArray, 4);
b) array_push($myArray, 4);
c) $myArray.add(4);
d) $myArray[] = 4;
Show Answer
Answer : Option (D)
Explaination: While $myArray[] = 4; is more concise for adding a single element, array_push() becomes more convenient when you need to add multiple elements in one go
Q11. How do you remove the last element from an
indexed array in PHP?
a) array_remove_last($myArray);
b) array_pop($myArray);
c) $myArray.pop();
d) $myArray[-1];
Show Answer
Answer : Option (B)
Q12. How do you find the length of an indexed array
in PHP?
a) count($myArray);
b) sizeof($myArray);
c) length($myArray);
d) $myArray.length();
Show Answer
Answer : Option (A)
Q13. How do you access elements of an associative array in PHP?
a) $myArray[0]
b) $myArray{'one'}
c) $myArray['one']
d) $myArray->one
Show Answer
Answer : Option (C)
Q14. How do you remove a key-value pair from an associative array in PHP?
a) array_remove($myArray, 'four');
b) unset($myArray['four']);
c) $myArray.remove('four');
d) $myArray['four'] = null;
Show Answer
Answer : Option (B)
Q15. What is a multidimensional array in PHP?
a) An array with multiple elements
b) An array with only strings as values
c) An array where each element is another array
d) An array with only integers as keys
Show Answer
Answer : Option (C)
Q16. How do you access elements of a multidimensional array in PHP?
a) $myArray[0, 1]
b) $myArray[0][1]
c) $myArray(0, 1)
d) $myArray->0->1
Show Answer
Answer : Option (B)
Q17. What is a function parameter in PHP?
a) A variable declared inside a function
b) A value passed into a function when it is called
c) A variable declared outside a function
d) A variable used for conditional statements
Show Answer
Answer : Option (B)
Q18. What is the purpose of the return statement in a function?
a) To declare a variable
b) To output text
c) To end the function's execution and optionally return a value to the caller
d) To include an external file
Show Answer
Answer : Option (C)
Q19. How do you use the strlen() function to get the length of a string?
a) strlen('Hello, World!');
b) length('Hello, World!');
c) string_length('Hello, World!');
d) strlen("Hello, World!");
Show Answer
Answer : Option (A)
Q20. How do you use the array_push() function to add an element to an array?
a) array_push($myArray, 42, 'apple');
b) $myArray.push(42, 'apple');
c) push_array($myArray, 42, 'apple');
d) array_push($myArray, 42);
Show Answer
Answer : Option (A)
Q21. How do you use the array_pop() function to remove the last element from an array?
a) array_remove_last($myArray);
b) array_pop($myArray);
c) $myArray.remove_last();
d) $myArray.pop();
Show Answer
Answer : Option (B)
Q22. How do you use the date() function to display the current date and time?
a) date("Y-m-d H:i:s");
b) current_date();
c) time();
d) date("d-m-Y H:i:s");
Show Answer
Answer : Option (A)
Q23.
<?phpfunction increment(){ static $counter = 0;$counter++;echo $counter;}increment();increment();?>(code-box)
Show Answer
Answer : Output: 1, 2
Q24. How do you use the explode() function to split a string into an array?
a) split(' ', "Hello, World!");
b) explode(" ", "Hello, World!");
c) separate(" ", "Hello, World!");
d) divide(" ", "Hello, World!");
Show Answer
Answer : Option (B)
Q25. How do you use the implode() function to join array elements into a string?
a) join($myArray, " ");
b) combine(" ", $myArray);
c) implode(" ", $myArray);
d) merge($myArray, " ");
Show Answer
Answer : Option (C)
Q26.
<?php$text = "Hello, World!";$length = strlen($text);$lowercase = strtolower($text);echo "Length: $length, Lowercase: $lowercase";?>(code-box)
Show Answer
Answer : Output:Length: 13, Lowercase: hello, world!
Q27. What is the difference between single quotes ('') and double quotes ("") for Strings in PHP?
a. Single quotes allow variable interpolation, while double quotes do not.
b. Double quotes allow variable interpolation, while single quotes do not.
c. Single quotes are used for long strings, while double quotes are used for
short strings.
d.There is no difference between them.
Show Answer
Answer : Option (B)
Explaination: In PHP, when you use double quotes ("") for defining strings, variable names within the string are evaluated and replaced with their values. This is known as variable interpolation.
Q28. How do you retrieve the value of a cookie in PHP?
a) $_COOKIE['myCookie'];
b) get_cookie('myCookie');
c) $_COOKIE.get('myCookie');
d) getcookie('myCookie');
Show Answer
Answer : Option (A)
Q29. How do you handle file uploads in PHP?
a) Use the file_upload() function
b) Use the upload_file() function
c) Use the move_uploaded_file() function
d) Use the send_file() function
Show Answer
Answer : Option (C)
Q30. How do you handle errors in PHP?
a) By using the handle_error() function
b) By using the error_reporting() function
c) By using the set_error_handler() function
d) By using the try, catch, and finally blocks
Show Answer
Answer : Option (C)
Q31. What is the purpose of the include_once and require_once statements in PHP?
a) To include external files only once, even if they are included multiple times in the code
b) To include external files and execute them immediately
c) To include external files and check if they exist before execution
d) To include external files conditionally based on user input
Show Answer
Answer : Option (A)
Q32.
<?phpfunction calculateSum(){$args = func_get_args();$sum = 0; foreach ($args as $arg){$sum += $arg;}return $sum;}$total = calculateSum(2, 4, 6, 8);echo "Total: $total";?>(code-box)
Show Answer
Answer : Output:20
Q33.
<?phpfunction applyOperation($num, $operation){return $operation($num);}$double = function($x){ return $x * 2;};echo applyOperation(5, $double);?>(code_box)
Show Answer
Answer : Output: 10
Q34. Which one of the following PHP functions can be used to build a function that accepts any number of arguments?
A func_get_argv()
B func_get_args()
C get_argv()
D get_argc()
Show Answer
Answer : Option (B)
Q35. The filesize() function returns the file size in ___.
A bits
B bytes
C kilobytes
D gigabytes
Show Answer
Answer : Option (B)
Q36. Unbounded loops repeats until some condition becomes
A) TRUE
B) False
C) Zero
D) Both A and B
Show Answer
Answer : Option (D)
Q37. Constants in PHP are not denoted by $ sign but they are differentiated by
A) Lowercase letters
B) Uppercase letters
C) Asterisk sign *
D) Hash sign #
Show Answer
Answer : Option (B)
Q38. Get, Post, Cookies and Sessions all are examples of
A) Arrays
B) Functions
C) Methods to pass information
D) None of them
Show Answer
Answer : Option (C)
Q39. Namespaces are offered in
A) PHP2
B) PHP4
C) PHP5
D) PHP6
Show Answer
Answer : Option (D)
Q40. Serialize ( ) function takes a value of any type and then
A) Decodes the value in integer form
B) Encodes a value into strings
C) Changes the value into characters
D) None of them
Show Answer
Answer : Option (B)
Q41. Global $count; this statement indicates what?
A) A variable is declared with that name of global
B) A constant is defined
C) Count variable declaration
D) Count variable declaration with global scope
Show Answer
Answer : Option (D)
Q42. Which of the following operations are supported by PHP?
A) AND
B) OR, Exclusive OR
C) Not
D) All of them
Show Answer
Answer : Option (D)
Q43. Which one of the following function is capable of reading a specific number of characters form a file?
A fgets()
B fget()
C fileget()
D filegets()
Show Answer
Answer : Option (A)
Q44.
<?php$number = "123456";if (!filter_var($number, FILTER_VALIDATE_INT))echo("Valid");elseecho("Not Valid");?>(code-box)
Show Answer
Answer : not valid
Q45. Which built-in function in PHP add value to the end of the array
This_array()
is_array()
while_array()
are_array()
Show Answer
Answer : none of the option is correct
array_push() is correct.
Q46. If we filter many variables into one then which of the following filter is used?
• Filter_var_array()
• Filter_var()
• Filter_var_input()
• Filter_array_input()
Show Answer
Answer : Option (A)
Q47.
<?phpdefine(“GREETING”, “hello! how are you?”);Echo constant(“GREETING”);?>(code-box)
A.Hello! How are you ?
B.GREETING
C.GREETING, Hello ! How are you?
D.“GREETING”,”Hello! How are you ?
Show Answer
Answer : Option (A)
Q48.
<?phpecho count_chars("AA",1);?>(code-box)
a)2
b) [65]=>2
c) Array
d) None of the above
Show Answer
Answer : Option (C)
Q49.
<?php$x = 1;$fn = fn(&$x) => $x++;$fn($x);var_export($x);?>(code-box)
a)1 b)2
c)0 d) Null
Show Answer
Answer : Option (B)
Q50.
<?php$op2 = "blabla";function foo($op1){echo $op1;echo $op2;}foo("hello");?>(code-box)
a) helloblabla
b) Error
c) hello
d) helloblablablabla
Show Answer
Answer : Option (C)
Q51.
<?php$fruits = array ("apple", "orange", array ("pear","mango"),"banana");echo (count($fruits, 1));?>(code-box)
Show Answer
Answer : Output:6
Q52.
<?php$url = "phpprogramming@coderindeed.in"; echoltrim(strstr($url, "@"),"@");?>(code-box)
Show Answer
Answer : Output: coderindeed.in
Q53.
<?php$x = 5;$y = 10;function fun(){$y = $GLOBALS['x'] + $GLOBALS['y'];}fun();echo $y;?>(code-box)
Show Answer
Answer : 10
Q54.
<?php$a = 100;if ($a > 10)printf("PHP Quiz");else if ($a > 20)printf("PHP MCQ");else if($a > 30)printf("PHP Program");?>(code-box)
Show Answer
Answer : PHP Quiz
Q55.
<?php$php = array("Array", "Function", "Strings","File");echo pos($php);?>(code-box)
Show Answer
Answer : Array
Q56. Which one of the following function operates similarly to fgets(), except that it also strips any HTML and PHP tags form the input?
A. fgetsh()
B. fgetsp()
C. fgetsa()
D. fgetss()
Show Answer
Answer : Option (D)
Explaination: The fgetss() function reads a line from a file pointer (similar to fgets()) and strips HTML and PHP tags from the input. It's useful when you want to read and sanitize input from a file that may contain HTML or PHP code.
Q57. Which one of the following function reads a directory into an Array?
A. scandir()
B. readdir()
C. scandirectory()
D. readdirectory()
Show Answer
Answer : Option (A)
Q58. <?php $str = "Hello World" echo wordwrap($str,5,"\n"); ?>
A. Hello World
B. Hello
World
C. Hello wo
rld
D. World
Show Answer
Answer : Option (B)
Q59. Which function will return true if a variable is an array or false if it is not?
A. this_array()
B. is_array()
C. do_array()
D. in_array()
Show Answer
Answer : Option (B)
Q60.
< ?php $fruits = array ("apple", "mango","peach", "pear", "orange"); $subset = array_slice($fruits, 2); print_r ($subset); ?>(code-box)
A. Array ( [0] => peach )
B. Array ( [0] => apple [1] => mango [2] => peach)
C. Array ( [0] => apple [1] => mango )
D. Array ( [0] => peach [1] => pear [2] => orange)
Show Answer
Answer : Option (B)