<<

PHP Interview Questions and Answers

PHP or Hypertext Preprocessor is a general-purpose programming language used for web development. It can perform any task related to server-side programming. A PHP file consists of tags, and it ends with the extension “..” PHP code can be embedded into HTML code. It can also be used with a web content management system, web frameworks, and web template systems.

PHP is a simple language to work on, even a new user can work on it. At the same time, it offers multiple advanced features for professional developers. We can develop efficient codes using its distinctive features.

Being a popular programming language, many organizations look out for skilled and experienced professionals. This article lists some of the most popularly asked PHP interview questions. Let’s take a look –

Q1. What are the features of PHP?

Ans. PHP has some unique features, which include:

 Open-source  Platform Independent  Case sensitive  Flexibility  Efficiency  Third-party application support and security  Interpreted

Q2. Why do we use PHP?

Ans. There are various reasons to use PHP for server-side programming:

 It is a free language with no licensing fees; the cost of using it is minimal.  It supports multiple databases, including MySQL that is also a free source.  It is easier to create a website

Q3. Is PHP a loosely typed language?

Ans. Yes, it is a loosely typed language. It is not required to declare a variable type to declare a variable.

In PHP, if you declare a variable, then you don’t have to worry about what type of data would be stored in that variable. If you declare a variable called $String then it is not necessary to assign a string value, you can assign an integer value also.

Example:

$var=”Hello”: //String

$var= 20; //Integer

Q4. What are the different PHP Framework?

Ans. Following are the popular frameworks:

 CodeIgniter  2  CakePHP  Zend Framework   PHPixie  FuelPHP

Q5. What are the characteristics of PHP variables?

Ans. Following are the characteristics:

 Each variable is denoted by a dollar sign ($).  The value of a variable is the value of its most recent assignment.  Variables are assigned with “=” operator, where the variable is on the left-hand side and expression is on the right-hand side.  Variables can be but do not need to be declared before the assignment.  Variables do not have an intrinsic type; it does not know its variable type and can be assigned with any variable type.  Variables that are used in the code before they are assigned, have a default value.  PHP variables are -like.

Q6. What does PEAR mean?

Ans. PEAR stands for “PHP Extension and Application Repository”. PEAR provides a structured of open- to the PHP users.

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

Ans. By using the command-line interface (CLI) and specify the file name that is to be executed.

Syntax: php script.php

Q8. What is the difference between static and dynamic website?

Ans. Static web pages are written in HTML, JavaScript, CSS, etc. In static web pages, when a server sends a request to the web page the server sends the response to the client. After that, the web pages are visible through the web browser. In static web pages, the web pages are still the same until somebody changes it manually.

Dynamic pages are written in languages such as AJAX, ASP.NET, CGI, etc. There are different content for different visitors on dynamic pages. They are a bit complicated as compared to static pages, as information changes frequently in dynamic pages.

Q9. Explain the main differences between PHP 4 and PHP 5.

Ans. The main difference between PHP 4 and PHP 5 are:

PHP 4 PHP 5

● PHP 5 supports the OOPs concept and ● PHP 4 does not support the OOPs 2 is used concept and Zend engine 1 is used ● Objects are passed by reference in PHP ● Objects are passed by value in PHP 4 5

● It does not allow static methods and ● It allows static methods and properties properties in a class in a class

Q10. What is the difference between echo and print?

Ans. Both echo and print methods are used to print the output in the browser, but the difference between them is echo does not return any value after printing the output, and it is also faster than the print method. The print method is slower because it returns the Boolean value after printing the output.

Syntax:

 echo “PHP Developer”;  $n = print “ Developer”;

Q11. How can we execute PHP script using the command line?

Ans. PHP command is used in the command line to execute a PHP script. If you are using the file name, i.e. test.php the php test.phpcommand is used to run the script from the command line.

Q12. Mention the popular Content Management Systems (CMS) in PHP?

Ans. Following are some popular Content Management Systems (CMS):

 WordPress  Shopify   Wix  Magento

Q13. How to declare an array?

