MCQs > IT & Programming > PHP MCQs > Basic PHP MCQs

Basic PHP MCQ

1. The metacharacter, ____, specifies an anchor at the end of the line.

Answer

Correct Answer: ($)

Note: This Question is unanswered, help us to find answer for this one

2. The command _______ :users somefile.txt changes the group affiliation of somefile.txt to users.

Answer

Correct Answer: Chown

Note: This Question is unanswered, help us to find answer for this one

3. Which function can you use in error handling to stop the execution of a script and is equivalent to exit()?

Answer

Correct Answer: Die

Note: This Question is unanswered, help us to find answer for this one

4. Which are types of control structures in PHP?

Answer

Correct Answer: Break, continue, do-while, exception, for, foreach, if, switch, throw, while

Note: This Question is unanswered, help us to find answer for this one

5. Which loop displays all numbers from 1 to 10 inclusive?

Answer

Correct Answer: $i = 0; while ($i < 10) { echo ++$i . '
'; }

Note: This Question is unanswered, help us to find answer for this one

6. You want to find out what day Twelfth Night falls on after Christmas 2018. Which code should you use?

Answer

Correct Answer: $twelfth_night = strtotime('December 25, 2018 + 12 days'); echo strftime('%d', $twelfth_night);

Note: This Question is unanswered, help us to find answer for this one

7. Assume that $r is 255, and $g and $b are both 0. What is the correct code to output "#ff0000"?

Answer

Correct Answer: Printf('#%02x%02x%02x', 255, 0, 0);

Note: This Question is unanswered, help us to find answer for this one

8. You want to use wildcard characters when searching for records in a MySQL/MariaDB database using a PDO prepared statement. Which code should you use?

Answer

Correct Answer: $statement->bindValue(':name', '%' . $_GET['name'] . '%');

Note: This Question is unanswered, help us to find answer for this one

9. Why is it not recommended to make all of a class's variables public?

Answer

Correct Answer: You will have no control over which values the attribute can take. Any external code will be able to change it without any constraint.

Note: This Question is unanswered, help us to find answer for this one

10. Which two functions can sanitize text and validate text formats?

Answer

Correct Answer: Filter_var() and filter_input()

Note: This Question is unanswered, help us to find answer for this one

11. What are some of the main types of errors in PHP?

Answer

Correct Answer: Notices, warnings, fatal

Note: This Question is unanswered, help us to find answer for this one

12. Your site must allow uploading of large files. What might you need to do?

Answer

Correct Answer: Change the upload_max_filesize configuration parameter.

Note: This Question is unanswered, help us to find answer for this one

13. Which code will return the IP address of the client?

Answer

Correct Answer: $_SESSION[
Getenv(

Note: This question has more than 1 correct answers

Note: This Question is unanswered, help us to find answer for this one

14. DRY (Don't Repeat Yourself) is a principle of software development aimed at reducing repetition of software patterns. Which choice is not a way to write DRYer code with PHP?

Answer

Correct Answer: Namespacing

Note: This Question is unanswered, help us to find answer for this one

15. Which superglobal variable holds information about headers, paths, and script locations?

Answer

Correct Answer: $_SERVER

Note: This Question is unanswered, help us to find answer for this one

16. What type of computer language is PHP?

Answer

Correct Answer: Server-side scripting language

Note: This Question is unanswered, help us to find answer for this one

17. Which is the most complete list of data types that PHP supports?

Answer

Correct Answer: String, integer, float, boolean, array, object, NULL, resource

Note: This Question is unanswered, help us to find answer for this one

18. In PHP 7, What is the correct way to import multiple classes from namespace in a single declaration ?!

Answer

Correct Answer: Use myApp\myNamespace{ClassA, ClassB, ClassC};

Note: This Question is unanswered, help us to find answer for this one

19. Imagine a web application, built following a MVC architecture, that contains a quiz and a button to score it, When the user presses the Score button, which component should handle the request?

Answer

Correct Answer: Controller

Note: This Question is unanswered, help us to find answer for this one

20. Which is not a valid magic constant?

Answer

Correct Answer: __RESOURCE__

Note: This Question is unanswered, help us to find answer for this one

21. Where is PHP code executed?

Answer

Correct Answer: On a web server

Note: This Question is unanswered, help us to find answer for this one

22. Which command will extract the domain suffix ("com") from the string $string = "https://cat-bounce.com";?

Answer

Correct Answer: Substr($string, -3)

Note: This Question is unanswered, help us to find answer for this one

23. Which PHP variable name is invalid?

Answer

Correct Answer: $2times

Note: This Question is unanswered, help us to find answer for this one

24. What are getters and setters?

Answer

Correct Answer: Getters and setters are methods used to declare or obtain the values of variables, usually private ones

Note: This Question is unanswered, help us to find answer for this one

25. Why should you follow a PSR standard?

Answer

Correct Answer: Because coding standards often vary between developers and companies

Note: This Question is unanswered, help us to find answer for this one

26. You are using the following code to decide if a button is clicked, but it is never returning true. Which step is most likely to shed light on the problem? isset($_POST['submit'])

Answer

Correct Answer: Make sure the input field displaying the button is named 'submit'

Note: This Question is unanswered, help us to find answer for this one

27. If you want to pass a formfield to another page when a button is clicked, you should use the ** . If you want to store information across multiple pages, you should use the ** ?

Answer

Correct Answer: Request; session

Note: This Question is unanswered, help us to find answer for this one

28. When it comes to the value of a variable, what is the difference between NULL and empty?

Answer

Correct Answer: NULL is the lack of a value; empty is a blank value.

Note: This Question is unanswered, help us to find answer for this one

29. Assuming that $first_name and $family_name are valid strings, which statement is invalid?

Answer

Correct Answer: Echo $first_name. ' '. $family_name;

Note: This Question is unanswered, help us to find answer for this one

30. A PDO object called $db has been set up to use for database operations, including user authentication. All user-related properties are set. The script line public function __construct(&$db) shows a constructor that initializes all user-related properties to _ if no user has logged in. These parameters will be properly set by the login functions when a user logs in.

Answer

Correct Answer: NULL

Note: This Question is unanswered, help us to find answer for this one

31. Why does this code trigger an error?

Answer

Correct Answer: The apostrophe needs to be escaped by a backslash to prevent it from being treated as the closing quote.

Note: This Question is unanswered, help us to find answer for this one

32. What is the job of the controller as a component in MVC?

Answer

Correct Answer: The controller handles data passed to it by the view, and also passes data to the view. It interprets data sent by the view and disperses that data to the approrpiate models awaiting results to pass back to the view.

Note: This Question is unanswered, help us to find answer for this one

33. You want to list the modules available in your PHP installation. What command should you run?

Answer

Correct Answer: Php -m

Note: This Question is unanswered, help us to find answer for this one

34. The php not operator is !. Given the snippet, is there an out put and what is it?

Answer

Correct Answer: Output '21 is an odd number'

Note: This Question is unanswered, help us to find answer for this one

35. Which php control structure is used inside a loop to skip the rest of the current loops code and go back to the start of the loop for the next iteration

Answer

Correct Answer: Continue

Note: This Question is unanswered, help us to find answer for this one

36. What is the cause of 'Cannot modify header information - headers already sent'?

Answer

Correct Answer: Some html is being sent before a header() command that you are using for a redirect

Note: This Question is unanswered, help us to find answer for this one

37. Which code would you use to print all the elements in an array called $cupcakes?

Answer

Correct Answer: All of the answers

Note: This Question is unanswered, help us to find answer for this one

38. You are using the following code to find a users band, but it is returning false. Which step(s) would solve the problem? isset ($_GET['fav_band'])

Answer

Correct Answer: All of the answers

Note: This Question is unanswered, help us to find answer for this one

39. Both self and this are keywords that can be used to refer to member variables of an enclosing class. The difference is that $this->member should be used for ** members and self::$member should be used for ** members.

Answer

Correct Answer: Non-static,static

Note: This Question is unanswered, help us to find answer for this one

40. Your php page is unexpectedly rendering as totally blank. Which step will shed light on the problem?

Answer

Correct Answer: All of these answers

Note: This Question is unanswered, help us to find answer for this one

41. Both triple === and double == can be used to ** variables in php. If you want to hear that string "33" and the number 33 are equal, you would use ** . If you want to check if an array contains a particular string value at a particular index, you would use _

Answer

Correct Answer: Compare; doubles; triples

Note: This Question is unanswered, help us to find answer for this one

42. Assuming the Horse class exists, which is a valid example of inheritance in PHP?

Answer

Correct Answer: Class Pegasus extends Horse {}

Note: This Question is unanswered, help us to find answer for this one

43. Which operator would you use to find the remainder after division?

Answer

Correct Answer: %

Note: This Question is unanswered, help us to find answer for this one

44. What displays in a browser when the following code is written?

Answer

Correct Answer: The browser would display How much are the bananas?

Note: This Question is unanswered, help us to find answer for this one

45. How might you troubleshoot a "call to undefined function" error?

Answer

Correct Answer: All of these answers

Note: This Question is unanswered, help us to find answer for this one

46. The ignore_user_abort( ) function sets whether a client disconnect should abort a script execution. In what scenario would you, as a web developer, use this function?

Answer

Correct Answer: You would use this function if you have some important processing to do and you do not want to stop it, even if your users click Cancel.

Note: This Question is unanswered, help us to find answer for this one

47. PHP supports multiple types of loops. If you wanted to loop through a block of code if and as long a specified condition is true, which type of loop would you use?

Answer

Correct Answer: While

Note: This Question is unanswered, help us to find answer for this one

48. Which is the correct format for adding a comment to a PHP script?

Answer

Correct Answer: All of these answers

Note: This Question is unanswered, help us to find answer for this one

49. Which code snippet uses the correct syntax for creating an instance of the Pet class?

Answer

Correct Answer: All of these answers

Note: This Question is unanswered, help us to find answer for this one

50. What is the purpose of adding a lowercase "u" as a modifier after the final delimiter in a Perl-compatible regular expression?

Answer

Correct Answer: Both the pattern and subject string are treated as UTF-8.

Note: This Question is unanswered, help us to find answer for this one

51. What is the value of $total in this calculation?$total = 2 + 5 * 20 - 6 / 3

Answer

Correct Answer: 100

Note: This Question is unanswered, help us to find answer for this one

52. Which value equates to true?

Answer

Correct Answer: -1

Note: This Question is unanswered, help us to find answer for this one

53. Which are valid PHP error handling keywords?

Answer

Correct Answer: Try, throw, catch, finally

Note: This Question is unanswered, help us to find answer for this one

54. The ** operator is useful for sorting operations. It compares two values and returns an integer less than, equal to, or greater than 0 depending on whether the value on the ** is less than, equal to, or greater than the other.

Answer

Correct Answer: Spaceship; left

Note: This Question is unanswered, help us to find answer for this one

55. What is a key difference between GET and POST?

Answer

Correct Answer: GET displays the submitted data as part of the URL. During POST, this information is not shown, as it's encoded in the request body.

Note: This Question is unanswered, help us to find answer for this one

56. All variables in PHP start with which symbol?

Answer

Correct Answer: $

Note: This Question is unanswered, help us to find answer for this one

57. N a conditional statement, you want to execute the code only if both value are true. Which comparison operator should you use?

Answer

Correct Answer: &&

Note: This Question is unanswered, help us to find answer for this one

58. Which is the most secure way to avoid storing a password in clear text in database?

Answer

Correct Answer: $encrypted = password_hash($password, PASSWORD_DEFAULT);

Note: This Question is unanswered, help us to find answer for this one

59. What does this code output?echo 76 <=> '76 trombones';

Answer

Correct Answer: A parser error

Note: This Question is unanswered, help us to find answer for this one

60. The ____ function writes a text string and creates the file if it does not exist.

Answer

Correct Answer: File_put_contents()

Note: This Question is unanswered, help us to find answer for this one

61. A web application starts when a client sends a/an ________________ to a server.

Answer

Correct Answer: Request

Note: This Question is unanswered, help us to find answer for this one

62. A _______ is a complete declarative sentence that summarizes your speech.

Answer

Correct Answer: Central idea

Note: This Question is unanswered, help us to find answer for this one

63. A(n) ____ operator is used in php to perform mathematical calculations.

Answer

Correct Answer: Arithmetic.

Note: This Question is unanswered, help us to find answer for this one

64. To sort an associative array by key, use the ____ function.

Answer

Correct Answer: Ksort()

Note: This Question is unanswered, help us to find answer for this one

65. The ____ function is used to create a constant.

Answer

Correct Answer: Define()

Note: This Question is unanswered, help us to find answer for this one

66. The ____ function converts all of the letters in a string to capital letters.

Answer

Correct Answer: Strtoupper()

Note: This Question is unanswered, help us to find answer for this one

67. The class ________ starts with the word class followed by the name of the class.

Answer

Correct Answer: Definition

Note: This Question is unanswered, help us to find answer for this one

68. You use the ____ function to find the total number of elements in an array.

Answer

Correct Answer: Count()

Note: This Question is unanswered, help us to find answer for this one

69. You can create ____ that consist of multiple indexes or keys.

Answer

Correct Answer: Multidimensional arrays

Note: This Question is unanswered, help us to find answer for this one

70. Instead of reading a large text file into php, you can use the ____ to iterate through a text file.

Answer

Correct Answer: File pointer

Note: This Question is unanswered, help us to find answer for this one

71. In php, the escape character is the ____.

Answer

Correct Answer: Backslash

Note: This Question is unanswered, help us to find answer for this one

72. In php, a(n) e-mail message is sent using the ____ function.

Answer

Correct Answer: Mail()

Note: This Question is unanswered, help us to find answer for this one

73. By default, the properties of a php class are ____________.

Answer

Correct Answer: Public

Note: This Question is unanswered, help us to find answer for this one

74. A form that contains the values that a user previously entered in the form is called a ____ form.

Answer

Correct Answer: Sticky

Note: This Question is unanswered, help us to find answer for this one

75. Which of the following is correct about NULL?

Answer

Correct Answer: Both of the above.

Note: This Question is unanswered, help us to find answer for this one

76. A _________ is a way to store information (in variables ) to be used across multiple pages.

Answer

Correct Answer: Session
Cookie

Note: This question has more than 1 correct answers

Note: This Question is unanswered, help us to find answer for this one

77. You can extend the exception class, but you cannot override any of the preceding methods because they are declared as

Answer

Correct Answer: final

Note: This Question is unanswered, help us to find answer for this one

78. How would you create enums in PHP?

Answer

Correct Answer: Use SplEnum which gives the ability to emulate and create enumeration objects natively in PHP.

Note: This Question is unanswered, help us to find answer for this one

79.

If you are given two dates in this form   ??: Start Date: 2007-03-24 End Date: 2009-06-26 Which code snippet finds the difference between these two in the following form: 2 years, 3 months and 2 days PHP > 5.3


Answer

Correct Answer:

$date1 = "2008-11-01 22:45:00"; $date2 = "2009-12-04 13:44:01"; $diff = abs(strtotime($date2) - strtotime($date1)); $years = floor($diff / (365*60*60*24)); $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24)); $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24)); $hours = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24)/ (60*60)); $minuts = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60)/ 60); $seconds = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60 - $minuts*60)); printf("%d years, %d months, %d days, %d hours, %d minuts\n, %d seconds\n", $years, $months, $days, $hours, $minuts, $seconds);


