1. Which of the following will produce a value of “83” as its output?
echo intval(“0123”, 8);
echo (int) 083
echo (int) 123;
echo intval(“0123”);
Answer : echo intval(“0123”, 8);
2. What will be the output of the following code?
class Person
{
protected $name;
public function __construct($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
}
$person = new Person(“Foo”);
echo $person->getName();
Nothing will be printed on-screen.
Foo
Syntax error on line $this->name=$name
Fatal error on line $this->name=$name
Answer : Foo
3. What is the best practice for running MySQL queries in PHP? Consider the risk of SQL injection.
Use mysql_query() and variables:
for example:
$input = $_POST[‘user_input’];
mysql_query(“INSERT INTO table (column) VALUES (‘” . $input . “‘)”);
Use PDO prepared statements and parameterized queries:
for example:
$input= $_POST[“user-input”]
$stmt = $pdo->prepare(‘INSERT INTO table (column) VALUES (“:input”);
$stmt->execute(array(‘:input’ => $input));
Use mysql_query() and string escaped variables:
for example:
$input= $_POST[“user-input”]
$input_safe = mysql_real_escape_string($input);
mysql_query(“INSERT INTO table (column) VALUES (‘” . $input. “‘)”);
Use mysql_query() and variables with a blacklisting check:
for example:
$blacklist = array(“DROP”,”INSERT”,”DELETE”);
$input= $_POST[“user-input”]
if (!$array_search($blacklist))) mysql_query(“INSERT INTO table (column) VALUES (‘” . $input. “‘)”);
Answer : Use PDO prepared statements and parameterized queries:
for example:
$input= $_POST[“user-input”]
$stmt = $pdo->prepare(‘INSERT INTO table (column) VALUES (“:input”);
$stmt->execute(array(‘:input’ => $input));
4. Which MIME type needs to be used to send an attachment in mail?
text/html
text/plain
application/mixed
multipart/mixed
Answer : multipart/mixed
5. Which of the following file modes is used to write into a file at the end of the existing content, and create the file if the file does not exist?
r+
w+
a
x
Answer : a
6. Which of the following is not a valid DOM method in PHP?
loadXMLFile()
loadXML()
loadHTMLFile()
loadHTML()
Answer : loadXMLFile()
7. Placing the ____ symbol before a function tells PHP to suppress any errors generated by that function.
!
@
#
$
Answer : @
8. Given the following array:
$array = array(0 => ‘blue’, 1 => ‘red’, 2 => ‘green’, 3 => ‘red’);
Which one of the following will print 2?
echo array_search(‘green’, $array);
echo in_array(‘green’, $array);
echo array_key_exists(2, $array);
echo array_search(‘red’,$array);
Answer : echo array_search(‘green’, $array);
9. Which statement is not correct?
$x = null;
empty($x) return TRUE
is_null($x) return FALSE
isset($x) return FALSE
None of the above
Answer : None of the above
10. Which of the following are valid MySQLi Configuration Options? (choose all that apply)
Note: There may be more than one right answer.
mysqli.allow_persistent
mysqli.rollback_on_cached_size
mysqli.default_port
mysqli.default_socket
Answer : mysqli.default_port
mysqli.default_socket
mysqli.allow_persistent
11. Where can protected property or method be accessed?
it can only be accessed within the class itself.
it can be accessed outside of the class.
it can only be accessed within the class itself or in descendant classes.
It can not be accessed anywhere.
Answer : it can only be accessed within the class itself or in descendant classes.
12. What is the correct way to send an SMTP (Simple Mail Transfer Protocol) email using PHP?
s.sendmail($EmailAddress, [$MessageBody], msg.as_string())
sendmail($EmailAddress, “Subject”, $MessageBody);
mail($EmailAddress, “Subject”, $MessageBody);
<a href=”mailto:$EmailAddress“>$MessageBody</a>
Answer : mail($EmailAddress, “Subject”, $MessageBody);
13. Which function can be used to determine if a file exists? (choose all that apply)
Note: There may be more than one right answer.
is_readable()
file_exists()
feof()
is_file_exists()
Answer : is_readable()
file_exists()
14. Which of these is not a valid PHP XML API?
libxml_clear_errors()
libXMLError()
libxml_get_errors()
libxml_use_internal_errors()
Answer : libXMLError()
15. What will happen if a fatal error was thrown in your PHP program?
The PHP program will stop executing at the point where the error occurred.
The PHP program will show a warning message and program will continue executing.
Since PHP is a scripting language so it can not throw a fatal error.
Nothing will happen.
Answer : The PHP program will stop executing at the point where the error occurred.
16. PHP currently offers the following functions for searching strings using POSIX-style regular expressions? (choose all that apply)
Note: There may be more than one right answer.
sql_regcase()
eragi_replace()
ereg_replace()
spliti()
erag()
Answer : sql_regcase() ereg_replace()
17. ___ will display everything about the PHP installation, including the modules, version, variables, etc.
info()
phpinfo()
phpdata()
php()
Answer : phpinfo()
18. How to create a database with PHP in MySQL Server?
create_db(‘database’);
mysql_db(‘database’);
mysql_create_db(‘database’);
mysql_server_create_db(‘database’);
Answer : mysql_create_db(‘database’);
19. Which of the following is true about the singleton design pattern?
A singleton pattern means that a class will only have a single method.
A singleton pattern means that a class can have only one instance object.
A singleton pattern means that a class has only a single member variable.
Singletons cannot be implemented in PHP.
Answer : A singleton pattern means that a class can have only one instance object.
20. What is the correct syntax of mail() function in PHP?
mail($to,$subject,$message,$headers)
mail($from,$to,$subject,$message)
mail($to,$from,$subject,$message)
mail($to,$from,$message,$headers)
Answer : mail($to,$subject,$message,$headers)
21. How can you rename a file in PHP?
file_rename(oldname, newname)
file(oldname, newname)
rename(oldname, newname);
file_change(oldname, newname)
Answer : rename(oldname, newname);
22. What is wrong with the following code?
<?php
curl_setopt($ch, CURLOPT_URL, “http://www.example.com/”);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
?>
There is nothing wrong with the code.
The cURL resource $ch has not been created using the curl_init() method.
The $ch variable needs to be initialized as $ch=null;.
The code will cause a parse error.
Answer : The cURL resource $ch has not been created using the curl_init() method.
23. You can extend the exception class, but you can not override any of the preceding methods because they are declared as:
protected
final
static
private
Answer : static
24. __________ is a PHP super global variable which holds information about headers, paths, and script locations.
$_REQUEST
$_SERVER
$_INFO
$_GET
Answer : $_SERVER
25. Which in-built function will add a value to the end of an array?
array_unshift()
into_array()
inend_array()
array_push()
Answer : array_push()
26. The PDO_MYSQL Data Source Name (DSN) is composed of the following elements?
Note: There may be more than one right answer.
user_pass
db_table
dbname
unix_socket
charset
Answer : unix_socket
charset
27. What are methods in PHP?
Note: There may be more than one right answer.
Individual actions that an object will be able to perform are defined within the class as methods.
An individual instance of the data structure defined by a class.
These are the function defined inside a class and are used to access object data.
These are the functions in which the objects of the class are saved.
Answer :
Individual actions that an object will be able to perform are defined within the class as methods.
These are the function defined inside a class and are used to access object data.
28. What are methods in PHP?
Note: There may be more than one right answer.
Individual actions that an object will be able to perform are defined within the class as methods.
An individual instance of the data structure defined by a class.
These are the function defined inside a class and are used to access object data.
These are the functions in which the objects of the class are saved.
Answer :
Individual actions that an object will be able to perform are defined within the class as methods.
These are the function defined inside a class and are used to access object data.
29. Which of the following is used to send mail in PHP?
send_mail(to,subject,message,headers,parameters);
mail(to,subject,message,headers,parameters);
mail::(to,subject,message,headers,parameters);
mail_to(to,subject,message,headers,parameters);
Answer : mail(to,subject,message,headers,parameters);
30. Which of these is not a valid SimpleXML Parser method?
simplexml_import_dom()
simplexml_import_sax()
simplexml_load_file()
simplexml_load_string()
Answer : simplexml_import_sax()
31. PHP ____ are used to validate and sanitize external input.
Cookies
Filters
Exceptions
Files
Answer : Filters
32. <?php
$arr = array(‘Hello’,’World!’,’Beautiful’,’Day!’);
echo implode(” “,$arr);
?>
What will be the output of above code?
HelloWorld!BeautifulDay!
‘Hello’,’World!’,’Beautiful’,’Day!’
Hello World! Beautiful Day!
No output
Answer : Hello World! Beautiful Day!
33. Which function is used to destroy a variable or object?
destroy()
clear()
unset()
reset()
Answer : unset()
34. How can you connect with a MySQL Server in PHP?
database_connect(server, username, password)
mysql_connect(host, username, password)
mysql(server, username, password)
connect(server, username, password)
Answer : mysql_connect(host, username, password)
35. Which method is used to tweak an object’s cloning behavior?
clone()
__clone()
_clone
object_clone()
Answer : __clone()
36. <?php
var_dump( floor(3.74) );
?>
What will be the output of the code?
4
3
float(3)
floor(3)
Answer : float(3)
37. What is the correct syntax of constructor function?
function Construct($var, $var2){
//Code
}
__construct( $var1, $var2 ) {
// Code
}
function __construct( $var1, $var2 ) {
// Code
}
function constructor( $var1, $var2 ) {
// Code
}
Answer : function __construct( $var1, $var2 ) {
// Code
}
38. How can short tags be enabled in PHP?
Set the value of short_open_tag=on in the php.ini file and restart the web server.
Set the value of short_tag=on in the php.ini file and restart the web server.
Set the value of short_tags=on in the php.ini file.
None of the above.
Answer : Set the value of short_open_tag=on in the php.ini file and restart the web server.
39. With regards to the “static” keyword in PHP, which of the following statements is false?
The $this variable can be used inside any static method.
Static properties may only be initialized using a literal or a constant.
A property declared as static can not be accessed with an instantiated class object.
A static variable or method can be accessed without requiring instantiation of the class.
Answer : The $this variable can be used inside any static method.
40. How to remove whitespace from the beginning and end of $string variable?
cut($string);
removeSpace($string);
shorten($string);
trim($string);
Answer : trim($string);
41. Which function is used to read a file removing the HTML and PHP tags in it?
fgetss()
fgets()
fopen()
file_strip_tags()
42. What is the difference between “echo” and “print” in PHP?
Note: There may be more than one right answer.
“echo” can take multiple parameters, while “print” can only take a single parameter.
“print” has a return value of 1 so it can be used in expressions whereas “echo” has a void return type.
“print” performs slightly slower than “echo”.
There is no difference between “print” and “echo”.
Answer : “print” performs slightly slower than “echo”.
“print” has a return value of 1 so it can be used in expressions whereas “echo” has a void return type.
“echo” can take multiple parameters, while “print” can only take a single parameter.
43. The ________ function delays execution of the current script for a specified number of seconds.
delay()
timer()
sleep()
slow()
Answer : sleep()
44. <?php
$x = 1;
while ($x <= 3){
echo “$x, “;
$x++;
}
?>
What will be the output of above code?
1
1, 2, 3,
1, 2, 3
No output
Answer : 1, 2, 3,
45. Which of these is not a valid PHP XML DOM method?
getElementsByTagName
removeChild()
getElementsById()
appendChild()
Answer : getElementsById()
46. Which statement is incorrect with respect to “!==” and “==!”?
There is no such operator as ==! in PHP.
==! is a combination of 2 operators, == and !. It is same as == (!$var)
There is no such operator as !== in PHP.
!== returns true if left hand side operand is not identical to right hand side operand.
Answer : There is no such operator as ==! in PHP.
47. The _________ statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the statement. Check all that apply.
Note: There may be more than one right answer.
require
include
get
add
Answer : require
include
48. Several predefined variables in PHP are _____.
Superglobals
Data types
Functions
Arrays
Answer : Superglobals
49. Which expression will not produce ‘abcdef’ as a result provided if $var1=’abc’ and $var2=’def’?
$var1 + $var2
“{$var1}{$var2}”
$var1.$var2
implode(”, array($var1,$var2))
Answer : $var1 + $var2