In this section we are providing you some frequently asked PHP Interview Questions which will help you to win interview session easily. These sample questions are framed by experts. Candidates must read this section, Then by heart the questions and answers.

If there is any PHP interview question that have been asked to you, kindly post it in the the comment section.


 

1. How to collect IP address from an HTTP request ?

Ans:

$_SERVER['REMOTE_ADDR'];

2. How to collect IP address of the Web server in php ?

Ans:

$_SERVER['SERVER_ADDR'];

3. How to enable error reporting ?

Ans:

error_reporting(E_ALL);

4. What is $_GLOBAL ?

Ans:

It is an associative array which containing references to all variables currently defined in the global scope of the script.

5.What is .htaccess file ?

Ans:
.htaccess is a configuration file used to alter the default behavior of a Apache web server software. Most common usage is to redirect the http request to some URLs based on some conditions. For example, we can hide the .html or .php extensions of the URLs to make it SEO friendly

6.How to print structured information about a available in PHP ?

Ans:
var_dump — This function displays structured information about one or more expressions that includes its value and type. Objects and arrays are explored recursively with values indented to show structure.

Usage var_dump($a);

7.What is the difference between var_dump and print_r ?

Ans:
var_dump will display all the information of a variable including keys values and types while print_r display the keys and values only in a human readable format.

8.What is automatic type conversion ?

Ans:

In php we can declare variables without specifying its type, php it do that process automatically since PHP is a loosely types language.

For example :

 

9.How to make API calls from php scripts ?

Ans:
We can use cURL library to make HTTP calls from a php script

$ch = curl_init(); $postData='var1=value1&var2=value2&var3=value3'; curl_setopt($ch, CURLOPT_URL, "http://mydomain.com/ajaxurl"); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); curl_close($ch); echo $result;die;

10.How to change the php configurations at run time ?

Ans:

We can use `ini_set` function to change the configurations of php at run time.



 

11.How can we resolve the errors like ‘Maximum execution time of 30 seconds exceeded’ ?

Ans:

We can use the following code to increase the maximum execution time of the script

ini_set('max_execution_time', 300);

We can also set the max_execution_time for all the scripts in a website by the following code in .htaccess file

  php_value max_execution_time 300   

But, we must try to optimize the php script to avoid this kind of situations as a part good user experience.

12.How to avoid email sent through php getting into spam folder?

Ans:

There’s no special method of keeping your emails from being identified as spam. However we can consider some points that cause this problem. Let me explain few common reasons.

  1. sending mail using the `mail` function with minimum parameters
    We must use all possible mail headers like `MIME-version`, `Content-type`, `reply address`, `from address` etc in order to avoid this situation
  2. Not using a proper SMTP mail script like PHPmailer or SwiftMailer with an actual e-mail credentials including username, password etc

If we are able to send e-mail from an actual e-mail account using an SMTP mailer script with username and password, then we can avoid
If you’re on a shared web server, consider buying a unique IP address for yourself, because others using your IP may have gotten your IP blacklisted for spam. Do not send more than 250 emails to each provider per hour.
Give your users unsubscribe link and view in browser link, if they cannot see the email properly they will mark you as spam, if they no longer want emails for you they will mark you as spam.

13.How can we prevent SQL injection in PHP?

Ans:

Most popular way is, use prepared statements and parameterized queries. These are SQL statements that are sent to and parsed by the database server separately from any parameters. This way it is impossible for an attacker to inject malicious SQL.
You basically have two options to achieve this:

  • Using PDO (for any supported database driver):
$stmt = $pdo->prepare('SELECT * FROM employees WHERE name = :name');
$stmt->execute(array('name' => $name));
foreach ($stmt as $row) {
// do something with $row
}
  • Using MySQLi (for MySQL):
$stmt = $dbConnection->prepare('SELECT * FROM employees WHERE name = ?'); 
$stmt->bind_param('s', $name);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
// do something with $row
}