Note: This Question is unanswered, help us to find answer for this one

80. What is $_REQUEST?

Answer

Correct Answer: Includes all variables submitted through HTTP POST, such as an HTML form with action="post".

Note: This Question is unanswered, help us to find answer for this one

81. How can short tags be enabled in PHP?

Answer

Correct Answer: Set the value of short_open_tag=on in the php.ini file and restart the web server.

Note: This Question is unanswered, help us to find answer for this one

82. The ________ function delays execution of the current script for a specified number of seconds.

Answer

Correct Answer: sleep()

Note: This Question is unanswered, help us to find answer for this one

83. Which of the following loops does not exist in PHP?

Answer

Correct Answer: until loop

Note: This Question is unanswered, help us to find answer for this one

84. ___ will display everything about the PHP installation, including the modules, version, variables, etc.

Answer

Correct Answer: phpinfo()

Note: This Question is unanswered, help us to find answer for this one

85. Pick the correct PHP function to rewind the internal array pointer.

Answer

Correct Answer: prev()

Note: This Question is unanswered, help us to find answer for this one

86.

php $x = 1; while ($x <= 3){ echo X}

What will be the output of above code?


Answer

Correct Answer:

1, 2, 3,



Note: This Question is unanswered, help us to find answer for this one

87. Which of the following is used to print today