Ans. There are three types of arrays that you can declare i.e. numeric, multidimensional and associative arrays.

1. 1. 1. //Numeric

Array

1. 1. 2. $computer = array(“Dell”,

“Lenavo”, “HP”);

1. 1. 3. //Associative

Array

1. 1. 4. $color = array(“Sithi”=>”Red”,

“Amit”=>”Blue”, “Mahek”=>”Green”);

1. 1. 5. //Multidimensional

Array

1. 1. 6. $courses = array ( array(“PHP”,50), array(“JQuery”,15), array(“AngularJS”,20) );

Q14. Mention some of the constants in PHP and their purpose?

Ans. Following are some constants:

 _LINE_: The current line number of the file.  _FILE_: It represents the full path and file name of the file. If used inside an include, it will return the name of the included file.  _FUNCTION_: It returns the function name.  _CLASS_: It returns the class name as it was declared.  _METHOD_: It represents the method of the class.

Q15. Explain the uses of explode() and implode() function?

Ans. The explode() function is used to split by a specified string into pieces i.e. it breaks a string into an array.

Example: If you have a string

$str = “JOHN”:

Now, divide string into an array

$arr = explode(“,”, $str);

“,” will separate the string into an array

And put the resulting array into the variable “arr”

Use print_r ($arr); to print the array

Array(

[0] => J

[1] =>O

[2] => H

[3] => N

) which is equal to:

$arr = Array (“J”,”O”,”H”,”N”)

An implode() function is used to join the elements of an array to string. It returns a string from an array and it is remembered as “array to string”

Take an array like this $arr = Array (“J”,”O”,”H”,”N”);

Convert it into a string by putting the separator ‘-‘ between each element of the array.

$str = implode(“-“,$arr);

So your resulting string variable $str will contain:

J-O-H-N

Syntax:

$text = “She is beautiful”;

print_r (explode(” “,$text));

$strarr = array(‘Pen’,’Pencil’,’Eraser’); echo implode(” “,$strarr);

Q15. Explain the PHP $ and $$ variables?

Ans. The “$” variable is a standard variable that stores any value such as string, float, integer, etc.

The “$$” variable is used to store the value of $variable inside it.

$name=”Joe”;

${$name}=”Merry”;

${${$name}}=”Dolcie”; echo $name. “
”; echo ${$name}. “
”; echo $Cat. “
”; echo ${${$name}}. “
”; echo $Dog. “
”;

?>

Output:

Joe

Merry

Merry

Dolcie

Dolcie

Q16. Explain the difference between GET and POST methods?

Ans. There are two ways to send information via browser client to the web server:

 The GET method  The POST method

GET method POST method

● The POST method has no restriction to ● The GET method produces a long string any data size to be sent that appears in the server log ● It can be used to send both binary data ● It is restricted to send only 1024 as well as ASCII data characters ● In this method, security depends on the ● It is not used to send binary data such HTTP protocol because the data sent by the as word document or images to the server POST method goes through an HTTP header.

● $_GET is used to access all the sent ● $_POST associative array is provided by information using GET method the PHP to access all the sent information using POST method

Q17. Explain type casting and type juggling?

Ans. Type casting is defined as the assigning particular type of data type to the variable.

Example:

$foo = ‘1’; echo gettype($foo); // outputs ‘string’ settype($foo, ‘integer’); echo gettype($foo); // outputs ‘integer’

In type juggling the variable type is changing automatically with the value assigned to the variable, as PHP does not support variable declaration.

Example:

$foo = “0”; // $foo is string (ASCII 48)

$foo += 2; // $foo is now an integer (2)

$foo = $foo + 1.3; // $foo is now a float (3.3)

?>

Q18. Can you extend a Final defined class?

Ans. No, You cannot extend Final method. The final class prevents child class and method overloading.

Example of Final method:

} final public function moreTesting() { echo “ParentClass::moreTesting() called\n”;

}

} class ChildClass extends ParentClass { public function moreTesting() { echo “ChildClass::moreTesting() called\n”;

}

}

