PHP MCQs

PHP MCQs

Try to answer these 1000+ PHP MCQs and check your understanding of the PHP subject. Scroll down and let's begin!

1: What will be the value of $result? 1, 'val2' => array('val21' => 21, 'val22' => 22), 'val3' => 3 ); $result = array_key_exists('val22', $myArr); ?>

A.   false

B.   " "

C.   true

D.   an error

2: What are Traits in PHP?

A.   Traits are class interfaces.

B.   Traits are all of these things.

C.   Traits allow code reuse without direct inheritance.

D.   Traits allow multiple inheritance for classes.

3: What function allows you to print a backtrace?

A.   backtrace_print

B.   print_backtrace

C.   backtrace

D.   debug_backtrace_print

E.   debug_print_backtrace

4: How do single quotes interpret strings in PHP?

A.   literally except for variables which are expanded

B.   literally

C.   escape sequences are interpreted

D.   None of these

5: What is the recommendation regarding PHP closing tags in a document?

A.   Only use the closing tag when short tags are enabled.

B.   End all PHP files with '?>'.

C.   Omit the closing tag when the file contains only PHP code.

D.   There is no recommendation.

6: What will be the output for the following code? $values = array(0, NULL, FALSE); foreach($values as $key => $value) { echo isset($values[$key]) ? "TRUE " : "FALSE "; }

A.   FALSE FALSE FALSE

B.   TRUE FALSE FALSE

C.   TRUE TRUE TRUE

D.   fatal error when using isset on an array

E.   TRUE FALSE TRUE

7: Which statement opens a file for writing, to append data at the end of its content ?

A.   fopen("myFile", "wa")

B.   fopen("myFile", "a")

C.   fopen("myFile", "r+")

D.   fopen("myFile", "w")

8: The PHP function which capitalizes the first letter of each word in a string

A.   strtoupper()

B.   ucfirst()

C.   caps()

D.   ucwords()

9: How can you call this function? $greet = function($name) { printf("Hello %s\r\n", $name); };

A.   $greet $World;

B.   PHP does not support this syntax.

C.   $greet('World');

10: $table = array("old_value"); foreach($table as $row) { $row="new_value"; } echo $table[0]; will produce:

A.   "new_value"

B.   "old_value"

C.   Fatal error, as the variable used in foreach is read-only

11: To access functions of an instantiated class, you use the following:

A.   $class::function()

B.   class->function()

C.   class.function()

D.   $class->function()

E.   class::function

12: Which PHP function checks to see if HTTP headers have been sent?

A.   headers_sent()

B.   sent()

C.   http_response_code()

D.   header()

13: What is one of the difference between the use of single quotes and double quotes in PHP syntax?

A.   single quoted strings cannot contain line breaks

B.   variables names will not be expanded in double quotes

C.   variable names will be expanded in double quotes

D.   double quoted strings cannot contain line breaks

14: Which statement will NOT combine strings $s1 and $s2 into a single string?

A.   implode(' ',array($s1,$s2))

B.   "{$s1} {$s2}"

C.   $s1.$s2

D.   $s1 + $s2

15: In this example code, $result = print_r ($a); $result is the

A.   expression.

B.   variable.

C.   property.

D.   argument.

16: Given the following code, the Atom class can access which variables? class Flash { public $fast; private $superSpeed; private $ultraSonic; protected $warp; } class Atom extends Flash { }

A.   $fast, $warp

B.   $fast, $superSpeed, $ultraSonic

C.   $fast

D.   $superSpeed, $ultraSonic

E.   $fast, $superSpeed, $ultraSonic, $warp

17: Which is a valid character for starting a PHP function name?

A.   All of these

B.   dollar sign($)

C.   percent(%)

D.   underscore(_)

18: Atleast how many abstract methods must an abstract class contain?

A.   Five

B.   One

C.   Two

D.   More than two

E.   None

19: Which of the following is not a valid PHP variable declaration?

A.   $aVaR

B.   ${“MyVar”}

C.   $10_var

D.   $_10

20: Choose the string for which the POSIX regular expression ^[A-Za-z]$ matches.

A.   2I

B.   67

C.   P

D.   &

21: The interpreter executes one PHP command which ends in a semi-colon. This is referred to as a

A.   PHP statement.

B.   All of these

C.   PHP array.

D.   PHP function.

22: What is the value of $start after exiting the for loop?

A.   2

B.   4

C.   3