Answer

Correct Answer: echo date(format,timestamp);

Note: This Question is unanswered, help us to find answer for this one

88. PHP ____ are used to validate and sanitize external input.

Answer

Correct Answer: Filters

Note: This Question is unanswered, help us to find answer for this one

89. Which of the following will return the client

Answer

Correct Answer: $_SERVER['REMOTE_ADDR']

Note: This Question is unanswered, help us to find answer for this one

90. What does "===" operator do in PHP?

Answer

Correct Answer: Checks if the left and right values are equal and are of same type.

Note: This Question is unanswered, help us to find answer for this one

91. Which operator is used to access static properties or methods of a class?

Answer

Correct Answer: ::

Note: This Question is unanswered, help us to find answer for this one

92. Several predefined variables in PHP are _____.

Answer

Correct Answer: Superglobals

Note: This Question is unanswered, help us to find answer for this one

93. Which PHP sort method sorts by value?

Answer

Correct Answer: asort()

Note: This Question is unanswered, help us to find answer for this one

94. What is the time interval in the given dateInterval object specification: new DateInterval("P0000-10-00T00:01:01");

Answer

Correct Answer: 1 hour and 1 minute

Note: This Question is unanswered, help us to find answer for this one

95. Which of the following is not a valid directive for declare construct?