If you’re connecting to a database other than MySQL, there is a driver-specific second option that you can refer to (e.g. pg_prepare() and pg_execute() for PostgreSQL). PDO is the universal option.

14. How to fix “Headers already sent” error in PHP ?

Ans:

No output before sending headers. there should not be any output (i.e. echo.. or HTML codes) before the header(…….);command. remove any white-space(or newline) before tags. After header(…); you must use exit;

15.How to return JSON from a PHP Script ?

Ans:

We have to set the Content-Type header with application/json  as the value and print a valid JSON data using echo method

For example

 header('Content-Type: application/json');  
echo json_encode($data);

16.How can we fix “Memory size Exhausted” errors ?

Ans;

You can fix it at run time or can be set required size on the php.ini file

ini_set(‘memory_limit’, ‘512M’); // In a php script at run time

17.What’s the difference between the include() and require() functions?

Ans: 

They both include a specific file but on require the process exits with a fatal error if the file can’t be included, while include statement may still pass and jump to the next step in the execution.

18.How can we get the IP address of the client?

Ans:

This question might show you how playful and creative the candidate is because there are many options. $_SERVER[“REMOTE_ADDR”]; is the easiest solution, but the candidate can write x line scripts for this question.

19.What’s the difference between unset() and unlink()

Ans:

unset() sets a variable to “undefined” while unlink() deletes a file we pass to it from the file system.

20.What are the main error types in PHP and how do they differ?

Ans:

In PHP there are three main type of errors:

  • Notices – Simple, non-critical errors that are occurred during the script execution. An example of a Notice would be accessing an undefined variable.
  • Warnings – more important errors than Notices, however the scripts continue the execution. An example would be include() a file that does not exist.
  • Fatal – this type of error causes a termination of the script execution when it occurs. An example of a Fatal error would be accessing a property of a non-existent object or require() a non-existent file.

Understanding the error types is very important as they help developers understand what is going on during the development, and what to look out for during debugging.




 

21. What is the difference between GET and POST?

Ans:

  • GET displays the submitted data as part of the URL, during POST this information is not shown as it’s encoded in the request.
  • GET can handle a maximum of 2048 characters, POST has no such restrictions.
  • GET allows only ASCII data, POST has no restrictions, binary data are also allowed.
  • Normally GET is used to retrieve data while POST to insert and update.

Understanding the fundamentals of the HTTP protocol is very important to have for a PHP developer, and the differences between GET and POST are an essential part of it.

22. How can you enable error reporting in PHP?

Ans:

Check if “display_errors” is equal “on” in the php.ini or declare “ini_set(‘display_errors’, 1)” in your script.

Then, include “error_reporting(E_ALL)” in your code to display all types of error messages during the script execution.

Enabling error messages is very important especially during the debugging process as one can instantly get the exact line that is producing the error and can see also if the script in general is behaving correctly.

23. What are Traits?

Ans:

Traits are a mechanism that allows you to create reusable code in languages like PHP where multiple inheritance is not supported. A Trait cannot be instantiated on its own.

It’s important that a developer knows the powerful features of the language (s)he is working on, and Trait is one of such features.

24. Can the value of a constant change during the script’s execution?

Ans:

No, the value of a constant cannot be changed once it’s declared during the PHP execution.

25. Can you extend a Final defined class?

Ans:

No, you cannot extend a Final defined class. A Final class or method declaration prevents child class or method overriding.

26.What are the __construct() and __destruct() methods in a PHP class?

Ans:

All objects in PHP have Constructor and Destructor methods built-in. The Constructor method is called immediately after a new instance of the class is being created, and it’s used to initialize class properties. The Destructor method takes no parameters.

Understanding these two in PHP means that the candidate knows the very basics of OOP in PHP.

27. How we can get the number of elements in an array?

Ans:

The count() function is used to return the number of elements in an array.

28. How would you declare a function that receives one parameter name hello?

Ans:

If hello is true, then the function must print hello, but if the function doesn’t receive hello or hello is false the function must print bye.

29.

