Saturday 2 November 2019

PHP interview questions and answers for 3 year experience

PHP interview questions and answers for 3 year experience

Question: Are PHP functions case sensitive?
Inbuilt function are not sensitive.
User defined functions are senstivie.

Case sensitive (both user defined and PHP defined)
  1. variables
  2. constants
  3. array keys
  4. class properties
  5. class constants

Case insensitive (both user defined and PHP defined)
  1. functions
  2. class constructors
  3. class methods
  4. keywords and constructs (if, else, null, foreach, echo etc.)






Question: How to return call by reference value from function?
Just use &(ampersand) before the function name.
For Example.
function &showData($data){
   $data.=' New String';    
   return $data;
}



Question: How to check a form is submited OR Not?
if($_SERVER['REQUEST_METHOD'] == 'POST'){
    //form is submitted
}



Question: How to remove all leading zeros from string?
echo ltrim('00000012', '0'); //12



Question: How to find HTTP Method?
$_SERVER['REQUEST_METHOD']
It will return GET, HEAD, POST, PUT etc


Question: How to Serializing PHP object to JSON?
$userModel = new UserModel();
echo json_encode($userModel);//serialize data



Question: How to get current URL of web page?
echo 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];



Question: How to add N number of days in an data?
echo date('Y-m-d', strtotime("+30 days"));



Question: How to get Yesterday date?
echo date("F j, Y", time() - 60 * 60 * 24); //November 1, 2016


Question: What's the maximum size for an int in PHP? The size of an integer is platform-dependent
Maximum value of about two billion for 32 bit system.


Question: How to remove all specific characters at the end of a string?
$string = 'Hello. * cool';
echo $string = str_replace(array('*', '.',' '), '', $string); //Remove * . and space [you can add more as you want]