Tuesday, 9 September 2014

PHP interview Questions

How can we know the number of days between two given dates using PHP? 
Simple arithmetic:

$date1 = date('Y-m-d');
$date2 = '2006-07-01';
$days = (strtotime() - strtotime()) / (60 * 60 * 24);
echo "Number of days since '2006-07-01': $days";


What are the differences between Get and post methods.

GET Method
:
  • Data is append in url and information will appear in URL.
  • Get() transfer only 255 char.
  • It is not secure .
  • GET request is comparatively faster.
  • GET is the default method of the HTML FORM element.

POST Method:
  • POST request is encapsulated in the body of the HTTP request and can't be seen.
  • No limitation  for the POST request.
  • Post method is secure as compared to get method. 

Check password length in php

<?php
$password="checkpassword";


$passwordCheck=passwordLength($passwod);


function passwordLength($pwd)
{
    $pwdlen=strlen($pwd);//calculate length of password using strlen function


      if($pwdlen<=4) //if length of password is less than 4 then password is too short.
      {
         return "Password too short!";
      }
    else if ($pwdlen>16) //if length of password is greater than 16 then password is too Long.
   {
         return "Password too Long!";
     }



?>

PHP explode() Function


explode :- The explode() function split a string into an array.

<?php


$name= "PHP implode explode string functions";
$arr = (" ", $name);
print_r($arr);
?>

Different types of errors in PHP?

Three are three types of errors:

1. Notices: These are trivial,non-critical errors that PHP encounters while executing a script.
 for example, accessing a variable that has not yet been defined. By default,
such errors are not displayed to the user at all .

2. Warnings: These are more serious errors – for example, attempting
to include() a file which does not exist. By default, these errors are
displayed to the user, but they do not result in script termination.

3. Fatal errors: These are critical errors – for example,
instantiating an object of a non-existent class, or calling a
non-existent function. These errors cause the immediate termination of
the script, and PHP’s default behavior is to display them to the user
when they take place.




What is difference between unset and unlink..
Unset is used to delete(destroy) a variable whereas unlink used to delete a file.


 What is type hinting in PHP.
Type hinting means the function specifies what type of parameters must be and if it is not of the correct type an error will occur.PHP 5 introduces type hinting  and the functions are able to  force the parameters to be objects, interfaces ,array .