function showMessage($hello=false){
 echo ($hello)?'hello':'bye';
}
?>

 

Ans:

In this question, you can evaluate if the developer knows how to declare a function and how they would manage the fact of the parameter can or cannot be on the function call. You can also evaluate if the developer knows the if syntax and how to print text(echo function).

30. The value of the variable input is a string 1,2,3,4,5,6,7. How would you get the sum of the integers contained inside input?

echo array_sum(explode(',',$input));
?>

 

Ans:

The explode function is one of the most used functions in PHP, so it’s important to know if the developer knows this function. There is no unique answer to this question, but the answer must be similar to this one.


 

31. Suppose you receive a form submitted by a post to subscribe to a newsletter. This form has only one field, an input text field named email. How would you validate whether the field is empty? Print a message “The email cannot be empty” in this case.

if(empty($_POST['email'])){
echo "The email cannot be empty";
}
?>

Ans:

In this question, the candidate should be evaluated on his/her knowledge about forms management and validation. There is not unique answer for this question, but it must be similar to this one.

32. Suppose that you have to implement a class named Dragonball. This class must have an attribute named ballCount (which starts from 0) and a method iFoundaBall. When iFoundaBall is called, ballCount is increased by one. If the value of ballCount is equal to seven, then the message You can ask your wish is printed, and ballCount is reset to 0. How would you implement this class?

Ans:

class dragonBall{
private $ballCount;

public function __construct(){
$this->ballCount=0;
}

public function iFoundaBall(){
$this->ballCount++;
if($this->ballCount===7){
echo “You can ask for your wish.”;
$this->ballCount=0;
}
}
}
?>

33.What are the 3 scope levels available in PHP and how would you define them?

Ans:

Private – Visible only in its own class
Public – Visible to any other code accessing the class
Protected – Visible only to classes parent(s) and classes that extend the current class
This is important for any PHP developer to know because it shows an understanding that building applications is more than just being able to write code. One must also have an understanding about privileges and accessibility of that code. There are times protected variables or methods are extremely important, and an understanding of scope is needed to protect the integrity of the data in your application along with provide a clear path through the code.

34. What are getters and setters and why are they important?

Ans:

Getters and setters are methods used to declare or obtain the values of variables, usually private ones. They are important because it allows for a central location that is able to handle data prior to declaring it or returning it to the developer. Within a getter or setter one is able to consistently handle data that will eventually be passed into a variable or additional functions. An example of this would be a user’s name. If a setter is not being used and the developer is just declaring the $userName variable by hand, you could end up with results as such: “kevin”, “KEVIN”, “KeViN”, “”, etc. With a setter, the developer can not only adjust the value, for example, ucfirst($userName), but can also handle situations where the data is not valid such as the example where “” is passed. The same applies to a getter – when the data is being returned, it can be modifyed the results to include strtoupper($userName) for proper formatting further up the chain.
This is important for any developer who is looking to enter a team-based / application development job to know. Getters and setters are often used when dealing with objects, especially ones that will end up in a database or other storage medium. Because PHP is commonly used to build web applications, developers will run across getters and setters in more advanced environments. They are extremely powerful yet not talked about very much. It is impressive if a developer shows that he/she knows what they are and how to use them early on.

35. What does MVC stand for and what does each component do?

Ans:

MVC stands for Model View Controller.
The controller handles data passed to it by the view and also passes data to the view. It’s responsible for interpretation of the data sent by the view and dispersing that data to the appropriate models awaiting results to pass back to the view. Very little, if any business logic should be occurring in the controller.
The model’s job is to handle specific tasks related to a specific area of the application or functionality. Models will communicate directly with your database or other storage system and will handle business logic related to the results.
The view is passed data by the controller and is displayed to the user.
Overall, this question is worth knowing as the MVC design pattern has been used a lot in the last few years and is a very good design pattern to know. Even with more advanced flows that go down to repositories and entities, they still are following the same basic idea for the Controller and View. The Model is typically just split out into multiple components to handle specific tasks related to database data, business logic etc. The MVC design pattern helps draw a better understanding of what is being used, as a whole, in the industry.

