0% found this document useful (0 votes)
67 views

PHP

Uploaded by

eliyasdida1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

PHP

Uploaded by

eliyasdida1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

SanfoundryMenu

PHP MCQ (Multiple Choice Questions)

Here are 1000 MCQs on PHP (Chapterwise).

1. What is PHP?

a) PHP is an open-source programming language

b) PHP is used to develop dynamic and interactive websites

c) PHP is a server-side scripting language

d) All of the mentioned

View Answer

Answer: d

Explanation: PHP is an open-source server-side scripting language that is used to build dynamic and
interactive web pages or web applications.

2. Who is the father of PHP?

a) Drek Kolkevi

b) Rasmus Lerdorf

c) Willam Makepiece

d) List Barely

View Answer

Answer: b

Explanation: PHP was originally created by Rasmus Lerdorf in 1994.

3. What does PHP stand for?

a) PHP stands for Preprocessor Home Page


b) PHP stands for Pretext Hypertext Processor

c) PHP stands for Hypertext Preprocessor

d) PHP stands for Personal Hyper Processor

View Answer

4. Which of the following is the correct syntax to write a PHP code?

a) <?php ?>

b) < php >

c) < ? php ?>

d) <? ?>

View Answer

Answer: d

Explanation: Every section of PHP code starts and ends by turning on and off PHP tags to let the server
know that it needs to execute the PHP in between them.

5. Which of the following is the correct way to add a comment in PHP code?

a) #

b) //

c) /* */

d) All of the mentioned

View Answer

Answer: d

Explanation: In PHP, /* */ can also be used to comment just a single line although it is used for
paragraphs. // and # are used only for single-line comments.

advertisement
6. Which of the following is the default file extension of PHP files?

a) .php

b) .ph

c) .xml

d) .html

View Answer

Answer: a

Explanation: To run a PHP file on the server, it should be saved as AnyName.php

7. How to define a function in PHP?

a) functionName(parameters) {function body}

b) function {function body}

c) function functionName(parameters) {function body}

d) data type functionName(parameters) {function body}

View Answer

Answer: c

Explanation: PHP allows us to create our own user-defined functions. Any name ending with an open
and closed parenthesis is a function. The keyword function is always used to begin a function.

8. What will be the output of the following PHP code?

<?php

$x = 10;

$y = 20;

if ($x > $y && 1||1)

print "1000 PHP MCQ" ;


else

print "Welcome to Sanfoundry";

?>

a) no output

b) Welcome to Sanfoundry

c) 1000 PHP MCQ

d) error

View Answer

Answer: c

Explanation: Expression evaluates to true.

Output:

1000 PHP MCQ

9. Which is the right way of declaring a variable in PHP?

a) $3hello

b) $_hello

c) $this

d) $5_Hello

View Answer

Answer: b

Explanation: A variable in PHP can not start with a number, also $this is mainly used to refer properties
of a class so we can’t use $this as a user defined variable name.

10. What will be the output of the following PHP program?

<?php
$fruits = array ("apple", "orange", array ("pear", "mango"),"banana");

echo (count($fruits, 1));

?>

a) 6

b) 5

c) 4

d) 3

View Answer

Answer: a

Explanation: function count() will return the number of elements in an array. The parameter 1 counts
the array recursively i.e it will count all the elements of multidimensional arrays.

advertisement

11. What will be the output of the following PHP program?

<?php

function multi($num)

if ($num == 3)

echo "I Wonder";

if ($num == 7)

echo "Which One";

if ($num == 8)

echo "Is The";

if ($num == 19)

echo "Correct Answer";


}

$can = stripos("I love php, I love php too!","PHP");

multi($can);

?>

a) Correct Answer

b) Is The

c) I Wonder

d) Which One

View Answer

Answer: d

Explanation: The stripos() function finds the position of the first occurrence of a string inside another
string. In this case it returns 7.

12. Which of the following PHP functions can be used for generating unique ids?

a) md5()

b) uniqueid()

c) mdid()

d) id()

View Answer

Answer: b

Explanation: The function uniqueid() is used to generate a unique ID based on the microtime (current
time in microseconds). The ID generated from the function uniqueid() is not optimal, as it is based on
the system time. To generate an ID which is extremely difficult to predict we can use the md5() function.

13. In the following PHP program, what is/are the properties?

advertisement
<?php

class Example

public $name;

function Sample()

echo "Learn PHP @ Sanfoundry";

?>

a) function sample()

b) echo “This is an example”;

c) public $name;

d) class Example

View Answer

Answer: c

Explanation: Above code is an example of ‘classes’. Classes are the blueprints of objects. Classes are the
programmer-defined data type, which includes the local methods and the local variables. Class is a
collection of objects which has properties and behaviour.

14. What will be the output of the following PHP code?

<?php

define("GREETING", "PHP is a scripting language");

echo $GREETING;

?>
a) $GREETING

b) no output