D.   5

E.   1

23: Which type of array allows you to use textual keys so that array indices can be more descriptive?

A.   Key

B.   Textual

C.   Associative

D.   All of these

24: The following code will produce: var_dump(5 === "5");

A.   bool(false)

B.   int(5)

C.   bool(true)

D.   string(1) "5"

25: How would you create a numeric array from the following string that did not include "-test" and where each digit had its own array key? $str = '6.1.4.2.6-test';

A.   $arr = implode('-', $str); $arr = implode('.', $arr[0]);

B.   You can't

C.   $arr = explode('-', $str); $arr = explode('.', $arr[0]);

D.   $arr = str_replace('-', '', $str);

26: Which PHP function can be used to generate a unique ID for members of your site?

A.   mt_random()

B.   unpack()

C.   php_create_user()

D.   uniqid()

27: What is the GD Library?

A.   It is a library that gives the PHP the capability to access and create virtual domains. - (Generated Domain)

B.   It is a library that gives the PHP the capability to create and manipulate image files. - (Gif Draw)

C.   it's add a dozen of special new $_GLOBAL variables that gives principally informations on the user (localization, common social media cookies, etc...) - (Global iDentification)

28: What is the output of the following code?

A.   fastest faster

B.   fastest fastest

C.   faster faster

D.   faster fastest

29: Which keyword precedes a method name?

A.   method

B.   protected

C.   function

D.   public

30: Which of the following is NOT a function of PHP session support?

A.   session_pw

B.   session_destroy

C.   session_decode

D.   session_id

31: What is a collection of characters that is treated as one entity called?

A.   Function

B.   Element

C.   Parameters

D.   String

32: A fatal run-time error would be caught by

A.   E_ERROR

B.   E_WARNING

C.   ERROR

D.   E_NOTICE

33: Which function checks for the existence of a particular key?

A.   isset()

B.   array_key_exists()

C.   element()

D.   for_each()

34: What function can be used to help prevent MySQL injection?

A.   mysql_real_escape_string()

B.   mysql_escape()

C.   escape_mysql_string()

D.   None of the above

E.   mysqlescapethisstring()

35: Which PHP function sorts an array and maintains index association?

A.   awalk()

B.   asort()

C.   sort()

D.   array_alpha()

36: How do I typecast a variable to boolean?

A.   $var = (bool)$var;

B.   $var = ($var == $var);

C.   Both are valid statements.

D.   It's not possible to typecast variables like this.

37: Which of the following functions changes directory on ftp-connection?

A.   ftp_chmod()

B.   ftp_login()

C.   ftp_connect()

D.   ftp_chdir()

E.   ftp_mkdir()

38: What is the result of the following code?

A.   Array

B.   0

C.   2

D.   3

39: OCI8, PostgreSQL, and SQLSRV are all examples of

A.   queries available in PHP.

B.   database extensions available in PHP.

C.   form extensions available in PHP

D.   All of these

40: Which of the following outputs a formatted string?

A.   printf("Hello %s", $name);

B.   print("Hello %s", $name);

C.   echo("Hello %s", $name);

D.   output("Hello %s", $name);

41: What is a large binary data object called in PHP?

A.   lib

B.   blob

C.   bin

D.   blurb

42: The ......... function parses an English textual date or time into a Unix timestamp

A.   str_to_time()

B.   strtodate()

C.   strtotime()

D.   stroftime()

43: True or False? Arrays in PHP can only hold the same data type.

A.   True

B.   False

44: What is the output from this example? $str = "My name is O\'Connor."; echo stripslashes($str);

A.   My name is OConnor.

B.   My name is O'Connor.

C.   None of these

D.   My name is O\'Connor.

45: Which function allows you to delete a file?

A.   remove

B.   clearfile

C.   delete

D.   unlink

46: Which function removes all HTML and PHP tags?

A.   remove_html_php()

B.   tag_remove()

C.   remove_tags()

D.   strip_tags()

47: What is output to the screen when executing code: $> php -r

A.   aa

B.   PHP Parse error

C.   truea

D.   a

48: Which datatypes are treated as arrays in some cases?

A.   integer

B.   double

C.   strings

D.   float

49: What is the output?

A.   syntax error

B.   falsefalse

C.   true

D.   false

E.   None of the these

50: What type of statements tell your PHP script to execute a section of code only if certain criteria are met?

A.   Function

B.   Class

C.   Exception

D.   Conditional