36.How does one prevent the following Warning ‘Warning: Cannot modify header information – headers already sent’ and why does it occur in the first place?

Ans:

The candidate should not output anything to the browser before using code that modifies the HTTP headers. Once the developer calls echo or any other code that clears the buffer, the developer can no longer set cookies or headers. That is also true for error messages, so if an error happens before using the header command and the INI directive display_errors is set, then that will also cause that error to show.

37. What are SQL Injections, how do you prevent them and what are the best practices?

Ans:

SQL injections are a method to alter a query in a SQL statement send to the database server. That modified query then might leak information like username/password combinations and can help the intruder to further compromise the server.
To prevent SQL injections, one should always check & escape all user input. In PHP, this is easily forgotten due to the easy access to $_GET & $_POST, and is often forgotten by inexperienced developers. But there are also many other ways that users can manipulate variables used in a SQL query through cookies or even uploaded files (filenames). The only real protection is to use prepared statements everywhere consistently.
Do not use any of the mysql_* functions which have been deprecated since PHP 5.5 ,but rather use PDO, as it allows you to use other servers than MySQL out of the box. mysqli_* are still an option, but there is no real reason nowadays not to use PDO, ODBC or DBA to get real abstraction. Ideally you want to use Doctrine or Propel to get rid of writing SQL queries all together and use object-relational mapping which binds rows from the database to objects in the application.

38. What does the following code output?

$i = 016;
echo $i / 2;

 

Ans:

The Output should be 7. The leading zero indicates an octal number in PHP, so the number evaluates to the decimal number 14 instead to decimal 16.

39. Why would you use === instead of ==?

Ans:

If you would want to check for a certain type, like an integer or boolean, the === will do that exactly like one would expect from a strongly typed language, while == would convert the data temporarily and try to match both operand’s types. The identity operator (===) also performs better as a result of not having to deal with type conversion. Especially when checking variables for true/false, one should avoid using ==as this would also take into account 0/1 or other similar representation.

40. What are PSRs? Choose 1 and briefly describe it.

Ans:

PSRs are PHP Standards Recommendations that aim at standardising common aspects of PHP Development.



 

41.What PSR Standards do you follow? Why would you follow a PSR standard?

Ans:

One should folow a PSR because coding standards often vary between developers and companies. This can cause issues when reviewing or fixing another developer’s code and finding a code structure that is different from yours. A PSR standard can help streamline the expectations of how the code should look, thus cutting down confusion and in some cases, syntax errors.

42. Do you use Composer? If yes, what benefits have you found in it?

Ans:

Using Composer is a tool for dependency management. The candidate can declare the libraries your product relies on and Composer will manage the installation and updating of the libraries. The benefit is a consistent way of managing the libraries depended on so less time is spent managing the libraries.

43. What is PHP?

Ans:

PHP is a web language based on scripts that allows developers to dynamically create generated web pages.

44. What does the initials of PHP stand for?

Ans:

PHP means PHP: Hypertext Preprocessor.

45. Which programming language does PHP resemble to?

Ans:

PHP syntax resembles Perl and C

46. What does PEAR stands for?

Ans:

PEAR means “PHP Extension and Application Repository”. it extends PHP and provides a higher level of programming for web developers.

47. What is the actually used PHP version?

Ans:

Version 5 is the actually used version of PHP.

48. How do you execute a PHP script from the command line?

Ans:

Just use the PHP command line interface (CLI) and specify the file name of the script to be executed as follows:

php script.php

49. How to run the interactive PHP shell from the command line interface?

Ans:
Just use the PHP CLI program with the option -a as follows:

php -a

50.What are the correct and the most two common way to start and finish a PHP block of code?

Ans:
The two most  common ways to start and finish a PHP script are: 

 and <? [— PHP code —] ?>



 

51. How can we display the output directly to the browser?

