100% found this document useful (1 vote)
2K views

PHP MCQ

This document contains 20 multiple choice questions about PHP. It tests knowledge of basic PHP concepts like what PHP stands for, who created it, how variables are declared, common functions and their uses, and more. Each question is followed by a short description or explanation of the answer. The questions cover a wide range of fundamental PHP topics to assess overall understanding of the language.

Uploaded by

I'm Noob
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
2K views

PHP MCQ

This document contains 20 multiple choice questions about PHP. It tests knowledge of basic PHP concepts like what PHP stands for, who created it, how variables are declared, common functions and their uses, and more. Each question is followed by a short description or explanation of the answer. The questions cover a wide range of fundamental PHP topics to assess overall understanding of the language.

Uploaded by

I'm Noob
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 37

PHP MCQ

1) PHP stands for -


a. Hypertext Preprocessor
b. Pretext Hypertext Preprocessor
c. Personal Home Processor
d. None of the above
Answer: (a) Hypertext Preprocessor
Description: PHP stands for Hypertext Preprocessor. PHP is an
open-source, interpreted, and object-oriented scripting language
that can be executed on the server-side. It is well suited for web
development.

2) Who is known as the father of PHP?


a. Drek Kolkevi
b. List Barely
c. Rasmus Lerdrof
d. None of the above
Answer: (c) Rasmus Lerdrof
Description: PHP was created by Rasmus Lerdorf in 1994 but
appeared in the market in 1995. PHP 7.4.0 is the latest version
of PHP, which was released on 28 November.
3) Variable name in PHP starts with -
a. ! (Exclamation)
b. $ (Dollar)
c. & (Ampersand)
d. # (Hash)
Answer: (b) $ (Dollar)
Description:
In PHP, a variable is declared using a $ (Dollar) sign followed
by the variable name.
As PHP is a loosely typed language, so we do not need to
declare the data types of the variables. It automatically analyzes
the values and makes conversions to its correct datatype.
Variables in PHP are case-sensitive, as an instance the variables
$name and $NAME both are treated as different variables.

4) Which of the following is the default file extension of PHP?


a. .php
b. .hphp
c. .xml
d. .html
Answer: (a) .php
Description: Generally, a PHP file contains HTML tags and
some PHP scripting code. It is very easy to create a simple PHP
example. The programs in php are saved with the .php
extension.

5) Which of the following is not a variable scope in PHP?


a. Extern
b. Local
c. Static
d. Global
Answer: (a) Extern
Description:
The scope of a variable is defined as its range in the program
under which it can be accessed. PHP has three types of variable
scopes:
o Local variable
o Global variable
o Static variable

6) Which of the following is correct to add a comment in php?


a. & …… &
b. // ……
c. /* …… */
d. Both (b) and (c)
Answer: (d) Both (b) and (c)
Description:
PHP comments can be used to describe any line of code so that
other developers can understand the code easily. PHP supports
single-line and multi-line comments. There are two ways to use
single-line comments in PHP.
o // (C++ style single line comment)
o # (Unix Shell style single line comment)

For multiple lines comment in PHP, we need to enclose all lines


within /* …..*/.

7) Which of the following is used to display the output in PHP?


a. echo
b. write
c. print
d. Both (a) and (c)

Answer: (d) Both (a) and (c)


Description: In PHP, both echo and print are language
constructs. Both echo and print statements can be used with or
without parentheses. We can use these statements to output
variables or strings. The echo is a statement, which is used to
display the output. The print is a statement used as an alternative
to echo at many times to display the output.

8) Which of the following is the use of strlen() function in PHP?


a. The strlen() function returns the type of string
b. The strlen() function returns the length of string
c. The strlen() function returns the value of string
d. The strlen() function returns both value and type of string

Answer: (b) The strlen() function returns the length of string


Description: The strlen() function is predefined function of PHP.
It is used to find the length of string or returns the length of a
string including all the whitespaces and special character.

9) Which of the following is used for concatenation in PHP?


a. + (plus)
b. * (Asterisk)
c. . (dot)
d. append()

