Php Interview Questions And Answers

php interview questions and answers   Top 32 Php Interview Questions And Answers

1) What is PHP?

PHP is HyperText Preprocessor. It is an open source programming language which is used for server side web development.

● The key feature of PHP is that it supports multiple databases like mysql, oracle, sybase, postgreSQL etc.
● Also PHP is an object oriented language.
● It can be easily embedded in HTML and scripts.


2) Which are the different frameworks in PHP?

Some of the popular and commonly used PHP frameworks are:

CakePHP : It is an open source web framework written in PHP. It follows the model-view-controller concept, and distributed under MIT licence.
Its repository is on GitHub : https://github.com/cakephp/cakephp.

CodeIgniter : It is an open source web framework written in PHP. It follows the model-view-controller concept, and distributed under MIT licence. While controller classes are a necessary part of development under CodeIgniter, models and views are optional.
Its repository is on GitHub : https://github.com/bcit-ci/CodeIgniter.

Symfony : It is an open source web framework written in PHP. It is distributed under MIT licence. It is a set of reusable PHP components/libraries. Symfony has a low performance overhead used with a bytecode cache.
Its repository is on GitHub : https://github.com/symfony/symfony.

Zend Framework : It is an open source web framework written in PHP 7. It is distributed under BSD licence. The framework uses various packages by the use of Composer as part of its package dependency managers.
Its repository is on GitHub : https://github.com/zendframework/zendframework.


3) What is echo in PHP?

Echo is used to output one or more strings in PHP.

● Echo don't have return value.
● We can pass multiple strings in echo which can be comma separated.
● Echo is faster then print statement.

Here is sample code snippet of echo():

Sample 1:

<?php 
echo "Hello world!"; 
?>

Sample 2:
<?php
$str = "Hello world!";
echo $str;
?>

Sample 3:
<?php
$str1="Hello world!";
$str2="What a nice day!";
echo $str1 . " " . $str2;
?> 

Sample 4:
<?php
echo 'This ','string ','was ','made ','with multiple parameters.';
?> 


4) In PHP, what are the key difference between echo and print?

Following are the key differences between echo and print:

● Echo don't return any value whereas print do. And print always return value 1.
● Echo is faster whereas print is slow.
● In echo we can pass multiple strings separated by commas whereas in print we cant pass multiple strings.

The reason why echo is faster than print is that it doesn't return any value.


5) What is the difference between GET and POST methods?

GET method can transfer max of 1024 bytes whereas POST can send large chunks of data.
POST method is much secure then GET method.


6) what is the variable in PHP?

A variable in PHP is the temporary storage location which holds data. We don't need to declare the variable explicitly as string, integer or float.
The values can be directly assigned to the variables. $s="hello";
$x=10;
$y=4.6;

The variable name should be start with letter or underscore. It cant start with the numbers or special characters.
Variables can be added without declaring there type.


7) How to make variables constant in PHP?

In PHP following are the ways to define the variables as constant:

● Define function e.g. define("variable","constant val");
● Const keyword can be use with the variable name e.g. const $a = 10;


8) In PHP, what are the magic constant?

Magic constants are the pre-defined constants in PHP which starts and ends with double underscore. These are case-insensitive. Some of these are __LINE__, __FILE__, __DIR__, __CLASS__, __FUNCTION__ etc.


9) In PHP, what are the different data types?

In PHP following are the data types:

Scalar data types - integer, boolean, string, float
Compound data types - array, object
Special data types - resource, NULL


10) In PHP, what are arrays?

Similar to other programming languages, PHP also has arrays, these are multiple values of same type. Following are the different type of arrays:

Indexed Array: There index is represented via a $a[0] = 1
Associative Array: Associate name with each array element e.g. $sal["Son"]="3500";
Multidimensional Array: These are the arrays of array e.g. $emp[$r][$c]


11) What are the key string functions in PHP?

Following are the key functions of string in PHP:

strtolower() : This function convers the string to lowecase.

strtoupper() : This function convers the string to uppercase.

ucfirst() : This function converts the first character of a string to uppercase.

lcfirst() : This function converts the first character of a string to lowercase

ucwords(): This function converts the first character of each word in a string to uppercase.

strrev() : This function reverses a string.

strlen() : This function returns the length of a string. Here is the sample code snippet:

<?php
echo strlen("Hello world!"); // outputs 12
?>


12) What are the different ways to include file in PHP?

There are two ways to include files in PHP:
● include
● require

● The biggest advantage of including files is the re-usability of the code.
● Upon failure require produces a fatal error (E_COMPILE_ERROR) and stop the script.
● Whereas upon failure include only produce a warning (E_WARNING) and the script will continue.
● Including files saves a lot of work. This means that you can create a standard header, footer, or menu file for all your web pages. Then, when the header needs to be updated, you can only update the header include file.
● Here is the syntax of both the types:

include 'filename';

or

require 'filename';


13) What is the difference between include and require in PHP?

If file is not found then include will send warning whereas require send fatal error.


14) What is session in PHP?

Session in used to temporary store the data so that it can be transferred across different PHP pages. Session_start() function is used to start or resume the session.

session_start();

$_SESSION[""tmp""] = ""Scale"";

echo $_SESSION[""tmp""];

It is good practice to destroy all the sessions after using them in the program. This can be done by using below mentioned function:
session_destroy();


15) What is setcookie() function in PHP?

This function is used to set cookie in PHP which can then be accessed globally by using $_COOKIE. setcookie(""name"", ""abc""); $_COOKIE[""name""];


16) What is the difference between $ and $$ variables in PHP?

In PHP, $var is the variable that stores value like float, integer, string etc. whereas $$var is the reference variable that stores the value of other variable.


17) In PHP, what are the default argument value functions?

These are the functions which allows to define default value such that if no argument value passes then it will take default value.
eg function add($x=2){}


18) In PHP, what are the variable length argument value functions?

These are the functions in PHP which allows to pass n number of arguments in function. Here is the sample example:
function add(...$x){}


19) Describe file handling in PHP?

In PHP we can do following operations in file - read, write, append, delete, create and close file. Fopen() - To open file in different modes like read-only, write-only, read-write etc. fclose() - To close file fread() - To read file fwrite() - To write onto the file unlink() - This is used to delete file


20) What is $_FILES in PHP?

This is global file contain all the file information such as type, size and error association. $_FILES['filename']['name']
$_FILES['filename']['type']


21) How to connect to database in PHP?

mysqli_connect() function is used to connect to MYSQL database. Below is the sytanx to connect: mysqli_connect (server, username, password)

mysqli_close() function is used to disconnect from MYSQL database.


22) How to execute SQL query in PHP?

mysqli_query($conn,$sql) function is used to execute SQL query. Here $conn is the connection object that we have got in return of mysqli_connect(). $sql is the SQL query that you want to execute.


23) In PHP, what is in_array() function?

This function is used to check if there is any value exits in array.


24) What is explode() function in PHP?

Explode function breaks the string into an array. Here is the syntax:
explode ( string $delimiter , string $string [, int $limit ] );


25) What is implode() function in PHP?

Implode function joins the elements of array and returns string. Here is the syntax:
implode(separator,array)


26) What are the PHP filters?

In PHP filters are used to validate and filter the data.

Few of the predefined filter constants are:
FILTER_VALIDATE_BOOLEAN - Validates boolean
FILTER_VALIDATE_EMAIL - Validates email address
FILTER_VALIDATE_URL - Validates URL


27) In PHP, what is FILTER_SANITIZE_EMAIL?

This is predefined filter constant which is being used to remove all the illegal characters from the email address.

Example:
$email = "[email protected]//.com";
$email = filter_var($email, FILTER_SANITIZE_EMAIL);

The output is : [email protected]


28) In PHP, what is FILTER_SANITIZE_SPECIAL_CHARS?

This is predefined filter constant which is being used to remove the special characters.


29) In PHP, what is FILTER_VALIDATE_IP?

This is predefined filter constant which is being used to validate an IP address. This enables to detect the valid or invalid IP addresses.


30) In PHP, what is die() function?

Die() function is used to handle the error if something goes wrong in the code. Example:

if(! $retval ) {
    die('Could not get data: ' . mysqli_error());
  }


31) In PHP, which file to use to setup RewriteRules?

The file where we define RewriteRule is .htaccess.


32) In PHP, how to rewrite the url for html web pages?

Here is the code snippet which need to define in .htaccess to rewrite the url for html pages:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]