Ans:

To be able to display the output directly to the browser, we have to use the special tags .

52. What is the main difference between PHP 4 and PHP 5?

Ans:

PHP 5 presents many additional OOP (Object Oriented Programming) features.

53. Is multiple inheritance supported in PHP?

Ans:

PHP includes only single inheritance, it means that a class can be extended from only one single class using the keyword ‘extended’.

54. What is the meaning of a final class and a final method?

Ans:
‘final’ is introduced in PHP5. Final class means that this class cannot be extended and a final method cannot be overrided.

55. How comparison of objects is done in PHP5?

Ans:

We use the operator ‘==’ to test is two object are instanced from the same class and have same attributes and equal values. We can test if two object are refering to the same instance of the same class by the use of the identity operator ‘===’.

56. How can PHP and HTML interact?

Ans:
It is possible to generate HTML through PHP scripts, and it is possible to pass informations from HTML to PHP.

57. What type of operation is needed when passing values through a form or an URL?

Ans:

If we would like to pass values througn a form or an URL then we need to encode and to decode them using htmlspecialchars() and urlencode().

58. How can PHP and Javascript interact?

Ans:

PHP and Javascript cannot directly interacts since PHP is a server side language and Javascript is a client side language. However we can exchange variables since PHP is able to generate Javascript code to be executed by the browser and it is possible to pass specific variables back to PHP via the URL.

59. What is needed to be able to use image function?

Ans:

GD library is needed to be able execute image functions.

60. What is the use of the function ‘imagetypes()’?

Ans:

imagetypes() gives the image format and types supported by the current version of GD-PHP.


 

61. What is the use of the function ‘imagetypes()’?

Ans:

imagetypes() gives the image format and types supported by the current version of GD-PHP.

62. What are the functions to be used to get the image’s properties (size, width and height)?

Ans:

The functions are getimagesize() for size, imagesx() for width and imagesy() for height.

63. How failures in execution are handled with include() and require() functions?

Ans:

If the function require() cannot access to the file then it ends with a fatal error. However, the include() function gives a warning and the PHP script continues to execute.

64. What is the main difference between require() and require_once()?

Ans:

require() and require_once() perform the same task except that the second function checks if the PHP script is already included or not before executing it.

(same for include_once() and include())

65. How can I display text with a PHP script?

Ans:

Two methods are possible:

66. How can we display information of a variable and readable by human with PHP?

Ans:

To be able to display a human-readable result we use print_r().

67. How is it possible to set an infinite execution time for PHP script?

Ans:

The set_time_limit(0) added at the beginning of a script sets to infinite the time of execution to not have the PHP error ‘maximum execution time exceeded’.It is also possible to specify this in the php.ini file.

68. What does the PHP error ‘Parse error in PHP – unexpected T_variable at line x’ means?

Ans:

This is a PHP syntax error expressing that a mistake at the line x stops parsing and executing the program.

69. What should we do to be able to export data into an Excel file?

Ans:

The most common and used way is to get data into a format supported by Excel. For example, it is possible to write a .csv file, to choose for example comma as separator between fields and then to open the file with Excel.

70. What is the function file_get_contents() usefull for?

Ans:

file_get_contents() lets reading a file and storing it in a string variable.



 

71. How can we connect to a MySQL database from a PHP script?

Ans:

To be able to connect to a MySQL database, we must use mysql_connect() function as follows:

72. What is the function mysql_pconnect() usefull for?

Ans:

mysql_pconnect() ensure a persistent connection to the database, it means that the connection do not close when the the PHP script ends.

73. How the result set of Mysql be handled in PHP?

Ans:

The result set can be handled using mysql_fetch_array, mysql_fetch_assoc, mysql_fetch_object or mysql_fetch_row.

74. How is it possible to know the number of rows returned in result set?

Ans:

The function mysql_num_rows() returns the number of rows in a result set.

75. Which function gives us the number of affected entries by a query?

Ans:

mysql_affected_rows() return the number of entries affected by an SQL query.