Answer: (c) . (dot)
Description: In PHP, the . (dot) operator is used for
concatenation. Suppose there are two variables $a and $b, so the
statement $a . $b will concatenate both $a and $b.

10) Which of the following starts with __ (double underscore) in


PHP?
a. Inbuilt constants
b. User-defined constants
c. Magic constants
d. Default constants
Answer: (c) Magic constants
Description: Magic constants are the predefined constants in
PHP which get changed on the basis of their use. They start with
a double underscore (__) and end with double underscore. They
are similar to other predefined constants, but as they change
their values with the context, they are called magic constants.

11) Which of the following is the use of strpos() function in


PHP?
a. The strpos() function is used to search for the spaces in a
string
b. The strpos() function is used to search for a number in a
string
c. The strpos() function is used to search for a character/text
in a string
d. The strpos() function is used to search for a capitalize
character in a string

Answer: (c) The strpos() function is used to search for a


character/text in a string
Description: The strops() is in-built function of PHP. It is used
to find the position of the first occurrence of a string inside
another string or substring in a string.

12) What does PEAR stands for?


a. PHP extension and application repository
b. PHP enhancement and application reduce
c. PHP event and application repository
d. None of the above

Answer: (a) PHP extension and application repository


Description: PEAR is a framework and repository for reusable
PHP components. PEAR stands for PHP Extension and
Application Repository. It contains all types of PHP code
snippets and libraries.

13) Which of the following is the correct way to create a


function in PHP?
a. Create myFunction()
b. New_function myFunction()
c. function myFunction()
d. None of the above

Answer: (c) function myFunction()


Description: We can declare and call user-defined functions
easily. The syntax to declare user-defined functions is given
below -
function functionname(){
//code to be executed
}

14) Which of the following PHP function is used to generate


unique id?
a. id()
b. mdid()
c. uniqueid()
d. None of the above

Answer: (c) uniqueid()
Description: We can declare and call user-defined functions
easily. The syntax to declare user-defined functions is given
below -
function functionname(){
//code to be executed
}

15) Which of the following is the correct way of defining a


variable in PHP?
a. $variable name = value;
b. $variable_name = value;
c. $variable_name = value
d. $variable name as value;

Answer: (b) $variable_name = value;


Description:
In PHP, a variable is declared using a $ sign followed by the
variable name. A PHP variable name cannot contain spaces.
Syntax of declaring a variable in PHP is given below:
$variable_name=value;

16) Which of the following is the correct use of the strcmp()


function in PHP?
a. The strcmp() function is used to compare the strings
excluding case
b. The strcmp() function is used to compare the uppercase
strings
c. The strcmp() function is used to compare the lowercase
strings
d. The strcmp() function is used to compare the strings
including case

Answer: (d) The strcmp() function is used to compare the strings


including case
Description: The strcmp() is a string comparison function in
PHP. It is a built-in function of PHP, which is case sensitive,
which means it treats capital and the small case separately. It is
used to compare two strings from each other. This function
compares two strings and tells whether a string is greater, less,
or equal to another string.

17) What is the use of fopen() function in PHP?


a. The fopen() function is used to open folders in PHP
b. The fopen() function is used to open remote server
c. The fopen() function is used to open files in PHP
d. None of the above

Answer: (c) The fopen() function is used to open files in PHP


Description: PHP fopen() function is used to open file or URL
and returns resource. The fopen() function accepts two
arguments: $filename and $mode. The $filename represents the
file to be opended and $mode represents the file mode for
example read-only, read-write, write-only etc.

18) What is the use of isset() function in PHP?


a. The isset() function is used to check whether variable is set
or not
b. The isset() function is used to check whether the variable is
free or not
c. The isset() function is used to check whether the variable is
string or not
d. None of the above

Answer: (a) The isset() function is used to check whether a


variable is set or not
Description: The isset() function is a built-in function of PHP,
which is used to determine that a variable is set or not. If a
variable is considered set, it means the variable is declared and
has a different value from the NULL. In short, it checks that the
variable is declared and not null. This function returns true if the
variable is not null, otherwise it returns false.

19) What is the use of sprintf() function in PHP?


