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

51: Which keyword is used to refer to properties or methods within the class itself?

A.   private

B.   protected

C.   $this

D.   public

52: Which of the following is the correct way to add comments to PHP?

A.   All listed methods are valid

B.   /* Comment code here */

C.   // Comment code here

D.   # Comment code here

53: Which PHP extension provides an abstract method for database retrieval?

A.   TRC

B.   STMP

C.   PDO

D.   POP

54: Which of the following language constructs is used to terminate a running script?

A.   kill()

B.   break()

C.   exit()

D.   halt()

E.   eval()

55: To output information about a variable use the function

A.   print_var()

B.   display_var()

C.   output()

D.   var_dump()

56: What function converts 'Hatfields & McCoys' into 'Hatfields & McCoys'?

A.   htmlentities()

B.   ampersand()

C.   transform()

D.   convert()

57: Which PHP function creates an array by using one array for keys and another for its values?

A.   array_combine()

B.   combine()

C.   array2()

D.   $array()

58: Which statement is true about PHP constants?

A.   They are case-sensitive by default.

B.   They cannot be changed.

C.   They cannot contain an array.

D.   All of these.

59: HTTP cookies can be used to:

A.   identify a user.

B.   create client side scripts.

C.   validate input fields.

D.   check for SQL injections.

60: Given, the following: What is the output?

A.   retail computer computer

B.   retail advertising computer

C.   retail retail retail

D.   retail retail computer

61: True or false? We can include ("sandy.php") two times in one PHP page "my.php".

A.   False

B.   True

62: What type of operator is && in PHP?

A.   arithmetic operator

B.   comparison operator

C.   assignment operator

D.   logical operator

63: How do you perform a strict comparison of variables for type and value?

A.   $a = $b

B.   $a === $b

C.   None of these

D.   $a == $b

64: Which function returns one or more randomly selected keys from an array?

A.   array_rshuffle()

B.   array_rchunk()

C.   array_rand_pop()

D.   array_rand()

E.   array_diff_key()

65: Which PHP function creates a directory on an FTP server?

A.   ftp_chdir()

B.   ftp_create_dir()

C.   ftp_mkdir()

D.   ftp_make_dir()

66: Which function allows you to specify a start position of a string and length arguments to retrieve part of a string?

A.   substr()

B.   chunk_split()

C.   parse_str()

D.   str()

67: What is the result of the following code?

A.   hELLO

B.   HELLO

C.   hellO

D.   None of these

E.   hello

68: Which of the following displays the browser properties using PHP?

A.   $_SERVER['BROWSER_DETAIL']

B.   $_SERVER['HTTP_AGENT']

C.   $_SERVER['BROWSER_NAME']

D.   $_SERVER['HTTP_USER_AGENT']

69: When using the POST method, variables are displayed in the URL:

A.   True

B.   Only when posted with AJAX

C.   False

D.   Only when posted by a form

70: What is the official website of PHP

A.   php.org

B.   php.net

C.   php.com

D.   php.edu

E.   php.us

71: mysqli_num_rows() function is used to:

A.   Count number of rows in result of MySql Query.

B.   Get all rows contain numbers.

C.   Get all columns with numeric data.

D.   Count number of columns in MySql Table.

E.   Count number of characters in single row of MySql Query result.

72: How do you specify a string literal in PHP?

A.   All of these

B.   "Hello there"

C.   <<<EOD Hello there EOD;

D.   'Hello there'

73: Which function returns information on the PHP configuration you are using?

A.   phpAll();

B.   phpNotice();

C.   phpinfo();

D.   phpsearch();

E.   phpconfig();

74: Which of the following is a string operator?

A.   plus(+)

B.   dot(.)

C.   decrement(--)

D.   minus(-)

75: What is the right way to set a value on an object stdClass?

A.   $variable.value = 'value';

B.   $variable.['value'] = 'value';

C.   $variable->value = 'value';