76. What is the difference between mysql_fetch_object() and mysql_fetch_array()?

Ans:

The mysql_fetch_object() function collects the first single matching record where mysql_fetch_array() collects all matching records from the table in an array.

77. How can we access the data sent through the URL with the GET method?

Ans:

In order to access the data sent via the GET method, we you use $_GET array like this:

www.url.com?var=value
$variable = $_GET[“var”]; this will now contain ‘value’

78. How can we access the data sent through the URL with the POST method?

Ans:

To access the data sent this way, you use the $_POST array.

Imagine you have a form field called ‘var’ on the form, when the user clicks submit to the post form, you can then access the value like this:

$_POST[“var”];

79. How can we check the value of a given variable is a number?

Ans:

It is possible to use the dedicated function, is_numeric() to check whether it is a number or not.

80. How can we check the value of a given variable is alphanumeric?

Ans:
It is possible to use the dedicated function, ctype_alnum to check whether it is an alphanumeric value or not.




 

81. How do I check if a given variable is empty?

Ans:

If we want to check whether a variable has a value or not, it is possible to use the empty() function.

82. What does the unlink() function means?

Ans:
The unlink() function is dedicated for file system handling. It simply deletes the file given as entry.

83. What does the unset() function means?

Ans:

The unset() function is dedicated for variable management. It will make a variable undefined.

84. How do I escape data before storing it into the database?

Ans:

addslashes function enables us to escape data before storage into the database.

85. How is it possible to remove escape characters from a string?

Ans:

The stripslashes function enables us to remove the escape characters before apostrophes in a string.

86. How can we automatically escape incoming data?

Ans:

We have to enable the Magic quotes entry in the configuration file of PHP.

87. What does the function get_magic_quotes_gpc() means?

Ans:

The function get_magic_quotes_gpc() tells us whether the magic quotes is switched on or no.

88. Is it possible to remove the HTML tags from data?

Ans:

The strip_tags() function enables us to clean a string from the HTML tags.

89. what is the static variable in function useful for?

Ans:

A static variable is defined within a function only the first time and its value can be modified during function calls as follows:

90. How can we define a variable accessible in functions of a PHP script?

Ans:

This feature is possible using the global keyword.


 

91. How is it possible to return a value from a function?

Ans:

A function returns a value using the instruction ‘return $value;’.

92. What is the most convenient hashing method to be used to hash passwords?

Ans:

It is preferable to use crypt() which natively supports several hashing algorithms or the function hash() which supports more variants than crypt() rather than using the common hashing algorithms such as md5, sha1 or sha256 because they are conceived to be fast. hence, hashing passwords with these algorithms can vulnerability.

93. Which cryptographic extension provide generation and verification of digital signatures?

Ans:

The PHP-openssl extension provides several cryptographic operations including generation and verification of digital signatures.

94. How a constant is defined in a PHP script?

Ans:

The define() directive lets us defining a constant as follows:

define (“ACONSTANT”, 123);

95. How can you pass a variable by reference?

Ans:

To be able to pass a variable by reference, we use an ampersand in front of it, as follows $var1 = &$var2

96. Will a comparison of an integer 12 and a string “13” work in PHP?

Ans:

“13” and 12 can be compared in PHP since it casts everything to the integer type.

97. How is it possible to cast types in PHP?

Ans:

The name of the output type have to be specified in parentheses before the variable which is to be cast as follows:

  • (int), (integer) – cast to integer
  • (bool), (boolean) – cast to boolean
  • (float), (double), (real) – cast to float
  • (string) – cast to string
  • (array) – cast to array
  • (object) – cast to object

98. When a conditional statement is ended with an endif?

Ans:

When the original if was followed by : and then the code block without braces.

99. How is the ternary conditional operator used in PHP?

Ans:

It is composed of three expressions: a condition, and two operands describing what instruction should be performed when the specified condition is true or false as follows:

Expression_1 ? Expression_2 : Expression_3;

