Hướng dẫn php mcq

1) PHP stands for -

  1. Hypertext Preprocessor
  2. Pretext Hypertext Preprocessor
  3. Personal Home Processor
  4. 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?

  1. Drek Kolkevi
  2. List Barely
  3. Rasmus Lerdrof
  4. 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 -

  1. ! (Exclamation)
  2. $ (Dollar)
  3. & (Ampersand)
  4. # (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?

  1. .php
  2. .hphp
  3. .xml
  4. .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?

  1. Extern
  2. Local
  3. Static
  4. 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?

  1. & …… &
  2. // ……
  3. /* …… */
  4. 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?

  1. echo
  2. write
  3. print
  4. 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?

  1. The strlen() function returns the type of string
  2. The strlen() function returns the length of string
  3. The strlen() function returns the value of string
  4. 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?

  1. + (plus)
  2. * (Asterisk)
  3. . (dot)
  4. 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?

  1. Inbuilt constants
  2. User-defined constants
  3. Magic constants
  4. 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?

  1. The strpos() function is used to search for the spaces in a string
  2. The strpos() function is used to search for a number in a string
  3. The strpos() function is used to search for a character/text in a string
  4. 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?

  1. PHP extension and application repository
  2. PHP enhancement and application reduce
  3. PHP event and application repository
  4. 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?

  1. Create myFunction()
  2. New_function myFunction()
  3. function myFunction()
  4. 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?

  1. id()
  2. mdid()
  3. uniqueid()
  4. 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?

  1. $variable name = value;
  2. $variable_name = value;
  3. $variable_name = value
  4. $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?

  1. The strcmp() function is used to compare the strings excluding case
  2. The strcmp() function is used to compare the uppercase strings
  3. The strcmp() function is used to compare the lowercase strings
  4. 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?

  1. The fopen() function is used to open folders in PHP
  2. The fopen() function is used to open remote server
  3. The fopen() function is used to open files in PHP
  4. 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?

  1. The isset() function is used to check whether variable is set or not
  2. The isset() function is used to check whether the variable is free or not
  3. The isset() function is used to check whether the variable is string or not
  4. 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?

  1. The sprintf() function is used to print the output of program
  2. The sprintf() function is used to send output to variable
  3. Both of the above
  4. 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?

  1. fopen("sample.txt", "r");
  2. fopen("sample.txt", "r+");
  3. fopen("sample.txt", "read");
  4. 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?

  1. php_info()
  2. phpinfo()
  3. info()
  4. 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?

  1. glob()
  2. fold()
  3. file()
  4. 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?

  1. createcookie()
  2. makecookie()
  3. setcookie()
  4. 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?

  1. val()
  2. asc()
  3. ascii()
  4. 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?

  1. delete()
  2. unset()
  3. unlink()
  4. 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?

  1. sort()
  2. asrot()
  3. dsort()
  4. 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?

  1. Notepad++
  2. Notepad
  3. Adobe Dreamweaver
  4. 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?

  1. . (dot)
  2. ; (semicolon)
  3. ! (exclamation)
  4. / (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?

  1. showtype()
  2. gettype()
  3. settype()
  4. 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 -

  1. Double Quotes
  2. Single Quotes
  3. Both (a) and (b)
  4. 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?

  1. $newVar
  2. $new_Var
  3. $new-var
  4. 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?

  1. $season = array["summer" , "winter" , "spring" , "autumn"];
  2. $season = array("summer" , "winter" , "spring" , "autumn");
  3. $season = "summer" , "winter" , "spring" , "autumn";
  4. 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?

  1. array_push()
  2. inend_array()
  3. into_array()
  4. 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?

  1. toUpper($var)
  2. ucwords($var)
  3. ucword($var)
  4. 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?

  1. "Hello World";
  2. write("Hello World");
  3. echo "Hello World";
  4. 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?

  1. compress()
  2. zip_compress()
  3. gzcompress()
  4. 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?

  1. Standard PHP Library
  2. Simple PHP Library
  3. Simple PHP List
  4. 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?

  1. upper()
  2. uppercase()
  3. struppercase()
  4. 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 -

  1. implode()
  2. concat()
  3. explode()
  4. 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?

  1. filetime()
  2. fileatime()
  3. filectime()
  4. 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?

  1. filegets()
  2. fget()
  3. fgets()
  4. 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?

  1. strops()
  2. strrpos()
  3. strtr()
  4. 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. Welcome to the javaTpoint.com
  2. Welcome, to, the, javaTpoint.com
  3. com
  4. 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?

  1. Welcome to the javaTpoint.com
  2. Welcome, to, the, javaTpoint.com
  3. com
  4. 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?

  1. HelloWorld
  2. Hello, World
  3. Hello World
  4. 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. HelloWorld
  2. "$var1$var2"
  3. Hello World
  4. 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. Hi How are you
  2. How are you
  3. Hi
  4. 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. 0
  2. 1
  3. 01
  4. 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. Welcome to the javaTpoint.com
  2. welcome to the javaTpoint.com
  3. Welcome To The JavaTpoint.com
  4. 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. 2015
  2. 2020
  3. 1515
  4. 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. Hii everyone
  2. Hello World
  3. Hello World Hii everyone
  4. 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. welcome To The JavaTpoint.com
  2. Welcome To The JavaTpoint.com
  3. Welcome To The javaTpoint.com
  4. 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. HelloWorld
  2. This is javaTpoint.com
  3. Hello
  4. 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. Welcome to the javaTpoint.com
  2. Wulcomu to tha juvuTpoint.com
  3. Walcoma to the jeveTpoint.com
  4. 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. Hello World
  2. Infinite loop
  3. no output
  4. 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. dlroW olleH
  2. olleH dlroW
  3. Hello dlroW
  4. 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?

  1. diff_array
  2. array_diff
  3. arrays_diff
  4. 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. 160
  2. 1652
  3. 80
  4. 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?

  1. str_to_time()
  2. strtodate()
  3. strtotime()
  4. 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?

  1. date_sunrise()
  2. date-sunrise()
  3. sunrise()
  4. 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.