Answer

Correct Answer: strict_types

Note: This Question is unanswered, help us to find answer for this one

96. Which of the following will produce a value of "83" as its output??

Answer

Correct Answer: echo intval(

Note: This Question is unanswered, help us to find answer for this one

97.

Which of the following code snippets would give two functions that would take a string and return, if it starts or ends with the specified character/string?

For example: $str='{apples}'; echo startsWith($str,'|'); //Returns true echo endsWith($str,'}'); //Returns true

Answer

Correct Answer:

function startsWith($haystack, $needle) { return !strncmp($haystack, $needle, strlen($needle)); } function endsWith($haystack, $needle) { $length = strlen($needle); if ($length == 0) { return true; } return (substr($haystack, -$length) === $needle); }



Note: This Question is unanswered, help us to find answer for this one

98. How to check if a variable is empty?

Answer

Correct Answer: empty($variable)

Note: This Question is unanswered, help us to find answer for this one

99. Which statement is incorrect with respect to "!==" and "==!"?

Answer

Correct Answer: !== returns true if left hand side operand is not identical to right hand side operand.

Note: This Question is unanswered, help us to find answer for this one

100. Which of the following data types are supported in PHP? Check all the apply.

Answer

Correct Answer: Float
Boolean
NULL

Note: This question has more than 1 correct answers

Note: This Question is unanswered, help us to find answer for this one

101. _________ are like variables except that once they are defined they cannot be changed or undefined.

Answer

Correct Answer: Constants

Note: This Question is unanswered, help us to find answer for this one

102. __________ is a PHP super global variable which holds information about headers, paths, and script locations.

Answer

Correct Answer: $_SERVER

Note: This Question is unanswered, help us to find answer for this one

103. Which of the following method is triggered when invoking inaccessible methods in a static context?

Answer

Correct Answer: __callStatic()

Note: This Question is unanswered, help us to find answer for this one

104. What of the following variables are correct Superglobals ?

Answer

Correct Answer: $_SERVER $_GET $_POST $_COOKIE

Note: This Question is unanswered, help us to find answer for this one

105. Which expression will not produce ‘abcdef’ as a result provided if $var1=’abc’ and $var2=’def’?

Answer

Correct Answer: $var1.$var2

Note: This Question is unanswered, help us to find answer for this one

106. Which of the following statement is not correct?

Answer

Correct Answer: isset() is faster, but it's not the same as array_key_exists().

Note: This Question is unanswered, help us to find answer for this one

107. Which of the following is the correct way to create dynamic variable name in PHP?

Answer

Correct Answer: ${'ab'.$variable}='HELLO'

Note: This Question is unanswered, help us to find answer for this one

108. Which statement is not correct?

Answer

Correct Answer: sizeof() has a little bit of overhead with respect to count()

Note: This Question is unanswered, help us to find answer for this one

109. The following code would: $reflFunc = new ReflectionFunction('func'); print $reflFunc->getFileName();

Answer

Correct Answer: Give the path of 'func' function

Note: This Question is unanswered, help us to find answer for this one

110. Choose the correct PHP function that would give the size of a supported image file.

Answer

Correct Answer: getimagesize()

Note: This Question is unanswered, help us to find answer for this one

111.

 What is the output when the following code is executed?  $alpha = array('A' => 'AA', 'Bud' => 'BB','C' => 'D'); $food = array('tea', 'drink', 'pizza', 'juice'); printf(foods var export food true)

Answer

Correct Answer:

Alpha: array ( 'A' => 'AA', 'Bud' => 'BB', 'C' => 'D', ) Food: array ( 0 => 'tea', 1 => 'drink', 2 => 'pizza', 3 => 'juice', )



Note: This Question is unanswered, help us to find answer for this one

112.

Which following code snippet is about setting auto commit mode with SQL and through the API?