a. The sprintf() function is used to print the output of program
b. The sprintf() function is used to send output to variable
c. Both of the above
d. None of the above

Answer: (b) The sprintf() function is used to send output to


variable
Description: The sprintf() is an in-built function of PHP which
writes a formatted string to a variable. It returns a formatted
string. The sprintf() function is similar to the printf() function,
but the only difference between both of them is that sprint()
saves the output into a string instead of displaying the formatted
message on browser like printf() function.

20) Which of the following is the correct way to open the file
"sample.txt" as readable?
a. fopen("sample.txt", "r");
b. fopen("sample.txt", "r+");
c. fopen("sample.txt", "read");
d. fopen("sample.txt");

Answer: (a) fopen("sample.txt", "r");


Description: The fopen() function accepts two arguments:
$filename and $mode. The $filename represents the file to be
opended and $mode represents the file mode for example, read-
only, read-write, write-only etc. The "r" mode opens the file in
read-only mode. It places the file pointer at the beginning of the
file.

21) Which of the following function displays the information


about PHP and its configuration?
a. php_info()
b. phpinfo()
c. info()
d. None of the above

Answer: (b) phpinfo()
Description: The phpinfo() function gives us the information
about the PHP configuration and installation in our system. The
phpinfo() function is generally used to check the configuration
settings.

22) Which of the following function is used to find files in PHP?


a. glob()
b. fold()
c. file()
d. None of the above

Answer: (a) glob()
Description: The glob() function in PHP is used to return an
array of filenames or directories that matches a specified pattern.

23) Which of the following function is used to set cookie in


PHP?
a. createcookie()
b. makecookie()
c. setcookie()
d. None of the above

Answer: (c) setcookie()
Description: PHP cookie is a small piece of information that is
stored at client browser. It is used to recognize the user. PHP
setcookie() function is used to set cookie with HTTP response.
Once the cookie is set, you can access it by the $_COOKIE
superglobal variable.

24) Which of the following function is used to get the ASCII


value of a character in PHP?
a. val()
b. asc()
c. ascii()
d. chr()
Answer: (d) chr()
Description: The PHP chr() function is used to generate a single
byte string from a number. In other words, we can say that it
returns a character from a specified ASCII value.

25) Which of the following function is used to unset a variable


in PHP?
a. delete()
b. unset()
c. unlink()
d. None of the above

Answer: (b) unset()
Description: The unset() function is a predefined variable
handling function of PHP, which is used to unset a specified
variable. In other words, "the unset() function destroys the
variables".

26) Which of the following function is used to sort an array in


descending order?
a. sort()
b. asrot()
c. dsort()
d. rsort()
Answer: (d) rsort()
Description: The PHP rsort( ) function is used to sort an array in
descending order. This function introduced in PHP 4.0.

27) Which of the following is/are the code editors in PHP?


a. Notepad++
b. Notepad
c. Adobe Dreamweaver
d. All of the above

Answer: (d) All of the above


Description: PHP programs can be written on any editor, such as
- Notepad, Notepad++, Dreamweaver, etc. These programs save
with .php extension, i.e., filename.php inside the htdocs folder.

28) Which of the following is used to end a statement in PHP?


a. . (dot)
b. ; (semicolon)
c. ! (exclamation)
d. / (slash)
Answer: (b) ; (semicolon)
Description: PHP statements end with a semicolon (;).

29) Which of the following function in PHP can be used to test


the type of any variable?
a. showtype()
b. gettype()
c. settype()
d. None of the above

Answer: (b) gettype()
Description: The gettype() function is an inbuilt function in
PHP, and as its name implies, it returns the type of a variable.
The gettype() function in PHP is used to check the type of
existing variable.

30) String values in PHP must be enclosed within -


a. Double Quotes
b. Single Quotes
c. Both (a) and (b)
d. None of the above
Answer: (c) Both (a) and (b)
Description:
There are 4 ways to specify a string literal in PHP.
o single quoted
o double quoted
o heredoc syntax
o newdoc syntax (since PHP 5.3)

We can create a string in PHP by enclosing the text in a single-


