PHP
PHP
1. What is PHP?
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.
a) Drek Kolkevi
b) Rasmus Lerdorf
c) Willam Makepiece
d) List Barely
View Answer
Answer: b
View Answer
a) <?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) /* */
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
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.
<?php
$x = 10;
$y = 20;
?>
a) no output
b) Welcome to Sanfoundry
d) error
View Answer
Answer: c
Output:
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.
<?php
$fruits = array ("apple", "orange", array ("pear", "mango"),"banana");
?>
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
<?php
function multi($num)
if ($num == 3)
if ($num == 7)
if ($num == 8)
if ($num == 19)
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.
advertisement
<?php
class Example
public $name;
function Sample()
?>
a) function sample()
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.
<?php
echo $GREETING;
?>
a) $GREETING
b) no output
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
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
17. Which of the following web servers are required to run the PHP script?
b) IIS
c) XAMPP
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]";
?>
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.
<?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;
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 = strip_tags($input,"<i></i>");
echo $input;
?>
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'].
b) i) and iv)
d) i) and ii)
View Answer
Answer: b
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;
printf("PHP Quiz");
printf("PHP MCQ");
printf("PHP Program");
?>
a)
PHP Quiz
PHP MCQ
PHP Program
b) PHP Quiz
c) No output
d) PHP MCQ
View Answer
Answer: b
a) Only iv)
b) i) and ii)
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.
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.
a) PHP 6
b) PHP 4
c) PHP 5
d) PHP 5.3
View Answer
Answer: c
<?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.
$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.
<?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
<?php
define("VAR_NAME","test");
${VAR_NAME} = "value";
echo VAR_NAME;
echo ${VAR_NAME};
?>
a) testtest
b) testvalue
d) test
View Answer
Answer: b
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.
<?php
$i = 5;
while (--$i > 0 && ++$i)
print $i;
?>
a) 555555555…infinitely
b) 54321
c) error
d) 5
View Answer
Answer: a
<?php
function constant()
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.
<?php
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.
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 : .