D.   $variable['value'] = 'value';

E.   $variable[value] = 'value';

76: How do you inherit the methods from BaseClass in MyClass?

A.   class MyClass parent BaseClass {

B.   class MyClass extends BaseClass {

C.   class MyClass inherits BaseClass {

D.   class MyClass includes BaseClass {

E.   class MyClass using BaseClass {

77: Which variable contains values of $_GET, $_POST, and $_COOKIE?

A.   $_ALL

B.   $_REQUEST

C.   $_MERGE

D.   $_INCLUDE

78: Can PHP scripts be embedded directly into XHTML documents?

A.   No

B.   Yes

C.   Depends on the Operating System

79: __construct is a

A.   regex method for an object.

B.   class for the object.

C.   constructor for an object.

D.   pseudo element for an object.

80: An Object is a/an ___ of a class

A.   Prototype

B.   Type

C.   Object

D.   Instance

81: Which of the following stores PHP values, such as strings, numbers or arrays?

A.   classes

B.   attributes

C.   variables

D.   ID's

82: Which of these assigns a value of type string to variable $x?

A.   $x[] = "true";

B.   $x = "true";

C.   $x = true;

83: If a string is enclosed in double-quotes, PHP will interpret more escape sequences for special characters such as

A.   \r

B.   \n

C.   \"

D.   All of these

84: True or False : list — Assign variables as if they were an array

A.   Yes

B.   No

85: Which statement returns the number of characters in the string variable $a?

A.   count($a)

B.   strlen($a)

C.   len($a)

D.   chr($a)

E.   sizeof($a)

86: Which PHP math function would you use to return the number with the lowest value of two specified numbers?

A.   ceil()

B.   max()

C.   min()

D.   bottom()

87: How to call a method from within a class?

A.   public my_function();

B.   private my_function();

C.   $this->my_function();

D.   $this.my_function();

E.   $this( my_function() );

88: Which PHP function returns part of a string?

A.   ctrl alt delete

B.   All of these

C.   substr()

D.   strpos()

89: In the following PHP script, $arr = array(1,2,3);, 1, 2, and 3 are the array

A.   subscript

B.   index

C.   values

D.   keys

90: Which of the following is NOT a command for sending output to the standard output?

A.   print

B.   send

C.   echo

D.   printf

91: What is meant by nl2br()?

A.   Inserts HTML line breaks (<BR />) before all newlines in a string.

B.   Inserts HTML line breaks (<P />) before all newlines in a string.

C.   Inserts HTML line breaks (<NL/>) before all newlines in a string.

D.   Inserts HTML line breaks (<HR />) before all newlines in a string.

92: Choose the correct value for $i. $i = 2; $i++;

A.   3

B.   2

C.   5

D.   4

93: Which of the following variables does PHP make automatically available to you?

A.   $_DIR

B.   $_UPLOADED_FILES

C.   $_SERVER

94: Which PHP function finds the position of the last occurrence of a substring within a string?

A.   haystack()

B.   strrpos()

C.   split()

D.   pos()

95: To modify the default PHP settings such as whether to accept the short form open tag (<?), you need to change the

A.   htaccess file.

B.   php.ini file.

C.   php.inc file.

D.   machine.config file.

96: Which construct is typically used for exception handling in PHP?

A.   modify()

B.   try / catch

C.   E_STRICT

D.   session();

97: Which function tests a string for an exact match of the argument?

A.   preg_match()

B.   regex()

C.   test()

D.   str()

98: Which statements start a session and end a session?

A.   session_start(); unset_session();

B.   begin_session(); end_session();

C.   start_session(); session_explode();

D.   session_start(); session_destroy();

E.   start_session(); end_session();

99: Which of these does not represent a single, valid PHP variable

A.   $my-Var

B.   $my_Var

C.   $myVar

D.   $myvar

100: Which of the following are examples of PHP comments?

A.   Both // and /* */

B.   /* */

C.   //

D.   None of these