PHP MCQ Questions
PHP MCQ Questions
PHP MCQ Questions
This set of PHP Interview Questions and Answers focuses on “Basics – 2”.
1. Which is the right way of declaring a variable in PHP?
i) $3hello
ii) $_hello
iii) $this
iv) $This
a) Only ii)
b) Only iii)
c) ii), iii) and iv)
d) ii) and iv)
View Answer
Answer: d
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 define variable name.
2. What will be the output of the following code?
<?php
$foo = 'Bob';
$bar = &$foo;
$bar = "My name is $bar";
echo $bar;
echo $foo;
?>
a) Error
b) My name is BobBob
c) My name is BobMy name is Bob
d) My name is Bob Bob
View Answer
Answer: c
Explanation: Firstly, the line $bar = &$foo; will reference $foo via $bar. So $bar is assigned value Bob. Therefore
$bar = “My name is $bar”; will print My name is Bob($bar=Bob as said before).
3. Which of the following PHP statements will output Hello World on the screen?
i) echo (“Hello World”);
ii) print (“Hello World”);
iii) printf (“Hello World”);
iv) sprintf (“Hello World”);
a) i) and ii)
b) i), ii) and iii)
c) All of the mentioned
d) i), ii) and iv)
View Answer
Answer: b
Explanation: echo(), print() and printf() all three can be used to output a statement onto the screen. The sprintf()
statement is functionally identical to printf() except that the output is assigned to a string rather than rendered to the
browser.
4. What will be the output of the following PHP code?
<?php
$color = "maroon";
$var = $color[2];
echo "$var";
?>
a) a
b) Error
c) $var
d) r
View Answer
Answer: d
Explanation: PHP treats strings in the same fashion as arrays, allowing for specific characters to be accessed via
array offset notation. In an array, index always starts from 0. So in the line $var = $color[2]; if we count from start
‘r’ comes at index 2. So the output will be r.
5. What will be the output of the following PHP code?
<?php
$score = 1234;
$scoreboard = (array) $score;
echo $scoreboard[0];
?>
a) 1
b) Error
c) 1234
d) 2
View Answer
Answer: c
Explanation: The (array) is a cast operator which is used for converting values from other data types to array.
6. What will be the output of the following PHP code?
advertisement
<?php
$total = "25 students";
$more = 10;
$total = $total + $more;
echo "$total";
?>
a) Error
b) 35 students
c) 35
d) 25 students
View Answer
Answer: c
Explanation: The integer value at the beginning of the original $total string is used in the calculation. However if it
begins with anything but a numerical value, the value will be 0.
7. Which of the below statements is equivalent to $add += $add ?
a) $add = $add
b) $add = $add +$add
c) $add = $add + 1
d) $add = $add + $add + 1
View Answer
Answer: b
Explanation: a += b is an addition assignment whose outcome is a = a + b. Same can be done with
subtraction,multiplication,division etc.
8. Which statement will output $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 escape character.
9. What will be the output of the following code?
<?php
function track() {
static $count = 0;
$count++;
echo $count;
}
track();
track();
track();
?>
a) 123
b) 111
c) 000
d) 011
View Answer
Answer: a
Explanation: Because $count is static, it retains its previous value each time the function is executed.
10. What will be the output of the following PHP code?
<?php
$a = "clue";
$a .= "get";
echo "$a";
?>
a) get
b) true
c) false
d) clueget
View Answer
Answer: d
Explanation: ‘.’ is a concatenation operator. $a. = “get” is same as $a=$a.”get” where $a is having value of “clue” in
the previous statement. So the output will be clueget
PHP Questions & Answers – Basics – 3
This set of PHP Questions and Answers for Freshers focuses on “Basics – 3”.
1. What will be the output of the following PHP code?
<?php
$a = 5;
$b = 5;
echo ($a === $b);
?>
a) 5 === 5
b) Error
c) 1
d) False
View Answer
Answer: c
Explanation: === operator returns 1 if $a and $b are equivalent and $a and $b have the same type.
2. Which of the below symbols is a newline character?
a) \r
b) \n
c) /n
d) /r
View Answer
Answer: b
Explanation: PHP treats \n as newline character.
3. What will be the output of the following PHP code?
<?php
$num = 10;
echo 'What is her age? \n She is $num years old';
?>
a) What is her age? \n She is $num years old
b)
What is her age?
She is $num years old
c) What is her age? She is 10 years old
d)
What is her age?
She is 10 years old
View Answer
Answer: a
Explanation: When a string is enclosed within single quotes both variables and escape sequences will not be
interpreted when the string is parsed.
function foo()
{
$args = func_get_args();
foreach ($args as $k => $v)
{
echo "arg".($k+1).": $v\n";
}
}
foo();
/* will print nothing */
foo("Hello");
/* will print Hello */
foo("Hello","World","Bye");
/* will print Hello World Bye */
2. Which one of the following PHP functions can be used to find files?
a) glob()
b) file()
c) fold()
d) get_file()
View Answer
Answer: a
Explanation: The function glob() returns an array of filenames or directories which matches a specified pattern. The
function returns an array of files/directories, or it will return FALSE on failure. Here is an example-
d) World
View Answer
Answer: b
Explanation: The wordwrap() function wraps a string into new lines when it reaches a specific length.
9. What will be the output of the following PHP code?
<?php
echo ucwords("i love my country");
?>
a) I love my country
b) i love my Country
c) I love my Country
d) I Love My Country
View Answer
Answer: d
Explanation: The ucwords() function converts the first character of each word in a string to uppercase.
10. What will be the output of the following PHP code?
<?php
echo lcfirst("welcome to India");
?>
a) welcome to India
b) welcome to india
c) Welcome to India
d) Welcome to india
View Answer
Answer: a
Explanation: The lcfirst() function converts the first character of a string to lowercase.
PHP Questions & Answers – Arrays-1
This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Arrays”.
1. PHP’s numerically indexed array begin with position ___________
a) 1
b) 2
c) 0
d) -1
View Answer
Answer: c
Explanation: Like all the other programming languages, the first element of an array always starts with ‘0’.
2. Which of the following are correct ways of creating an array?
i) state[0] = “karnataka”;
ii) $state[] = array(“karnataka”);
iii) $state[0] = “karnataka”;
iv) $state = array(“karnataka”);
a) iii) and iv)
b) ii) and iii)
c) Only i)
d) ii), iii) and iv)
View Answer
Answer: a
Explanation: A variable name should start with $ symbol which is not present in i) and you need not put the square
brackets when you use the array() constructor.
3. What will be the output of the following php code?
<?php
$states = array("Karnataka" => array
("population" => "11,35,000", "capital" => "Bangalore"),
"Tamil Nadu" => array( "population" => "17,90,000",
"capital" => "Chennai") );
echo $states["Karnataka"]["population"];
?>
a) Karnataka 11,35,000
b) 11,35,000
c) population 11,35,000
d) Karnataka population
View Answer
Answer: b
Explanation: In the following PHP code, the variable states are treated as a multidimensional array and accordingly
traverse it to get the value of ‘Karnataka’s population’.
4. Which of the following PHP function will return true if a variable is an array or false if it is not an array?
a) this_array()
b) is_array()
c) do_array()
d) in_array()
View Answer
Answer: b
Explanation: The function is_array() is an inbuilt function in PHP which is used to check whether a variable is an
array or not. Its prototype follows: boolean is_array(mixed variable).
5. Which in-built function will add a value to the end of an array?
a) array_unshift()
b) into_array()
c) inend_array()
d) array_push()
View Answer
Answer: d
Explanation: array_push adds a value to the end of an array, returning the total count of elements in the array after
the new value has been added.
6. What will be the output of the following PHP code?
<?php
$state = array ("Karnataka", "Goa", "Tamil Nadu",
"Andhra Pradesh");
echo (array_search ("Tamil Nadu", $state) );
?>
a) True
b) 1
c) False
d) 2
View Answer
Answer: d
Explanation: The array_search() function searches an array for a specified value, returning its key if located and
FALSE otherwise.
7. What will be the output of the following PHP code?
<?php
$fruits = array ("apple", "orange", "banana");
echo (next($fruits));
echo (next($fruits));
?>
a) orangebanana
b) appleorange
c) orangeorange
d) appleapple
View Answer
Answer: a
Explanation: The next() function returns the value of the next element in the array. In the first ‘next($fruits)’ call, it
will print orange which is next to apple and so on.
advertisement
8. Which of the following function is used to get the value of the previous element in an array?
a) last()
b) before()
c) prev()
d) previous()
View Answer
Answer: c
Explanation: The prev() function returns the previous element in the array.
9. What will be the output of the following PHP code?
<?php
$fruits = array ("apple", "orange", array ("pear", "mango"),
"banana");
echo (count($fruits, 1));
?>
a) 3
b) 4
c) 5
d) 6
View Answer
Answer: d
Explanation: The 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.
10. Which function returns an array consisting of associative key/value pairs?
a) count()
b) array_count()
c) array_count_values()
d) count_values()
View Answer
Answer: c
Explanation: The function array_count_values() will count all the values of an array. It will return an associative
array, where the keys will be the original array’s values, and the values are the number of occurrences.
PHP Coding Questions and Answers – Arrays – 2
This set of Tricky PHP Questions & Answers focuses on “Arrays – 2”.
1. What will be the output of the following PHP code?
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[2] . ", " . $cars[1] . " and " . $cars[0] . ".";
?>
a) I like Volvo, Toyota and BMW
b) I like Volvo, BMW and Toyota
c) I like BMW, Volvo and Toyota
d) I like Toyota, BMW and Volvo
View Answer
Answer: d
Explanation: The order of elements defined. In the echo statement when we call the elements of array using its
index, it will be printed accordingly. As index ‘0’ indicates ‘Volvo’ , ‘1’ for ‘BMW’ and ‘2’ for Toyota’.
2. What will be the output of the following PHP code?
<?php
$fname = array("Peter", "Ben", "Joe");
$age = array("35", "37", "43");
$c = array_combine($age, $fname);
print_r($c);
?>
a) Array ( Peter Ben Joe )
b) Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )
c) Array ( 35 37 43 )
d) Array ( [35] => Peter [37] => Ben [43] => Joe )
View Answer
Answer: d
Explanation: Here “keys” array is $age and “values” array is $fname. The function array_combine() will create an
array by using the elements from one “keys” array and one “values” array. So when variable c is called, it will print
keys and values.
3. What will be the output of the following PHP code?
<?php
$a=array("A","Cat","Dog","A","Dog");
$b=array("A","A","Cat","A","Tiger");
$c=array_combine($a,$b);
print_r(array_count_values($c));
?>
a) Array ( [A] => 5 [Cat] => 2 [Dog] => 2 [Tiger] => 1 )
b) Array ( [A] => 2 [Cat] => 2 [Dog] => 1 [Tiger] => 1 )
c) Array ( [A] => 6 [Cat] => 1 [Dog] => 2 [Tiger] => 1 )
d) Array ( [A] => 2 [Tiger] => 1 )
View Answer
Answer: d
Explanation: The function The array_count_values() counts all the values of an array and the The function
array_combine() will create an array by using the elements from one “keys” array and one “values” array.
4. What will be the output of the following PHP code?
<?php
$a1 = array("a" => "red", "b" => "green", "c" => "blue", "d" => "yellow");
$a2 = array("e" => "red", "f" => "green", "g" => "blue", "h" => "orange");
$a3 = array("i" => "orange");
$a4 = array_merge($a2, $a3);
$result = array_diff($a1, $a4);
print_r($result);
?>
a) Array ( [d] => yellow )
b) Array ( [i] => orange )
c) Array ( [h] => orange )
d) Array ( [d] => yellow [h] => orange )
View Answer
Answer: a
Explanation: The array_diff() function compares the values of two (or more) arrays, and returns the differences. This
function compares the values of two (or more) arrays, and return an array that contains the entries from array1 that
are not present in other arrays (array2, array3, etc).
5. What will be the output of the following PHP code?
<?php
$a1 = array("red", "green");
$a2 = array("blue", "yellow");
$a3 = array_merge($a1, $a2);
$a4 = array("a", "b", "c", "d");
$a = array_combine($a4, $a3);
print_r($a);
?>
a) Array ( [a] => blue [b] => yellow [c] => red [d] => green )
b) Array ( [0] => blue [1] => yellow [2] => red [3] => green )
c) Array ( [0] => red [1] => green [2] => blue [3] => yellow )
d) Array ( [a] => red [b] => green [c] => blue [d] => yellow )
View Answer
Answer: d
Explanation: The function array_merge() merges one or more arrays into one array. If in the function array_merge(),
two or more array elements have the same key, the last one overrides the others. The function array_combine() will
create an array by using the elements from one “keys” array and one “values” array. The program is the basic
combined application of array_combine() and array_merge().
6. What will be the output of the following PHP code?
<?php
$a = array("a" => "india", "b" => "brazil", "c" => "china");
echo array_shift($a);
echo "<br>";
array_pop($a);
print_r($a);
?>
a)
india
Array ( [b] => Brazil )
advertisement
b)
india
Array ( [a] => brazil )
c)
china
Array ( [a] => india )
d)
china
Array ( [a] => brazil )
View Answer
Answer: a
Explanation: The function array_shift() removes the first element from an array, and it returns the value of the
removed element and the function array_pop() deletes the last element of an array. So “a” => “India”, “c” =>
“China” will be deleted and “b” => “Brazil” will be printed.
3. The practice of creating objects based on predefined classes is often referred to as ______________
a) class creation
b) object creation
c) object instantiation
d) class instantiation
View Answer
Answer: d
Explanation: In object-oriented programming, classes are the blueprints of php objects. Classes do not actually
become objects until instantiation is done. When someone instantiates a class, it creates an instance of it, thus
creating the object. In other words, instantiation is the process of creating an instance of an object in memory.
4. Which one of the following property scopes is not supported by PHP?
a) friendly
b) final
c) public
d) static
View Answer
Answer: a
Explanation: PHP supports five class property scopes: public, private, protected, final and static.
5. Which one of the following can be used to instantiate an object in PHP assuming class name to be Foo?
a) $obj = new $foo;
b) $obj = new foo;
c) $obj = new foo ();
d) obj = new foo ();
View Answer
Answer: c
Explanation: To create a new object in PHP we can use the new statement to instantiate a class.
6. Which one of the following is the right way to define a constant?
a) constant PI = “3.1415”;
b) const $PI = “3.1415”;
c) constant PI = ‘3.1415’;
d) const PI = ‘3.1415’;
View Answer
Answer: d
Explanation: Class constants are created like: const NAME = ‘VALUE’;
7. Which one of the following is the right way to call a class constant, given that the class is mathFunction?
a) echo PI;
b) echo mathFunction->PI;
c) echo mathFunction::PI;
d) echo mathFunction=PI;
View Answer
Answer: c
Explanation: The Scope Resolution Operator “::” is a token that allows access to static, constant, and overridden
properties or methods of a class.
8. Which one of the following is the right way to invoke a method?
a) $object->methodName();
b) object->methodName();
c) object::methodName();
d) $object::methodName();
View Answer
Answer: a
Explanation: “->” is a dynamic class method invocation in PHP.
9. Which of the following is/are the right way to declare a method?
i) function functionName() { function body }
ii) scope function functionName() { function body }
iii) method methodName() { method body }
iv) scope method methodName() { method body }
a) Only ii)
b) Only iv)
c) i) and ii)
d) iii) and iv)
View Answer
Answer: c
Explanation: In case of public methods, you can forgo explicitly declaring the scope and just declare the method like
you would a function.
10. Which of the following method scopes is/are not supported by PHP?
i) private
ii) friendly
iii) static
iv) abstract
a) Only ii)
b) Only iv)
c) ii) and iv)
d) Only i)
View Answer
Answer: a
Explanation: PHP supports six method scopes: public, private, final, static, protected and abstract. But it does not
support friendly
PHP Questions & Answers – Basics of Object-Oriented PHP- 2
This set of PHP Questions and Answers for Experienced people focuses on “Basics of Object-Oriented PHP- 2”.
1. Which method scope prevents a method from being overridden by a subclass?
a) Abstract
b) Protected
c) Final
d) Static
View Answer
Answer: c
Explanation: When we declare a method is as final then it is not possible to override that method. Methods should
not be overridden due to some security or any other reasons.
2. Which of the following statements is/are true about Constructors in PHP?
i) PHP 4 introduced class constructors.
ii) Constructors can accept parameters.
iii) Constructors can call class methods or other functions.
iv) Class constructors can call on other constructors.
a) ii)
b) ii) and iii)
c) i), ii), iii) and iv)
d) ii), iii) and iv)
View Answer
Answer: c
Explanation: If a class name and the function name is similar then the function is known as constructor. Constructor
is automatically called when an object will be initialized. Constructors can accept parameters. Constructors can call
class methods or other functions. Class constructors can call on other constructors.
3. PHP recognizes constructors by the name _________
a) classname()
b) _construct()
c) function _construct()
d) function __construct()
View Answer
Answer: d
Explanation: A double underscore followed by the construct keyword. Its syntax is function __construct
([ argument1, argument2,…..]) { Class Initialization code }.
4. Which version of PHP introduced the instanceof keyword?
a) PHP 4
b) PHP 5
c) PHP 5.3
d) PHP 6
View answer
Answer: b
Explanation: Using instanceof keyword we can determine whether an object is an instance of a class. $manager =
new Employee() … if ($manager instanceof Employee) echo “True”;
5. Which one of the following functions is used to determine whether a class exists?
a) exist()
b) exist_class()
c) class_exist()
d) __exist()
View Answer
Answer: c
Explanation: The class_exist() function returns true or false according to whether the class exists within the
currently executing script content.
6. Which one of the following functions is used to determine object type?
a) obj_type()
b) type()
c) is_a()
d) is_obj()
View Answer
Answer: c
Explanation: The is_a() function returns true if object belongs to a class type or if it belongs to a class that is a child
of that class. Or else false is returned.
7. Which one of the following keyword is used to inherit our subclass into a superclass?
a) extends
b) implements
c) inherit
d) include
View Answer
Answer: a
Explanation: When we extend a class then the subclass will inherit all the public and protected methods from the
parent class.
The keyword implements are used with interfaces. With inheritance, we use the keyword extends.
8. In the PHP code given below, what is/are the properties?
<?php
class Example
{
public $name;
function Sample()
{
echo "This is an example";
}
}
?>
a) echo “This is an example”;
b) public $name;
c) class Example
d) function sample()
View Answer
Answer: b
Explanation: Above code is an example of ‘classes’ in PHP. 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.
advertisement
9. Which keyword is used to refer to properties or methods within the class itself?
a) private
b) public
c) protected
d) $this
View Answer
Answer: d
Explanation: In PHP, the self and ‘this’ keyword are used to refer the class members within the scope of a class
itself. The class members can be either variables or functions.
10. Which keyword allows class members (methods and properties) to be used without needing to instantiate a new
instance of the class?
a) protected
b) final
c) static
d) private
View Answer
Answer: c
Explanation: Sometimes it is very handy to access the methods and properties in terms of a class rather than an
object. But this can be done through static keyword. Any method declared as ‘static’ can be accessed without the
creation of an object
PHP Questions & Answers – Strings and Regular Expressions – 1
This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Strings and Regular Expressions – 1”.
1. PHP has long supported two regular expression implementations known as _______ and _______
i) Perl
ii) PEAR
iii) Pearl
iv) POSIX
a) i) and ii)
b) ii) and iv)
c) i) and iv)
d) ii) and iii)
View Answer
Answer: c
Explanation: None.
2. Which one of the following regular expression matches any string containing zero or one p?
a) p+
b) p*
c) P?
d) p#
View Answer
Answer: c
Explanation: None.
9. Which of the following would be a potential match for the Perl-based regular expression /fo{2,4}/ ?
i) fol
ii) fool
iii) fooool
iv) fooooool
a) Only i)
b) ii) and iii)
c) i), iii) and iv)
d) i) and iv)
View Answer
Answer: b
Explanation: This matches f followed by two to four occurrences of o.
10. Which among the following is/are not a metacharacter?
i) \a
ii) \A
iii) \b
iv) \B
a) Only i)
b) i) and iii)
c) ii), iii) and iv)
d) ii) and iv)
View Answer
Answer: a
Explanation: /A, /b and /B are metacharacters. \A: Matches only at the beginning of the string. \b: Matches a word
boundary. \B: Matches anything but a word boundary.
PHP Questions & Answers – Strings and Regular Expressions – 2
This set of PHP Interview Questions and Answers for Experienced people focuses on “Strings and Regular
Expressions – 2”.
1. How many functions does PHP offer for searching and modifying strings using Perl-compatible regular
expressions.
a) 7
b) 8
c) 9
d) 10
View Answer
Answer: b
Explanation: The functions are preg_filter(), preg_grep(), preg_match(), preg_match_all(), preg_quote(),
preg_replace(), preg_replace_callback(), and preg_split().
2. What will be the output of the following PHP code?
<?php
$foods = array("pasta", "steak", "fish", "potatoes");
$food = preg_grep("/^s/", $foods);
print_r($food);
?>
a) Array ( [0] => pasta [1] => steak [2] => fish [3] => potatoes )
b) Array ( [3] => potatoes )
c) Array ( [1] => steak )
d) Array ( [0] => potatoes )
View Answer
Answer: c
Explanation: This function is used to search an array for foods beginning with s.
3. Say we have two compare two strings which of the following function/functions can you use?
i) strcmp()
ii) strcasecmp()
iii) strspn()
iv) strcspn()
a) i) and ii)
b) iii) and iv)
c) None of the mentioned
d) All of the mentioned
View Answer
Answer: d
Explanation: All of the functions mentioned above can be used to compare strings in some or the other way.
4. Which one of the following functions will convert a string to all uppercase?
a) strtoupper()
b) uppercase()
c) str_uppercase()
d) struppercase()
View Answer
Answer: a
Explanation: Its prototype follows string strtoupper(string str).
5. What will be the output of the following PHP code?
<?php
$title = "O'malley wins the heavyweight championship!";
echo ucwords($title);
?>
a) O’Malley Wins The Heavyweight Championship!
b) O’malley Wins The Heavyweight Championship!
c) O’Malley wins the heavyweight championship!
d) o’malley wins the heavyweight championship!
View Answer
Answer: d
Explanation: The ucwords() function capitalizes the first letter of each word in a string. Its prototype follows: string
ucwords(string str).
6. What will be the output of the following PHP code?
<?php
echo str_pad("Salad", 5)." is good.";
?>
a) SaladSaladSaladSaladSalad is good
b) is good SaladSaladSaladSaladSalad
c) is good Salad
d) Salad is good
View Answer
Answer: d
Explanation: The str_pad() function pads a string with a specified number of characters.
advertisement
7. Which one of the following functions can be used to concatenate array elements to form a single delimited string?
a) explode()
b) implode()
c) concat()
d) concatenate()
View Answer
Answer: b
Explanation: None.
8. Which one of the following functions finds the last occurrence of a string, returning its numerical position?
a) strlastpos()
b) strpos()
c) strlast()
d) strrpos()
View Answer
Answer: d
Explanation: None.
9. What will be the output of the following PHP code?
<?php
$author = "[email protected]";
$author = str_replace("a","@",$author);
echo "Contact the author of this article at $author.";
?>
a) Contact the author of this article at nachiketh@[email protected]
b) Cont@ct the @uthor of this @rticle @t n@chiketh@[email protected]
c) Contact the author of this article at n@chiketh@[email protected]
d) Error
View Answer
Answer: c
Explanation: The str_replace() function case sensitively replaces all instances of a string with another.
10. What will be the output of the following PHP code?
<?php
$url = "[email protected]";
echo ltrim(strstr($url, "@"),"@");
?>
a) [email protected]
b) nachiketh
c) nachiketh@
d) example.com
View Answer
Answer: d
Explanation: The strstr() function returns the remainder of a string beginning with the first occurrence of a
predefined string.
PHP Questions & Answers – HTML Forms
This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “HTML Forms”.
1. Which two predefined variables are used to retrieve information from forms?
a) $GET & $SET
b) $_GET & $_SET
c) $__GET & $__SET
d) GET & SET
View Answer
Answer: b
Explanation: The global variables $_GET is used to collect form data after submitting an HTML form with the
method=”get”. The variable $_SET is also used to retrieve information from forms.
2. The attack which involves the insertion of malicious code into a page frequented by other users is known as
_______________
a) basic sql injection
b) advanced sql injection
c) cross-site scripting
d) scripting
View Answer
Answer: c
Explanation: The cross-site scripting attack is among one of the top five security attacks carried out across the
Internet. It is also known as XSS, this attack is a type of code injection attack which is made possible by incorrectly
validating user data, which usually gets inserted into the page through a web form or using an altered hyperlink.
3. When you use the $_GET variable to collect data, the data is visible to ___________
a) none
b) only you
c) everyone
d) selected few
View Answer
Answer: c
Explanation: The information sent from a form with the method GET is visible to everyone i.e. all variable names
and values are displayed in the URL.
4. When you use the $_POST variable to collect data, the data is visible to ___________
a) none
b) only you
c) everyone
d) selected few
View Answer
Answer: b
Explanation: The information sent from a form with the method POST is invisible to others i.e. all names/values are
embedded within the body of the HTTP request.
5. Which variable is used to collect form data sent with both the GET and POST methods?
a) $BOTH
b) $_BOTH
c) $REQUEST
d) $_REQUEST
View Answer
Answer: d
Explanation: In PHP the global variable $_REQUEST is used to collect data after submitting an HTML form.
6. Which one of the following should not be used while sending passwords or other sensitive information?
a) GET
b) POST
c) REQUEST
d) NEXT
View Answer
Answer: a
Explanation: The information sent from a form with the method GET is visible to everyone i.e. all variable names
and values are displayed in the URL. So, it should not be used while sending passwords or other sensitive
information.
7. Which function is used to remove all HTML tags from a string passed to a form?
a) remove_tags()
b) strip_tags()
c) tags_strip()
d) tags_remove()
View Answer
Answer: b
Explanation: The function strip_tags() is used to strip a string from HTML, XML, and PHP tags.
8. What will be the value of the variable $input in the following PHP code?
<?php
$input = "Swapna<td>Lawrence</td>you are really<i>pretty</i>!";
$input = strip_tags($input,"<i></i>");
echo $input;
?>
a) Swapna Lawrence you are really pretty!
b) Swapna <td>Lawrence</td> you are really<i>pretty</i>!
c) Swapna <td>Lawrence</td> you are really pretty!
d) Swapna Lawrence you are really<i>pretty</i>!
View Answer
Answer: d
Explanation: Italic tags <i></i> might be allowable, but table tags <td></td> could potentially wreak havoc on a
page.
advertisement