quote. It is the easiest way to specify string in PHP. In PHP, we
can specify string through enclosing text within double quote
also. But escape sequences and variables will be interpreted
using double quote PHP strings.

31) Which of the following variable name is invalid?


a. $newVar
b. $new_Var
c. $new-var
d. All of the above

Answer: (c) $new-var
Description: In PHP, a variable is declared using a $ sign
followed by the variable name. A variable name must start with
a letter or underscore (_) character.
A PHP variable name cannot contain spaces.

32) Which of the following is the correct way to create an array


in PHP?
a. $season = array["summer" , "winter" , "spring" ,
"autumn"];
b. $season = array("summer" , "winter" , "spring" ,
"autumn");
c. $season = "summer" , "winter" , "spring" , "autumn";
d. All of the above

Answer: (b) $season = array("summer" , "winter" , "spring" ,


"autumn");
Description: None.

33) Which of the following is a built-in function in PHP that


adds a value to the end of an array?
a. array_push()
b. inend_array()
c. into_array()
d. None of the above

Answer: (a) array_push()
Description: The array_push() is a built-in function of PHP. This
function helps the users to add the elements at the end of the
array. It allows to insert any number of elements in an array.
Even you can add a string as well as numeric values.

34) Which of the following function in PHP returns a text in title


case from a variable?
a. toUpper($var)
b. ucwords($var)
c. ucword($var)
d. All of the above

Answer: (b) ucwords($var)
Description: The ucwords() is an in-built function of PHP,
which is used to convert the first character of each word to
uppercase in a string. It takes a string as an input and converts
the first character of each word of the string to uppercase. The
other characters of the string remain the same.

35) Which of the following is the correct way to print "Hello


World" in PHP?
a. "Hello World";
b. write("Hello World");
c. echo "Hello World";
d. None of the above

Answer: (c) echo "Hello World";


Description: PHP echo is a language construct, not a function.
Therefore, you don't need to use parenthesis with it. The correct
way to print "Hello World" in PHP is:
echo "Hello World";

36) Which of the following function is used to compress a string


in PHP?
a. compress()
b. zip_compress()
c. gzcompress()
d. zip()
Answer: (c) gzcompress()
Description: The gzcompress() in PHP compresses the given
string using the ZLIB format.

37) What does SPL stands for in PHP?


a. Standard PHP Library
b. Simple PHP Library
c. Simple PHP List
d. None of the above
Answer: (a) Standard PHP Library
Description: In PHP, SPL stands for "Standard PHP Library".

38) Which of the following function converts a string to all


uppercase?
a. upper()
b. uppercase()
c. struppercase()
d. strtoupper()

Answer: (d) strtoupper()
Description: The strtoupper() is one of the most popular
functions of PHP, which is widely used to convert the string into
uppercase. It takes a string as a parameter and converts all
lowercase English character of that string to uppercase.

39) The function in PHP that can be used to concatenate array


elements to form a single delimited string is -
a. implode()
b. concat()
c. explode()
d. concatenate()
Answer: (a) implode()
Description: PHP implode() is a string function, which joins the
array elements in a string. The implode() function works the
same as the join() function and returns a string created from the
elements of the array. Basically, this function joins all elements
of an array in one string.

40) Which PHP function determines the last access time of a


file?
a. filetime()
b. fileatime()
c. filectime()
d. None of the above

Answer: (b) fileatime()
Description: The fileatime() function in PHP is used to return
the last access time of a file as a UNIX timestamp. On failure,
the fileatime() returns false.

41) Which PHP function is capable to read specific number of


characters from a file?
a. filegets()
b. fget()
c. fgets()
d. None of the above

Answer: (c) fgets()
Description: The PHP fgets() function is used to read a single
line from the file.

42) Which PHP function is used to find the position of the last
occurrence of a substring inside another string?
a. strops()
b. strrpos()
c. strtr()
d. None of the above

Answer: (b) strrpos()
Description: The strrpos() is an in-built function of PHP which
is used to find the position of the last occurrence of a substring
inside another string. It is a case-sensitive function of PHP,
which means it treats uppercase and lowercase characters
differently.

