PHP Interview Questions

PHP Interview Questions Download PHP Interview Questions PDF

PHP Interview Questions

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 )

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.

To terminate the script in PHP, exit() function is used. It is an inbuilt function which outputs a message and then terminates the current script. The message which is you want to display is passed as a parameter to the exit () function and this function terminates the script after displaying the message. It is an alias function of die () function. It doesn’t return any value.

Syntax: exit(message)

Where massage is a parameter to be passed as an argument. It defines message or status.

Exceptions of exit():

  • If no status is passed to exit(), then it can be called without parenthesis.
  • If a passed status is an integer then the value will not be printed but used as the exit status.
  • The range of exit status is from 0 to 254 where 255 is reserved in PHP.

Errors And Exceptions

  • exit() can be called without parentheses if no status is passed to it. Exit() function is also known by the term language construct.
  • If the status passed to an exit() is an integer as a parameter, that value will be used as the exit status and not be displayed.
  • The range of exit status should be in between 0 to 25. the exit status 255 should not be used because it is reserved by PHP.

 

Both MD5 and SHA256 are used as hashing algorithms. They take an input file and generate an output which can be of 256/128-bit size. This output represents a checksum or hash value. As, collisions are very rare between hash values, so no encryption takes place.

  • The difference between MD5 and SHA256 is that the former takes less time to calculate than later one.
  • SHA256 is difficult to handle than MD5 because of its size.
  • SHA256 is less secure than MD5
  • MD5 result in an output of 128 bits whereas SHA256 result output of 256 bits.

Concluding all points, it will be better to use MDA5 if you want to secure your files otherwise you can use SHA256.

Heredoc and nowdoc are the methods to define the string in PHP in different ways.

  • Heredoc process the $variable and special character while nowdoc doesn't do the same.
  • Heredoc string uses double quotes "" while nowdoc string uses single quote ''
  • Parsing is done in heredoc but not in nowdoc.

Path Traversal also is known as Directory Traversal is referring to an attack which is attacked by an attacker to read into the files of the web application. Also, he/she can reveal the content of the file outside the root directory of a web server or any application. Path traversal operates the web application file with the use of dot-dot-slash (../) sequences, as ../ is a cross-platform symbol to go up in the directory.

Path traversal basically implements by an attacker when he/she wants to gain secret passwords, access token or other information stored in the files. Path traversal attack allows the attacker to exploit vulnerabilities present in web file.

Floyd’s triangle is the right-angled triangle which starts with 1 and filled its rows with a consecutive number. The count of elements in next row will increment by one and the first row contains only one element.

Example of Floyd's triangle having 4 rows

The logic to print Floyd's triangle

<?php

echo "print Floyd's triangle"; echo "<pre>
$key = 1; for ($i = 1; $i <= 4; $i++) { for ($j = 1; $j <= $i; $j++) { echo $key; $key++; if ($j == $i) { echo "<br/>"; } } } echo ""; ?>

Output:

1

2 3

4 5 6

7 8 9 10

Cross-site scripting (XSS) is a type of computer security vulnerability typically found in web applications. XSS enables attackers to inject client-side script into web pages viewed by other users. A cross-site scripting vulnerability may be used by attackers to bypass access controls such as the same-origin policy.

crypt(), Mcrypt(), hash() are used for encryption in PHP

In PHP @ is used to suppress error messages.When we add @ before any statement in php then if any runtime error will occur on that line, then the error handled by PHP

List is similar to an array but it is not a function, instead it is a language construct. This is used for assignment of a list of variables in one operation. If you are using PHP 5 version, then the list values start from a rightmost parameter, and if you are using PHP 7 version, then your list starts with a left-most parameter. Code is like:

<?php
$info = array('red', 'sign', 'danger');
// Listing all the variables
list($color, $symbol, $fear) = $info;
echo "$color is $symbol of $fear”;?php>

There are many differences between PHP 5 and 7. Some of the main points are:

  • Performance: it is obvious that later versions are always better than the previous versions if they are stable. So, if you execute code in both versions, you will find the performance of PHP 7 is better than PHP5. This is because PHP 5 use Zend II and PHP & uses the latest model PHP-NG or Next Generation.
  • Return Type: In PHP 5, the programmer is not allowed to define the type of return value of a function which is the major drawback. But in PHP 7, this limitation is overcome and a programmer is allowed to define the return type of any function.
  • Error handling: In PHP 5, there is high difficulty to manage fatal errors but in PHP 7, some major errors are replaced by exceptions which can be managed effortlessly. In PHP 7, the new engine Exception Objects has been introduced.
  • 64-bit support: PHP 5 doesn’t support 64-bit integer while PHP 7 supports native 64-bit integers as well as large files.
  • Anonymous Class: Anonymous class is not present n PHP 5 but present in PHP 7. It is basically used when you need to execute the class only once to increase the execution time.
  • New Operators: Some new operators have added in PHP 7 such as <=> which is called a three-way comparison operator. Another operator has added is a null coalescing operator which symbol as?? and use to find whether something exists or not.
  • Group Use declaration: PHP 7 includes this feature whereas PHP 5 doesn’t have this feature.
  • 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