PHP Multiple Choice Questions
PHP MCQ
1) PHP stands for -
Hypertext Preprocessor
Pretext Hypertext Preprocessor
Personal Home Processor
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?
Drek Kolkevi
List Barely
Rasmus Lerdrof
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 -
! (Exclamation)
$ (Dollar)
& (Ampersand)
# (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?
.php
.hphp
.xml
.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?
Extern
Local
Static
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:
Local variable
Global variable
Static variable
6) Which of the following is correct to add a comment in php?
& …… &
// ……
/* …… */
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.
// (C++ style single line comment)
# (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?
echo
write
print
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?
The strlen() function returns the type of string
The strlen() function returns the length of string
The strlen() function returns the value of string
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?
+ (plus)
* (Asterisk)
. (dot)
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?
Inbuilt constants
User-defined constants
Magic constants
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?
The strpos() function is used to search for the spaces in a string
The strpos() function is used to search for a number in a string
The strpos() function is used to search for a character/text in a string
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?
PHP extension and application repository
PHP enhancement and application reduce
PHP event and application repository
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?
Create myFunction()
New_function myFunction()
function myFunction()
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?
id()
mdid()
uniqueid()
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?
$variable name = value;
$variable_name = value;
$variable_name = value
$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?
The strcmp() function is used to compare the strings excluding case
The strcmp() function is used to compare the uppercase strings
The strcmp() function is used to compare the lowercase strings
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?
The fopen() function is used to open folders in PHP
The fopen() function is used to open remote server
The fopen() function is used to open files in PHP
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?
The isset() function is used to check whether variable is set or not
The isset() function is used to check whether the variable is free or not
The isset() function is used to check whether the variable is string or not
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?
The sprintf() function is used to print the output of program
The sprintf() function is used to send output to variable
Both of the above
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?
fopen("sample.txt", "r");
fopen("sample.txt", "r+");
fopen("sample.txt", "read");
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?
php_info()
phpinfo()
info()
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?
glob()
fold()
file()
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?
createcookie()
makecookie()
setcookie()
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?
val()
asc()
ascii()
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?
delete()
unset()
unlink()
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?
sort()
asrot()
dsort()
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?
Notepad++
Notepad
Adobe Dreamweaver
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?
. (dot)
; (semicolon)
! (exclamation)
/ (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?
showtype()
gettype()
settype()
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 -
Double Quotes
Single Quotes
Both (a) and (b)
None of the above
Answer: (c) Both (a) and (b)
Description:
There are 4 ways to specify a string literal in PHP.
single quoted
double quoted
heredoc syntax
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?
$newVar
$new_Var
$new-var
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?
$season = array["summer" , "winter" , "spring" , "autumn"];
$season = array("summer" , "winter" , "spring" , "autumn");
$season = "summer" , "winter" , "spring" , "autumn";
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?
array_push()
inend_array()
into_array()
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?
toUpper($var)
ucwords($var)
ucword($var)
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?
"Hello World";
write("Hello World");
echo "Hello World";
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?
compress()
zip_compress()
gzcompress()
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?
Standard PHP Library
Simple PHP Library
Simple PHP List
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?
upper()
uppercase()
struppercase()
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 -
implode()
concat()
explode()
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?
filetime()
fileatime()
filectime()
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?
filegets()
fget()
fgets()
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?
strops()
strrpos()
strtr()
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?
<?php
echo "Welcome" . "to" . "the" . "javaTpoint.com";
?> <?php echo "Welcome" . "to" . "the" . "javaTpoint.com"; ?>
Welcome to the javaTpoint.com
Welcome, to, the, javaTpoint.com
com
Error
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?
<?php
echo "Welcome" , "to" , "the" , "javaTpoint.com";
?> <?php echo "Welcome" , "to" , "the" , "javaTpoint.com"; ?>
Welcome to the javaTpoint.com
Welcome, to, the, javaTpoint.com
com
Error
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?
<?php
$var1 = "Hello";
$var2 = "World";
echo $var1, $var2;
?> <?php $var1 = "Hello"; $var2 = "World"; echo $var1, $var2; ?>
HelloWorld
Hello, World
Hello World
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?
<?php
$var1 = "Hello";
$var2 = "World";
echo "$var1$var2";
?> <?php $var1 = "Hello"; $var2 = "World"; echo "$var1$var2"; ?>
HelloWorld
"$var1$var2"
Hello World
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?
<?php
$a;
if ($a)
{
echo "hi";
}
else
{
echo "How are you";
}
?> <?php $a; if ($a) { echo "hi"; } else { echo "How are you"; } ?>
Hi How are you
How are you
Hi
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?
<?php
$a = 0;
while ($a++)
{
echo "$a";
}
echo $a;
?> <?php $a = 0; while ($a++) { echo "$a"; } echo $a; ?>
0
1
01
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?
<?php
echo ucwords("welcome to the javaTpoint.com");
?> <?php echo ucwords("welcome to the javaTpoint.com"); ?>
Welcome to the javaTpoint.com
welcome to the javaTpoint.com
Welcome To The JavaTpoint.com
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?
<?php
$a = 15;
function show()
{
$a = 20;
echo "$a";
}
show();
echo "$a";
?> <?php $a = 15; function show() { $a = 20; echo "$a"; } show(); echo "$a"; ?>
2015
2020
1515
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?
<?php
$x = 15;
$y = 20;
if($x < ++$x || $y < ++$y)
{
echo "Hello World";
}
else
{
echo "Hii everyone";
}
?> <?php $x = 15; $y = 20; if($x < ++$x || $y < ++$y) { echo "Hello World"; } else { echo "Hii everyone"; } ?>
Hii everyone
Hello World
Hello World Hii everyone
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?
<?php
echo lcfirst("Welcome To The JavaTpoint.com");
?> <?php echo lcfirst("Welcome To The JavaTpoint.com"); ?>
welcome To The JavaTpoint.com
Welcome To The JavaTpoint.com
Welcome To The javaTpoint.com
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?
<?php
$a = "1";
switch($a)
{
case 1:
echo "Hello";
case 2:
echo "World";
default:
echo "This is javaTpoint.com";
}
?> <?php $a = "1"; switch($a) { case 1: echo "Hello"; case 2: echo "World"; default: echo "This is javaTpoint.com"; } ?>
HelloWorld
This is javaTpoint.com
Hello
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?
<?php
echo strtr("Walcoma to the juvuTpoint.com", "au", "ea");
?> <?php echo strtr("Walcoma to the juvuTpoint.com", "au", "ea"); ?>
Welcome to the javaTpoint.com
Wulcomu to tha juvuTpoint.com
Walcoma to the jeveTpoint.com
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?
<?php
while()
{
echo "Hello World";
}
?> <?php while() { echo "Hello World"; } ?>
Hello World
Infinite loop
no output
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?
<?php
$var1="Hello World";
echo strrev("$var1");
?> <?php $var1="Hello World"; echo strrev("$var1"); ?>
dlroW olleH
olleH dlroW
Hello dlroW
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?
diff_array
array_diff
arrays_diff
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?
<?php
$a = array(16, 5, 2);
echo array_product($a);
?> <?php $a = array(16, 5, 2); echo array_product($a); ?>
160
1652
80
32
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?
str_to_time()
strtodate()
strtotime()
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?
date_sunrise()
date-sunrise()
sunrise()
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.
Comments
Post a Comment