43) What will be the output of the following program?


1. <?php  
2. echo "Welcome" . "to" . "the" . "javaTpoint.com";  
3. ?>  
a. Welcome to the javaTpoint.com
b. Welcome, to, the, javaTpoint.com
c. com
d. Error
Hide Answer Workspace
Answer: (c) WelcometothejavaTpoint.com
Description: In echo statement the . (dot) operator is used to join
strings.

44) What will be the output of the following program?


1. <?php  
2. echo "Welcome" , "to" , "the" , "javaTpoint.com";  
3. ?>  
a. Welcome to the javaTpoint.com
b. Welcome, to, the, javaTpoint.com
c. com
d. Error
Hide Answer Workspace
Answer: (c) WelcometothejavaTpoint.com
Description: We can pass multiple strings separated by a comma
(,) in echo. In echo statement the , (comma) operator is also used
to join strings.

45) What will be the output of the following program?


1. <?php  
2. $var1 = "Hello";  
3. $var2 = "World";  
4. echo $var1, $var2;  
5. ?>  
a. HelloWorld
b. Hello, World
c. Hello World
d. None of the above
Answer: (a) HelloWorld
Description: We can pass multiple strings separated by a comma
(,) in echo. In echo statement the , (comma) operator is also used
to join strings.

46) What will be the output of the following program?


1. <?php  
2. $var1 = "Hello";  
3. $var2 = "World";  
4. echo "$var1$var2";  
5. ?>  
a. HelloWorld
b. "$var1$var2"
c. Hello World
d. None of the above

Answer: (a) HelloWorld
Description: In the given echo statement, both variables are
inside the double quotes, so the variable's value will be
substituted and printed on the screen.

47) What will be the output of the following program?


1. <?php  
2. $a;  
3. if ($a)  
4. {  
5. echo "hi";  
6. }  
7. else  
8. {  
9. echo "How are you";  
10. }  
11. ?>  
a. Hi How are you
b. How are you
c. Hi
d. None of the above
Answer: (b) How are you
Description: In the given program, the variable $a is
uninitialized. So, the if condition will fail, and the echo
statement of else block will be printed on screen.

48) What will be the output of the following program?


1. <?php  
2. $a = 0;  
3. while ($a++)  
4. {  
5. echo "$a";  
6. }  
7. echo $a;  
8. ?>  
a. 0
b. 1
c. 01
d. None of the above

Answer: (b) 1
Description: In the given program there is post-increment. So,
first, the condition is checked, therefore at first, the value 0 is
tested and does not enter the loop. So, the program will print 1.
49) What will be the output of the following program?
1. <?php  
2. echo ucwords("welcome to the javaTpoint.com");  
3. ?>  
a. Welcome to the javaTpoint.com
b. welcome to the javaTpoint.com
c. Welcome To The JavaTpoint.com
d. Welcome to the JavaTpoint.com

Answer: (c) Welcome To The JavaTpoint.com


Description: The ucwords() is an in-built function of PHP,
which is used to convert the first character of each word to
uppercase in a string. It takes a string as an input and converts
the first character of each word of the string to uppercase. The
other characters of the string remain the same.

50) What will be the output of the following program?


1. <?php  
2. $a = 15;  
3. function show()  
4. {  
5. $a = 20;  
6. echo "$a";  
7. }  
8. show();  
9. echo "$a";  
10. ?>  
a. 2015
b. 2020
c. 1515
d. 0

Answer: (a) 2015
Description: In the given program, first the show() function is
called, and the variable $a is initialized to 20, and 20 is printed
on the screen. Later, the global variable $a is initialized to 15,
and the value 15 is printed on the screen. So the output of the
above program will be 2015.

51) What will be the output of the following program?


1. <?php  
2. $x = 15;  
3. $y = 20;  
4. if($x < ++$x || $y < ++$y)  
5. {  
6. echo "Hello World";  
7. }  
8. else   
9. {  
10. echo "Hii everyone";  
11. }  
12. ?>  
a. Hii everyone
b. Hello World
c. Hello World Hii everyone
d. None of the above

Answer: (a) Hii everyone


