PHP Interview Questions

There is given PHP interview questions and answers that have been asked in many companies. Let's see the list of top PHP interview questions.

What is meant by public, private, protected, static and final scopes?

  • Public– Variables, classes, and methods which are declared public can be accessed from anywhere.
  • Private– Variables, classes and methods which are declared private can be accessed by the parent class only.
  • Protected– Variables, classes, and methods which are declared protected can be accessed by the parent and child classes only.
  • Static– The variable which is declared static can keep the value after losing the scope.
  • Final– This scope prevents the child class to declare the same item again.
.

Which PHP global variable is used for uploading a file?

$_FILE[] array contains all the information of an uploaded file. The use of various indexes of this array is mentioned below:

  • $_FILES[$fieldName][‘name’] – Keeps the original file name.
  • $_FILES[$fieldName][‘type’] – Keeps the file type of an uploaded file.
  • $_FILES[$fieldName][‘size’] – Stores the file size in bytes.
  • $_FILES[$fieldName][‘tmp_name’] – Keeps the temporary file name which is used to store the file in the server.
  • $_FILES[$fieldName][‘error’] – Contains error code related to the error that appears during the upload.
.

Difference between array_combine and array_merge?

array_combine is used to combine two or more arrays while array_merge is used to append one array at the end of another array.

array_combine is used to create a new array having keys of one array and values of another array that are combined with each other whereas array_merge is used to create a new array in such a way that values of the second array append at the end of the first array.

array_combine doesn’t override the values of the first array but in array_merge values of the first array overrides with the values of the second one.

Example of array_combine

<?php
$arr1    = array("sub1","sub2","sub3");
$arr2    = array(("php","html","css");
$new_arr = array_combine($arr1, $arr2);
print_r($new_arr);
?>

OUTPUT:

 Array([sub1] => php [sub2] => html [sub3 =>css)
Example of array_merge
<?php
$arr1 = array("sub1" => "node", "sub2" => "sql");
$arr2 = array("s1"=>"jQuery", "s3"=>"xml", "sub4"=>"Css");
$result = array_merge($arr1, $arr2);
 print_r($result);
?>

OUTPUT:

 Array ([s1] => jquery [sub2] => sql [s3] => xml [sub4] =>Css )
.

What is difference between ksort() and usort() functions.

  • ksort() function is used to sort an array according to its key values whereas asort() function is used to sort an array according to its values.
  • They both used to sort an associative array in PHP.

Example of asort():

<?php
$age = array("Peter"=>"37", "Ben"=>"27", "Joe"=>"43");
asort($age);
?>

Output: Key=Ben, Value=37 Key=Joe, Value=43 Key=Peter, Value=35

Example of ksort():

<?php
$age = array("Peter"=>"37", "Ben"=>"27", "Joe"=>"43");
ksort($age);
?>

Output: Key=Ben, Value=37

Key=Joe, Value=43
Key=Peter, Value=35
.

What is php.ini & .htacess file?

Both are used to make the changes to your PHP setting. These are explained below: php.ini: It is a special file in PHP where you make changes to your PHP settings. It works when you run PHP as CGI. It depends on you whether you want to use the default settings or changes the setting by editing a php.ini file or, making a new text file and save it as php.ini. .htaccess: It is a special file that you can use to manage/change the behavior of your site. It works when PHP is installed as an Apache module. These changes include such as redirecting your domain’s page to https or www, directing all users to one page, etc..

Explain preg_Match and preg_replace?

These are the commonly used regular expressions in PHP. These are an inbuilt function that is used to work with other regular functions. preg-Match: This is the function used to match a pattern in a defined string. If the patterns match with string, it returns true otherwise it returns false. Preg_replace: This function is used to perform a replace operation. In this, it first matches the pattern in the string and if pattern matched, ten replace that match with the specified pattern..

How to check curl is enabled or not in PHP

use function_exists(‘curl_version’) function to check curl is enabled or not. This function returns true if curl is enabled other false

Example :

if(function_exists('curl_version') ){
  echo "Curl is enabled";
}else{

echo "Curl is not enabled";

}
.

What is GD PHP?

GD is an open source library for creating dynamic images.

  • PHP uses GD library to create PNG, JPEG and GIF images.
  • It is also used for creating charts and graphics on the fly.
  • GD library requires an ANSI C compiler to run.

Sample code to generate an image in PHP

<?php
	header("Content-type: image/png");

	$string = $_GET['text'];
	$im = imagecreatefrompng("images/button1.png");
	$mongo = imagecolorallocate($im, 220, 210, 60);
	$px = (imagesx($im) - 7.5 * strlen($string)) / 2;
	imagestring($im, 3, $px, 9, $string, $mongo);
	imagepng($im);

	imagedestroy($im);
?>
.

What is PECL?

PECL is an online directory or repository for all known PHP extensions. It also provides hosting facilities for downloading and development of PHP extensions. You can read More about PECL from https://pecl.php.net/.

What is the use of Mbstring?

Mbstring

Mbstring is an extension used in PHP to handle non-ASCII strings. Mbstring provides multibyte specific string functions that help us to deal with multibyte encodings in PHP. Multibyte character encoding schemes are used to express more than 256 characters in the regular byte-wise coding system. Mbstring is designed to handle Unicode-based encodings such as UTF-8 and UCS-2 and many single-byte encodings for convenience PHP Character Encoding Requirements.

Below are some features of mbstring

  • It handles the character encoding conversion between the possible encoding pairs.
  • Offers automatic encoding conversion between the possible encoding pairs.
  • Supports function overloading feature which enables to add multibyte awareness to regular string functions.
  • Provides multibyte specific string functions that properly detect the beginning or ending of a multibyte character. For example, mb_strlen() and mb_split()
.