c) PHP is a scripting language

d) GREETING

View Answer

Answer: b

Explanation: Constants do not need a $ before them, they are referenced by their variable names itself.

15. A function in PHP which starts with __ (double underscore) is known as __________

a) Default Function

b) User Defined Function

c) Inbuilt Function

d) Magic Function

View Answer

Answer: d

Explanation: PHP functions that start with a double underscore – a “__” – are called magic functions.
They are functions that are always defined inside classes, and are not stand-alone functions.

16. How many functions does PHP offer for searching and modifying strings using Perl-compatible
regular expressions.

a) 10

b) 7

c) 8

d) 9

View Answer
Answer: c

Explanation: The functions are preg_filter(), preg_grep(), preg_match(), preg_match_all(), preg_quote(),


preg_replace(), preg_replace_callback(), and preg_split().

17. Which of the following web servers are required to run the PHP script?

a) Apache and PHP

b) IIS

c) XAMPP

d) Any of the mentioned

View Answer

Answer: b

Explanation: To run PHP code you need to have PHP and a web server, both IIS, XAMPP and Apache are
web servers. You can choose either one according to your platform.

18. What will be the output of the following PHP code snippet?

<?php

$url = "[email protected]";

echo ltrim(strstr($url, "@"),"@");

?>

a) [email protected]

b) [email protected]

c) phpmcq@

d) sanfoundry.com

View Answer

Answer: d
Explanation: The strstr() function returns the remainder of a string beginning with the first occurrence of
a predefined string.

19. Which of the following PHP functions can be used to get the current memory usage?

a) memory_get_usage()

b) memory_get_peak_usage()

c) get_peak_usage()

d) get_usage()

View Answer

Answer: a

Explanation: memory_get_usage() returns the amount of memory, in bytes, that’s currently being
allocated to the PHP script. We can set the parameter ‘real_usage’ to TRUE to get total memory
allocated from system, including unused pages. If it is not set or FALSE then only the used memory is
reported. To get the highest amount of memory used at any point, we can use the
memory_get_peak_usage() function.

20. Which one of the following PHP function is used to determine a file’s last access time?

a) filetime()

b) fileatime()

c) fileltime()

d) filectime()

View Answer

Answer: b

Explanation: The fileatime() function returns a file’s last access time in Unix timestamp format or FALSE
on error.

21. What will be the output of the following PHP code?

<?php
$x = 5;

$y = 10;

function fun()

$y = $GLOBALS['x'] + $GLOBALS['y'];

fun();

echo $y;

?>

a) 5

b) 10

c) 15

d) Error

View Answer

Answer: b

Explanation: The value of global variable y does not change therefore it’ll print 10;

22. PHP recognizes constructors by the name _________

a) function __construct()

b) function _construct()

c) classname()

d) _construct()

View Answer

Answer: a
Explanation: PHP recognizes constructors by double underscore followed by the construct keyword. Its
syntax is function __construct ([ argument1, argument2,…..]) { Class Initialization code }.

23. The developers of PHP deprecated the safe mode feature as of which PHP version?

a) PHP 5.3.1

b) PHP 5.3.0

c) PHP 5.1.0

d) PHP 5.2.0

View Answer

Answer: b

Explanation: This happened because safe mode often creates many problems as it resolves, largely due
to the need for enterprise applications to use many of the features safe mode disables.

24. What will be the value of the variable $input in the following PHP program?

<?php

$input = "PHP<td>stands for</td>Hypertext<i>Preprocessor</i>!";

$input = strip_tags($input,"<i></i>");

echo $input;

?>

a) PHP stands for Hypertext <i>Preprocessor</i>!

b) PHP stands for Hypertext Preprocessor!

c) PHP <td>stands for</td> Hypertext <i>Preprocessor</i>!

d) PHP <td>stands for</td> Hypertext Preprocessor!

View Answer

Answer: a
Explanation: Italic tags <i></i> might be allowable, but table tags <td></td> could potentially wreak
havoc on a page.

25. Which of the following variables does PHP use to authenticate a user?

i) $_SERVER['PHP_AUTH_USER'].

ii) $_SERVER['PHP_AUTH_USERS'].

iii) $_SERVER['PHP_AUTH_PU'].

iv) $_SERVER['PHP_AUTH_PW'].

a) ii) and iv)

b) i) and iv)

c) ii) and iii)

d) i) and ii)

View Answer

Answer: b

Explanation: $_SERVER[‘PHP_AUTH_USER’] and $_SERVER[‘PHP_AUTH_PW’] store the username and


password values, respectively.

26. What does PDO stand for?

a) PHP Database Orientation

b) PHP Data Orientation

c) PHP Data Object

d) PHP Database Object

View Answer

Answer: c

Explanation: PDO stands for PHP Data Object. The PDO class provides a common interface to different
database applications.
27. What will be the output of the following PHP program?

<?php

$a = 100;