Description: The precedence of ++ is higher than <, so first
increment happens and then comparison. But when comparison
happens, both OR operator condition will return false.
Therefore, the if loop will not run, and the echo statement in the
else block will be printed.

52) What will be the output of the following program?


1. <?php  
2. echo lcfirst("Welcome To The JavaTpoint.com");  
3. ?>  
a. welcome To The JavaTpoint.com
b. Welcome To The JavaTpoint.com
c. Welcome To The javaTpoint.com
d. None of the above

Answer: (a) welcome To The JavaTpoint.com


Description: The lcfirst() is in-built PHP string function. It is
used to convert the first character of a string to lowercase. In
other words, we can say that it makes a string's first character
lowercase. It returns the converted string.

53) What will be the output of the following program?


1. <?php  
2. $a = "1";  
3. switch($a)  
4. {  
5. case 1:  
6. echo "Hello";  
7. case 2:  
8. echo "World";  
9. default:  
10. echo "This is javaTpoint.com";  
11. }  
12. ?>  
a. HelloWorld
b. This is javaTpoint.com
c. Hello
d. HelloWorldThis is javaTpoint.com

Answer: (d) HelloWorldThis is javaTpoint.com


Description: In the given program, there is no break provided, so
all cases will get executed. Given variable $a is initialized with
1, so case 1 will execute first and then case 2 and at last default
will be executed.

54) What will be the output of the following program?


1. <?php  
2. echo strtr("Walcoma to the juvuTpoint.com", "au", "ea");  
3. ?>  
a. Welcome to the javaTpoint.com
b. Wulcomu to tha juvuTpoint.com
c. Walcoma to the jeveTpoint.com
d. None of the above

Answer: (a) Welcome to the javaTpoint.com


Description: The strtr() is an in-built function of PHP, which is
used to replace a substring inside the other string. It provides the
facility to change the particular word in a string. The strtr()
function translates characters or replaces the substrings. It is a
case-sensitive function.

55) What will be the output of the following program?


1. <?php  
2. while()  
3. {  
4. echo "Hello World";  
5. }  
6. ?>  
a. Hello World
b. Infinite loop
c. no output
d. Error

Answer: (d) Error
Description: The while loop is also called an Entry control loop
because the condition is checked before entering the loop body.
This means that first, the condition is checked. If the condition is
true, the block of code will be executed. So, a while loop cannot
be defined without a condition.

56) What will be the output of the following program?


1. <?php    
2.   $var1="Hello World";    
3.   echo strrev("$var1");     
4. ?>     
a. dlroW olleH
b. olleH dlroW
c. Hello dlroW
d. None of the above
Answer: (a) dlroW olleH
Description: The strrev() function is a predefined function of
PHP. As its name implies, it is used to reverse a string. It is one
of the most basic string operations which are used by
programmers and developers.

57) Which of the following function is used to compute the


difference between two arrays in PHP?
a. diff_array
b. array_diff
c. arrays_diff
d. diff_arrays

Answer: (b) array_diff
Description: The array_diff() function compares two or more
arrays and returns an array with the keys and values from the
first array, only if the value is not present in any of the other
arrays.

58) What will be the output of the following program?


1. <?php    
2. $a = array(16, 5, 2);  
3. echo array_product($a);  
4. ?>       
a. 160
b. 1652
c. 80
d. 32
Hide Answer Workspace
Answer: (a) 160
Description: The array_product() function calculates the product
of an array. It is an inbuilt function of PHP.

59) Which PHP function converts an English text datetime into a


Unix timestamp?
a. str_to_time()
b. strtodate()
c. strtotime()
d. None of the above

Answer: (c) strtotime()
Description: The PHP strtotime() function is used to convert an
English text datetime into a Unix timestamp.

60) Which of the following function in PHP returns the time of


sunrise of a particular day and location?
a. date_sunrise()
b. date-sunrise()
c. sunrise()
d. None of the above

Answer: (a) date_sunrise()
Description: This date_sunrise() function is used to get the
timing of sunrise of a particular day and location. This function
returns the sunrise time of different locations.

You might also like