// Results in Fatal error: Cannot override final method ParentClass::moreTesting()

?>

Q19. How to make a connection with MY SQL server?

Ans. If you want to make a connection with MS SQL server, then you have to provide MS SQL hostname, username, and password in mysqli_connect() method.

Example:

$serverName = “serverName\\sqlexpress”; //serverName\instanceName

$connectionInfo = array( “Database”=>”dbName”, “UID”=>”userName”, “PWD”=>”password”);

$conn = sqlsrv_connect( $serverName, $connectionInfo); if( $conn ) { echo “Connection established.
”;

}else{ echo “Connection could not be established.
”; die( print_r( sqlsrv_errors(), true));

}

?>

Q20. Explain the difference between mysqli_connect and mysqli_pconnect?

Ans. mysqli_connect () function looks for the existing persistence connection, and if it does not find the existing connection, then it will create a new database connection and terminate the connection at the end of the script.

Example:

$DBconnection = mysqli_connect(“localhost”,”username”,”password”,”dbname”);

// Check for valid connection if (mysqli_connect_errno())

{ echo “Unable to connect with MySQL: ” . mysqli_connect_error();

}

mysqli_pconnect() function is diminished in the new version of PHP but it is possible to create a persistent connection by using mysqli_connect() function with the prefix p.

Q21. How do we access the data transfer through the URL with the POST method?

Ans. To access the data transfer through the URL, we need to use the $_POST array.

Let’s assume we have a form field named ‘var’ on the form when the user clicks submit to the post form, then it is easy to access the value:

$_POST[“var”];

Q22. What is the process to check if a given variable is empty?

Ans. We can use the empty() function to check whether a variable is empty or not.

Q23. Explain the unlink() function in PHP?

Ans. The unlink() function is used for file system handling. It also deletes the file started as the entry.

Q24. Explain what does the unset() function mean?

Ans. The unset() function is introduced for variable management. It helps to make a variable undefined.

Q25. What is the process to automatically run off incoming data?

Ans. If we want to run off incoming data automatically, We have to allow the Magic quotes entry in the configuration file of PHP.

Q26. Describe what does the function get_magic_quotes_gpc() means?

Ans. In PHP, the function get_magic_quotes_gpc() is used to describes us whether the magic quotes is switched on or not.

Q27. Can we remove the HTML tags from data?

Ans. Yes, we can, the strip_tags() function allows us to clean a string from the HTML tags.

Q28. What is the procedure to cast types in PHP?

Ans. The name of the output type has to be defined 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

* (object) – cast to object

* (array) – cast to array

* (string) – cast to string

Q29. If the variable $var0 contains the value 500 and the $var1 is set to the character var0, what’s the value of $$var1?

Ans. $$var1 contains the value 500.

Q30. Explain what does accessing a class via :: means?

Ans. In PHP, :: is used to access static methods where object initialization is not required.

Q31. In PHP, objects use passed by value or passed by reference?

Ans. Objects used passed by value in PHP.

Q32. Which given code works faster?

Ans. Combining two variables as follows:

$variable1 = ‘Naukri’;

$variable2 = ‘Learning’;

$variable3 = $variable1.$variable2;

Or

$variable3 = “$variable1$variable2”;

$variable3 will contain “Naukri Learning”. The first code works faster than the second code.

Q33. What is the possible way to generate a session id?

Ans. We can generate a session id using cookies or URL parameters.

Q34. Explain what Persistent Cookie is?

Ans. In PHP, a persistent cookie is gathered in a cookie file on the browser’s computer. Cookies are automatically stored and deleted while we close browse.

Q35. At what phase sessions end in PHP?

Ans. Sessions end automatically when the PHP script completed with execution but can be manually ended using the session_write_close().

Q36. Explain what $GLOBALS is?

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

Q37. What does $_SERVER mean?

Ans. $_SERVER is an array, including data created by the webserver such as paths, headers, and script locations.

Q38. What does $_FILES mean?

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

Visit Naukri Learning website for more