if ($a > 10)

printf("PHP Quiz");

else if ($a > 20)

printf("PHP MCQ");

else if($a > 30)

printf("PHP Program");

?>

a)

PHP Quiz

PHP MCQ

PHP Program

b) PHP Quiz

c) No output

d) PHP MCQ

View Answer

Answer: b

Explanation: In if else if one condition is satisfied then no other condition is checked.

28. Which of the looping statements is/are supported by PHP?


i) for loop

ii) while loop

iii) do-while loop

iv) foreach loop

a) Only iv)

b) i) and ii)

c) i), ii) and iii)

d) i), ii), iii) and iv)

View Answer

Answer: d

Explanation: All are supported looping statements in PHP as they can repeat the same block of code a
given number of times, or until a certain condition is met.

29. Which PHP statement will give output as $x on the screen?

a) echo “\$x”;

b) echo “$$x”;

c) echo “/$x”;

d) echo “$x;”;

View Answer

Answer: a

Explanation: A backslash is used so that the dollar sign is treated as a normal string character rather than
prompt PHP to treat $x as a variable. The backslash used in this manner is known as the escape
character.

30. Which version of PHP introduced the advanced concepts of OOP?

a) PHP 6

b) PHP 4
c) PHP 5

d) PHP 5.3

View Answer

Answer: c

Explanation: Advanced concepts of OOP were introduced in PHP version 5.

31. What will be the output of the following PHP code?

<?php

$x = 4;

$y = 3

$z = 1;

$z = $z + $x + $y;

echo "$z";

?>

a) 15

b) 8

c) 1

d) $z

View Answer

Answer: b

Explanation: Normal addition of variables x, y and z occurs and result of 8 will be displayed.

32. What will be the output of the following PHP program?


<?php

$a = "$winner";

$b = "/$looser";

echo $a,$b;

?>

a) /

b) $looser

c) /$looser

d) $winner/$looser

View Answer

Answer: a

Explanation: Since variables $winner and $looser is not defined we only see / as output.

33. Which one of the following is the default PHP session name?

a) PHPSESSIONID

b) PHPIDSESS

c) PHPSESSID

d) PHPSESID

View Answer

Answer: c

Explanation: PHPSESSID is the default PHP session name. You can change this name by using the
session.name directive.

34. What will be the output of the following PHP program?

<?php
$mcq = 1;

switch(print $mcq)

case 2:

print "HTML";

break;

case 1:

print "CSS";

break;

default:

print "JavaScript";

?>

a) error

b) 1HTML

c) 1JavaScript

d) 1CSS

View Answer

Answer: d

Explanation: Print returns 1, thus it gives case 1.

35. What will be the output of the following PHP program?

<?php

define("VAR_NAME","test");
${VAR_NAME} = "value";

echo VAR_NAME;

echo ${VAR_NAME};

?>

a) testtest

b) testvalue

c) error, constant value cannot be changed

d) test

View Answer

Answer: b

Explanation: ${VAR_NAME} creates a new variable that is not same as VAR_NAME.

36. Which PHP function displays the web page’s most recent modification date?

a) getlastmod()

b) get_last_mod()

c) lastmod()

d) last_mod()

View Answer

Answer: a

Explanation: The function getlastmod() gets the time of the last modification of the main script of
execution. It returns the value of the page’s last modified header or FALSE in the case of an error.

37. What will be the output of the following PHP program?

<?php

$i = 5;
while (--$i > 0 && ++$i)

print $i;

?>

a) 555555555…infinitely

b) 54321

c) error

d) 5

View Answer

Answer: a

Explanation: As it is && operator it is being incremented and decremented continuously in PHP.

38. What will be the output of the following PHP code?

<?php

function constant()

define("GREETING", "Welcome to Sanfoundry",true);

echo greeting;

?>

a) GREETING

b) Welcome to Sanfoundry

c) ERROR
d) greeting

View Answer

Answer: b

Explanation: By default, constants are case sensitive in php. But the third parameter in define(), if set to
true, makes constants case insensitive.

39. Which variable is used to collect form data sent with both the GET and POST methods?

a) $_BOTH

b) $REQUEST

c) $_REQUEST

d) $BOTH

View Answer

Answer: c

Explanation: In PHP the global variable $_REQUEST is used to collect data after submitting an HTML
form.

40. What will be the output of the following PHP program?

<?php

$php = array("Array", "Function", "Strings", "File");

echo pos($php);

?>

a) Function

b) File

c) Strings

d) Array
View Answer

Answer: d

Explanation: The pos() function returns the value of the current element in an array, and since no
operation has been done, the current element is the first element.

41. If $a = 12 what will be returned when ($a == 12) ? 5 : 1 is executed?

a) 1

b) 5

c) 12

d) Error

View Answer

Answer: b

Explanation: ?: is known as ternary operator. If condition is true then the part just after the ? is executed
else the part after : .

You might also like