100. What is the function func_num_args() used for?

Ans:

The function func_num_args() is used to give the number of parameters passed into a function.



 

101. If the variable $var1 is set to 10 and the $var2 is set to the character var1, what’s the value of $var2?

Ans:

$var2 contains the value 10.

102. What does accessing a class via :: means?

Ans:

:: is used to access static methods that do not require object initialization.

103. In PHP, objects are they passed by value or by reference?

Ans:

In PHP, objects passed by value.

104. Are Parent constructors called implicitly inside a class constructor?

Ans:

No, a parent constructor have to be called explicitly as follows:

parent::constructor($value)

105. What’s the difference between __sleep and __wakeup?

Ans:

__sleep returns the array of all the variables that need to be saved, while __wakeup retrieves them.

106. What is faster?

Ans:

1- Combining two variables as follows:

$variable1 = ‘Hello ‘;

$variable2 = ‘World’;

$variable3 = $variable1.$variable2;

Or

2- $variable3 = “$variable1$variable2”;

$variable3 will contain “Hello World”. The first code is faster than the second code especially for large large sets of data.

107. What is the definition of a session?

Ans:

A session is a logical object enabling us to preserve temporary data across multiple PHP pages.

108. How to initiate a session in PHP?

Ans:

The use of the function session_start() lets us activating a session.

109. How is it possible to propagate a session id?

Ans:

It is possible to propagate a session id via cookies or URL parameters.

110. What is the meaning of a Persistent Cookie?

Ans:

A persistent cookie is permanently stored in a cookie file on the browser’s computer. By default, cookies are temporary and are erased if we close the browser.




 

111. When sessions ends?

Ans:

Sessions automatically ends when the PHP script finishs executing, but can be manually ended using the session_write_close().

112. What is the difference between session_unregister() and session_unset()?

Ans:

The session_unregister() function unregister a global variable from the current session and the session_unset() function free all session variables.

113. What does $GLOBALS means?

Ans:

$GLOBALS is associative array including references to all variables which are currently defined in the global scope of the script.

114. What does $_SERVER means?

Ans:

$_SERVER is an array including information created by the web server such as paths, headers, and script locations.

115. What does $_FILES means?

Ans:

$_FILES is an associative array composed of items sent to the current script via the HTTP POST method.

116. What is the difference between $_FILES[‘userfile’][‘name’] and $_FILES[‘userfile’][‘tmp_name’]?

Ans:

$_FILES[‘userfile’][‘name’] represents the original name of the file on the client machine,

$_FILES[‘userfile’][‘tmp_name’] represents the temporary filename of the file stored on the server.

117. How can we get the error when there is a problem to upload a file?

Ans:

$_FILES[‘userfile’][‘error’] contains the error code associated with the uploaded file.

118. How can we change the maximum size of the files to be uploaded?

Ans:

We can change the maximum size of files to be uploaded by changing upload_max_filesize in php.ini.

119. What does $_ENV means?

Ans:

$_ENV is an associative array of variables sent to the current PHP script via the environment method.

120. What does $_COOKIE means?

Ans:

$_COOKIE is an associative array of variables sent to the current PHP script using the HTTP Cookies.


 

121. What does the scope of variables means?

Ans:

The scope of a variable is the context within which it is defined. For the most part all PHP variables only have a single scope. This single scope spans included and required files as well.

122. what the difference between the ‘BITWISE AND’ operator and the ‘LOGICAL AND’ operator?

Ans:

$a and $b : TRUE if both $a and $b are TRUE.

$a & $b     : Bits that are set in both $a and $b are set.

123. What are the two main string operators?

Ans:

The first is the concatenation operator (‘.’), which returns the concatenation of its right and left arguments. The second is (‘.=’), which appends the argument on the right to the argument on the left.

124. What does the array operator ‘===’ means?

Ans:

$a === $b TRUE if $a and $b have the same key/value pairs in the same order and of the same types.

125. What is the differences between $a != $b and $a !== $b?

Ans:

!= means inequality (TRUE if $a is not equal to $b) and !== means non-identity (TRUE if $a is not identical to $b).

126. How can we determine whether a PHP variable is an instantiated object of a certain class?

Ans:

To be able to verify whether a PHP variable is an instantiated object of a certain class we use instanceof.

127. What is the goto statement useful for?

Ans:

The goto statement can be placed to enable jumping inside the PHP program. The target is pointed by a label followed by a colon, and the instruction is specified as a goto statement followed by the desired target label.

128. what is the difference between Exception::getMessage and Exception::getLine ?

Ans:

Exception::getMessage lets us getting the Exception message and Exception::getLine lets us getting the line in which the exception occurred.

129. What does the expression Exception::__toString means?

Ans:

Exception::__toString gives the String representation of the exception.

130. How is it possible to parse a configuration file?

Ans:

The function parse_ini_file() enables us to load in the ini file specified in filename, and returns the settings in it in an associative array.



 

131. How can we determine whether a variable is set?

Ans:

The boolean function isset determines if a variable is set and is not NULL.

132. What is the difference between the functions strstr() and stristr()?

Ans:

The string function strstr(string allString, string occ) returns part of allString from the first occurrence of occ to the end of allString. This function is case-sensitive. stristr() is identical to strstr() except that it is case insensitive.

133. what is the difference between for and foreach?

Ans:

for is expressed as follows:

for (expr1; expr2; expr3)

statement

The first expression is executed once at the beginning. In each iteration, expr2 is evaluated. If it is TRUE, the loop continues and the statements inside for are executed. If it evaluates to FALSE, the execution of the loop ends. expr3 is tested at the end of each iteration.

However, foreach provides an easy way to iterate over arrays and it is only used with arrays and objects.

134. Is it possible to submit a form with a dedicated button?

Ans:

It is possible to use the document.form.submit() function to submit the form. For example:

135. What is the difference between ereg_replace() and eregi_replace()?

Ans:

The function eregi_replace() is identical to the function ereg_replace() except that it ignores case distinction when matching alphabetic characters.

136. Is it possible to protect special characters in a query string?

Ans:

Yes, we use the urlencode() function to be able to protect special characters.

137. What are the three classes of errors that can occur in PHP?

Ans:

The three basic classes of errors are notices (non-critical), warnings (serious errors) and fatal errors (critical errors).

138. What is the difference between characters \034 and \x34?

Ans:

\034 is octal 34 and \x34 is hex 34.

139. How can we pass the variable through the navigation between the pages?

Ans:

It is possible to pass the variables between the PHP pages using sessions, cookies or hidden form fields.

140. Is it possible to extend the execution time of a php script?

Ans:

The use of the set_time_limit(int seconds) enables us to extend the execution time of a php script. The default limit is 30 seconds.




 

141. Is it possible to destroy a cookie?

Ans:

Yes, it is possible by setting the cookie with a past expiration time.

142. What is the default session time in php?

Ans:

The default session time in php is until closing of browser

143. Is it possible to use COM component in PHP?

Ans:

Yes, it’s possible to integrate (Distributed) Component Object Model components ((D)COM) in PHP scripts which is provided as a framework.

144. Explain whether it is possible to share a single instance of a Memcache between multiple PHP projects?

Ans:

Yes, it is possible to share a single instance of Memcache between multiple projects. Memcache is a memory store space, and you can run memcache on one or more servers. You can also configure your client to speak to a particular set of instances. So, you can run two different Memcache processes on the same host and yet they are completely independent. Unless, if you have partitioned your data, then it becomes necessary to know from which instance to get the data from or to put into.

145. Explain how you can update Memcached when you make changes to PHP?

Ans:

When PHP changes you can update Memcached by

  • Clearing the Cache proactively: Clearing the cache when an insert or update is made.
  • Resetting the Cache: It is similar to the first method but rather than just deleting the keys and waiting for the next request for the data to refresh the cache, reset the values after the insert or update.