Core PHP Interview Questions

Core PHP Interview Questions Download Core PHP Interview Questions PDF

Core 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 )
  • Session and cookie both are used to store values or data.
  • cookie stores data in your browser and a session is stored on the server.
  • Session destroys that when browser close and cookie delete when set time expires.

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

You can use a .htaccess file to block the direct access of directory in PHP. It would be best if you add all the files in one directory, to which you want to deny access.

For Apache, you can use this code:

<&lt  Order deny, allow  Deny from all</&lt

But first, you have to create a .htaccess file, if it is not present. Create the .htaccess file in the root of your server and then apply the above rule.

Default session time in PHP is 1440 seconds (24 minutes) and Default session storage path is temporary folder/tmp on server.

You can change default session time by using below code

<?php
// server should keep session data for AT LEAST 1 hour
ini_set('session.gc_maxlifetime', 3600);

// each client should remember their session id for EXACTLY 1 hour
session_set_cookie_params(3600);
?>

In PHP Type hinting is used to specify the excepted data type of functions argument.
Type hinting is introduced in PHP 5.

Example usage:-

//send Email function argument $email Type hinted of Email Class. It means to call this function you must have to pass an email object otherwise an error is generated.

<?php
function sendEmail (Email $email)
{
  $email->send();
}
?>
  • 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

PHP is a server side scripting language for creating dynamic web pages. There are so many functions available for displaying output in PHP. Here, I will explain some basic functions for displaying output in PHP. The basic functions for displaying output in PHP are as follows:

  • print() Function
  • echo() Function
  • printf() Function
  • sprintf() Function
  • Var_dump() Function
  • print_r() Function

In PHP, one can specify two different submission methods for a form. The method is specified inside a FORM element, using the METHOD attribute. The difference between METHOD=”GET” (the default) and METHOD=”POST” is primarily defined in terms of form data encoding. According to the technical HTML specifications, GET means that form data is to be encoded (by a browser) into a URL while POST means that the form data is to appear within the message body of the HTTP request.

 GetPost
History:Parameters remain in browser history because they are part of the URLParameters are not saved in browser history.
Bookmarked:Can be bookmarked.Can not be bookmarked.
BACK button/re-submit behavior:GET requests are re-executed but may not be re-submitted to the server if the HTML is stored in the browser cache.The browser usually alerts the user that data will need to be re-submitted.
Encoding type (enctype attribute):application/x-www-form-urlencodedmultipart/form-data or application/x-www-form-urlencoded Use multipart encoding for binary data.
Parameters:can send but the parameter data is limited to what we can stuff into the request line (URL). Safest to use less than 2K of parameters, some servers handle up to 64KCan send parameters, including uploading files, to the server.
Hacked:Easier to hack for script kiddiesMore difficult to hack
Restrictions on form data type:Yes, only ASCII characters allowed.No restrictions. Binary data is also allowed.
Security:GET is less secure compared to POST because data sent is part of the URL. So it’s saved in browser history and server logs in plaintext.POST is a little safer than GET because the parameters are not stored in browser history or in web server logs.
Restrictions on form data length:Yes, since form data is in the URL and URL length is restricted. A safe URL length limit is often 2048 characters but varies by browser and web server.No restrictions
Usability:GET method should not be used when sending passwords or other sensitive information.POST method used when sending passwords or other sensitive information.
Visibility:GET method is visible to everyone (it will be displayed in the browsers address bar) and has limits on the amount of information to send.POST method variables are not displayed in the URL.
Cached:Can be cachedNot Cached
Large variable values:7607 characters maximum size.8 Mb max size for the POST method.

It is clear from the name SHA256 that the length is of 256 bits long. If you are using hexadecimal representation, then you require 64 digits to replace 256 bits, as one digit represents four bits. Or if you are using a binary representation which means one byte equals to eight bits, then you need 32 digits.

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>

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.