PHP interview questions | php interview questions and answers | top 70 php interview questions and answers | top 70 php interview questions and answers for 0 – 4 years experience

Dear Programmers, in this article we will discussed on PHP Interview Questions and Answers in details.  As per our experience and expectation, this article will cover more than 80% of PHP Interview Questions and Answers. Later on if you will work on project, even it will help lots.  

We are trying to provide best optimum questions and answers so that, it will save your valuable time as well you can focus on your, only on target/achievement

Table of Content

  1. String and String functions
  2. Array and Array functions
  3. Use of include and require, and differentiate between both
  4. How to use GET, POST, REQUEST
  5. Define SESSION & Cookies and different between both
  6. Different type of errors
  7. Use of Curl and Explanation about an API
  8. Global Variables
  9. Logical Programmes
  10. Magic functions
  11. History of PHP

1. String and String functions

1. What is string ?

string is a collection of characters, like “Hello String”.  In other way, you can say that, A string is series of characters

A string literal can be specified in four different ways:

  1. single quoted
  2. double quoted
  3. heredoc syntax
  4. nowdoc syntax (since PHP 5.3.0)

More Details click here

2. What is explode in php ?

The explode() function break the string into an array with particular separator/delimiter,  it splits the string wherever the delimiter character occurs. Let’s see an example

<?php
echo "<strong>Original Text </strong> : ".$text = "performing some operations on this using explode";
echo "<br/>";
echo "<pre>";
// default, will beak into all possible split`
print_r(explode(" ",$text));
?>
What is explode in php
What is explode in php

3. What is implode in php ?

The implode() function returns a string from the elements of an array with particular separator/delimiter.

<?php
$names = ['Pradip','Ajay','Rahul','Adeep'];
echo "<b>Name List in array format. </b> <pre>";
print_r($names);
echo "<pre/>";
$names_string = implode(", ",$names);
echo "<br/> after implode by comm : ".$names_string;
echo "<br/> after implode by space: ".implode(" ",$names);
?>
What is implode in php
What is implode in php

4. Different Between explode and implode function in php ?

Click here for details answers.

5. What is addslashes function in php

The addslashes() function returns a string with backslashes added before characters that need to be escaped.
single quote (‘)
double quote (“)
backslash ()
NULL

<?php 
$str = addslashes('What does "yolo" mean?');
echo($str);

echo "<br/><br/>";

$str2 = addslashes("It's a Mango");
echo($str2);
?>
addslashes
addslashes

6. Difference between echo & print ?

  • echo doesn’t return value while print return value of 1. so it can be used in expressions.
  • echo can take multiple parameters (rarely uses) while print can take one argument.
  • echo is faster than print
<?php
echo "<h2>PHP echo function example</h2>";
echo "Hello echo!<br>";
echo "we are learning PHP!<br>";
echo "This ", "string ", "was ", "made ", "with multiple parameters.";
?>
<br/>

<?php
print "<h2>PHP print function example</h2>";
print "Hello print!<br>";
print "we are learning PHP";
?>
echo print different

Note: When, we passed multiple parameters in print then it’s occurred error.

<?php
print "print cannot", "handle", "multipel ","multiple parameters.";
?>
Print can't take multiple parameters
Print can’t take multiple parameters

7. Define ltrim function in php

The ltrim() function removes whitespace or other predefined characters from the left side of a string

<?php 
$str = "Hello World!";
echo "<b> Original text : </b>".$str . "<br>";
echo "<b> after ltrim with Hello : </b> ".ltrim($str,"Hello");

echo "<br/>";

$str = "   Hello World!";
echo "<b>Original text : </b>".$str . "<br>";
echo "<b>after ltrim : </b>".ltrim($str);
?>
ltrim function in php
ltrim function in php

8. Define rtrim function in php

The rtrim() function removes whitespace or other predefined characters from the right side of a string.

<?php
$str = "Hello World! Hello";

echo "<b>Original Text : </b>".$str . "<br>";

echo "<br/> <b>After trim with Hello:  </b>".rtrim($str,"Hello");
?>
rtrim function in php
rtrim function in php

9. Define trim function in PHP

Removes whitespace or other predefined characters from both sides of a string

<?php 
$str = "Hello World! Hello";
echo "<b>Original Text : </b>".$str;
echo "<br><b>After trim with Hello: </b>".trim($str,"Hello");

$str = "     Hello World!     ";

echo "<br/><br/><b>Original Text : </b>".$str ;
echo "<br/><b>After trim with space : </b>".trim($str);
?>
trim function in PHP
trim function in PHP

10. What does md5 function in php ?

The MD5 hashing algorithm is a one-way cryptographic function that accepts a message of any length as input and returns
as output a fixed-length digest value to be used for authenticating the original message.

The md5 function in PHP is used to calculate the md5 hash of a string. It is commonly used to encrypt a string.

Note: Generally in php, we encrypt a string or password in md5 and stored in database.

<?php
$str = "password@123";

echo "<b> Original Text: </b>".$str;

echo "<br/><br/><b> Encrypted Text: </b>".md5($str);
?>
md5 function in php
md5 function in php

11. What is sha1 function in php ?

  • The sha1() function calculates the SHA-1 hash of a string.
  • The sha1() function uses the US Secure Hash Algorithm 1
  • SHA-1 (short for Secure Hash Algorithm 1) is one of several cryptographic hash functions. SHA-1 is most often used to verify that a file has been unaltered. This is done by producing a checksum before the file has been transmitted, and then again once it reaches its destination.
  • For more details click here.
<?php
$str = "Password@1234";

echo "<b>Original String: </b>".$str;

echo "<br/><b>Encrypted String: </b>".sha1($str);
?>
sha1 algorithms in PHP

12. What is strcmp function in PHP ?

  • The strcmp() is an inbuilt function in PHP and is used to compare two strings. This function is case-sensitive which points that capital and small cases will be treated differently
  • Returns 0 if the strings are equal.
  • Returns a negative value ( < 0 ), if $string2 is greater than $string1.
  • Returns a positive value ( > 0 ) if $string1 is greater than $string2

take an example. Both string are equal.

<?php
$str1 = "Hello world!";
$str2 = "Hello world!";

echo "<b>Original string1: </b>".$str1;
echo "<br/><b>Original string2: </b>".$str2;

echo "<br/><b>After applied strcmp: </b>".strcmp($str1,$str2);
?>
strcmp function in PHP
strcmp function in PHP

13. how to get string length in php ?

The strlen() function return the length of the string on success, and 0 if the string is empty.

<?php
echo "<b>Length of string:</b> ".strlen("To find string length");
?>
strlen function in php
strlen function in php

14. Define strtolower & strtoupper function in PHP

strtolower() function convert all character in lowercase

  • strtolower() function convert all character in lowercase.
  • strtoupper() function convert all character in uppercase.
<?php
$original = "This is very nice article";

echo "<b>Original text: </b>".$original;
echo "<br><b>after applied strtolower: </b>".strtolower($original);
echo "<br><b>after applied strtoupper: </b>".strtoupper($original);

?>
Define strtolower & strtoupper function in PHP
Define strtolower & strtoupper function in PHP

15. Define ucfirst & ucwords function in PHP

  • The ucfirst() function converts the first character of a string to uppercase.
  • The ucwords() function converts the first character of each word in a string to uppercase.
<?php
$original = "this is very nice article";

echo "<b>Original text: </b>".$original;
echo "<br><b>after applied ucfirst: </b>".ucfirst($original);
echo "<br><b>after applied ucwords: </b>".ucwords($original);
?>
ucfirst and ucwords function in php
ucfirst and ucwords function in php

16. Define isset and empty function in php

isset

  • The isset() function checks whether a variable is set, which means that it has to be declared and is not NULL.
  • This function returns true if the variable exists and is not NULL, otherwise it returns false.

empty

This function returns false if the variable exists and is not empty, otherwise it returns true.

The following values evaluates to empty:

  • 0
  • 0.0
  • “0”
  • ” “
  • NULL
  • FALSE
  • array()
<?php
$a = "";

// True because $a is empty
if (empty($a)) {
  echo "Variable 'a' is empty.<br>";
}

// True because $a is set
if (isset($a)) {
  echo "Variable 'a' is set";
}

if(isset($b))
{
	echo "<br/>b is set";
}
else{
	echo "<br/>'b' is not set";
}
?>
isset and empty functions

17. Define print_r function

  • print_r is an inbuilt function that is used in PHP to print or display the information stored in a variable.
  • Basically it prints human-readable information about a variable, lets take an example.
  • Mostly Developer use this function to print array.
<?php
echo "<pre>";
$a = array("red", "green", "blue");
print_r($a);

echo "<br>";

$b = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
print_r($b);
echo "</pre>";

$text = "This is normal text";
print_r($text);
?>
print_r function in php
print_r function in php

18. What does unset function ?

unset() destroys the specified variables. In other way you can say that unset /reset the variable to nothing. for more details click here.

<?php
	
	$a = "We are leaning unset function";
	echo "The value of variable 'a' before unset: <b>" . $a . "</b><br>";
	unset($a);
	echo "The value of variable 'a' after unset: " . $a;
?>

19. explain about var_dump function ?

The var_dump() function dumps information about one or more variables. The information holds type and value of the variable(s). I used to say to BRAHMASTRA in PHP.

<?php
$int_d = 100;
echo "Original: <b> $int_d </b><br/>";
echo var_dump($int_d) . "<br>";

$text = "Var Dump Function!";
echo "<br/>Original: <b> $text </b><br/>";
echo var_dump($text) . "<br>";

$float = 32.5;
echo "<br/>Original: <b> $float </b><br/>";
echo var_dump($float) . "<br>";

echo "<br/>";
echo var_dump($int_d, $text,$float) . "<br>";
?>
var_dump function
var_dump function

20. How to replace string in php ?

The str_replace() function replaces some characters with some other characters in a string

This function works by the following rules:

  • If the string to be searched is an array, it returns an array.
  • If the string to be searched is an array, find and replace is performed with every array element.
  • If both find and replace are arrays, and replace has fewer elements than find, an empty string will be used as replace.
  • If find is an array and replace is a string, the replace string will be used for every find value.
<?php
$main_string = "This is main string";

echo "<b>Original text : </b>".$main_string;

echo "<br><b>After replace with 'text'</b> ".str_replace("string","text",$main_string);
?>
<pre>

<?php
$arr = array(1,2,3,10,4,5,10);

$rep_arr = str_replace("10","100",$arr,$i);

echo "<br/>Original array list <br/>";
print_r($arr);

echo "<br/>After replace array list<br/>";
print_r($rep_arr);

echo "Replacements : $i places";

?>
String Replace
String Replace

2. Array and Array functions

21. What is an array in php ?

Note: An array is a data structure that stores one or more similar type of values in a single variable. in other programming language. like. Java, .Net.

  • An array is a special variable, which can hold more than one value at a time.
  • An array can hold many values under a single variable, and you can access the values by referring to an index number.

Types of an array

  • Numeric array − An array with a numeric index.
  • Associative array − An array with strings as index. This stores element values in association with key values.
  • Multidimensional array − An array containing one or more arrays and values are accessed using multiple indices

22. How to define array in php

In PHP there are two way to create an array list.

  • using PHP build-in function array()
  •  short array syntax using []
<pre>
<?php
$arr = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'); 
echo "<b>Usig array() function</b><br/>";
print_r($arr);

$arr2 = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
echo "<b>Usig short code [] syntax</b><br/>";
print_r($arr2);
?>
</pre>
How to define array in php
How to define array in php

23. What is in_array() function in php ?

  • The in_array() function searches an array for a specific value
  • Returns TRUE if the value is found in the array, or FALSE otherwise
<?php
$arr = [1,4,5,7,100,55,66,77];

echo "Array list and search number 100 in this list<pre>";
	print_r($arr);
echo "<pre/>";


if (in_array(100,$arr))
  {
  	echo "Match found<br>";
  }
else
  {
  	echo "Match not found<br>";
  }
?>
in_array() function in PHP
in_array() function in PHP

24. define use of compact() funtion in PHP

The compact() function is an built-in function in PHP, And it is used to create an array using variables.

<pre>
<?php
$name = "Ajay";
$email = "ajay@gmail.com";
$mobile = "9716449792";
$age = "29";

$result = compact("name", "email", "mobile","age");
echo "<b>Normal variable to be an array</b><br/>";
print_r($result);

$name_list = ['Ajay','Rahul','Ramesh'];
$mobile_number = ['9988776655','2233445566','5566778899'];

$result2 = compact('name_list','mobile_number');
echo "<b>array type of variable is an array</b><br/>";
print_r($result2);
?>
compact() function in PHP
compact() function in PHP

25. Define extract() function in php ?

  • The extract() Function is an build-in function in PHP. The extract() function does an array to variable conversion.
  • simply you can say the converts array keys into variable names and array values into variable value.
<?php
$posted_data = ['name' => 'Pradip','age'=>29,'mobile'=>'9716449790','email'=>'pradipmehta10@gmail.com'];

$number_of_variables = extract($posted_data);

echo "Number of variables is in array : <b> $number_of_variables</b>";

echo "<br><br>Name: ".$name;
echo "<br>age: ".$age;
echo "<br>Mobile No.: ".$mobile;
echo "<br>Email ID: ".$email;

?>
extract() function in php
extract() function in php

26. How to get length of an array in php?

There are two build-in function to get length of an array, count() and sizeof(). Let’s see an example of each.

<?php
$data = [1,2,33,44,5,8,90];

echo "<pre>";
	
	print_r($data);

echo "<pre/>";

echo "Array length using count() function: ".count($data);
echo "<br><br>Array length using sizeof() function: ".sizeof($data);
?>
Array length functions
Array length functions

27. How to find end element of an array in php ?

end() function in build-in function in php which moves internal pointer and return last element of an array.

<?php
$data = [1,2,33,44,5,8,90,100];
echo "<pre>";	
	print_r($data);
echo "<pre/>";

echo "Last element of an array : ".end($data);
?>
end() function in PHP
end() function in PHP

28. what is array_value() function in php ?

array_values() is built-in function in PHP, which return an array with default index(numeric index from 0 and increased by 1), from an array. for example, if you are defined an array with no specific key then that array having default index and it’s started from 0,1…n

Note: returned array from array_value() function, it’s key index will start from 0 and increased by 1 till end of array elements.

<?php
$data = ['name' => 'Pradip Mehta','age' => '28','mobile'=>'1122334455'];
echo "<b>Array with defined key.</b><br/>";

echo "<pre>";
	
	print_r($data);

echo "<pre/>";

echo "<b>After applied array_value() function.</b><br/>";

$array_data =array_values($data);

echo "<pre>";
	
	print_r($array_data);

echo "<pre/>";
?>
array_values() function in php
array_values() function in php

29. Define list() function in PHP

  • The list() function is an inbuilt function in PHP which is used to assign array values to multiple variables at a time.
  • This function will only work on numerical key arrays.
  • The array is assigned to multiple values, then the first element in the array is assigned to the first variable, second to the second variable, and so on, till the number of variables.
  • The list() function is used to assign values to a list of variables in one operation.
  • The list variable cannot exceed the length of the defined array.
<?php
$data = ['Day','Week','Month','Year'];
echo "<b>Array List.</b><br/>";
echo "<pre>";
	print_r($data);
echo "<pre/>";

list($day,$week,$month) = $data;

echo "<b>Using Array List Variable</b><br/>";

echo "<br>Day : ".$day;
echo "<br>Week : ".$week;
echo "<br>Month : ".$month;
?>
list() in php
list() in php

30. Define array_key_exists() function in PHP

array_key_exists() is built-in function in php, which check key/index is exists in array or not. if key/index is exists then return true otherwise return false.

<?php
$data = ['name' => 'Pradip Mehta','age' => '28','mobile'=>'1122334455'];
echo "<b>Array with defined key.</b><br/>";

echo "<pre>";
	
	print_r($data);

echo "<pre/>";

if(array_key_exists('name',$data))
{
	echo "Name : ".$data['name'];
}
else{
echo "Name is not available in this array list";	
}
?>
array_key_exists() in PHP
array_key_exists() in PHP

31. What is array_push function in php ?

array_push() is a built-in function in PHP, which insert/add a new record in end of an existing array.

<pre>
<?php
$arr = array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'); 
echo "<b>Intial array List</b><br/>";
print_r($arr);

array_push($arr,'Sunaday');
echo "<b>After pushed one element</b><br/>";
print_r($arr);
?>
</pre>
array_push() function in PHP
array_push() function in PHP

32. What is array_pop function in PHP ?

array_pop() function is a built-in function in PHP, which delete a last element from an array.

<pre>
<?php
$arr = array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'); 
echo "<b>Intial array List</b><br/>";
print_r($arr);

array_pop($arr);
echo "<b>After poped element</b><br/>";
print_r($arr);
?>
</pre>
array_pop() function in PHP
array_pop() function in PHP

33. How to search a value from an array ?

<pre>
<?php
$arr = array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'); 
echo "<b>Intial array List</b><br/>";
print_r($arr);

echo "<br/>Return key : ".array_search('Thursday',$arr);

?>
</pre>
array_search() in PHP
array_search() in PHP

34. How to get unique array list from an existing array ?

array_unique() is built-in function in PHP, which remove the duplicate record from an array list.

<pre>
<?php
$arr = [1,2,4,100,11,1,100];
echo "<b>Intial array List</b><br/>";
print_r($arr);

echo "<b>After applied array_unique() array List</b><br/>";
print_r(array_unique($arr));

?>
</pre>
array_unique in PHP
array_unique in PHP

35. Define array_merge() function in PHP

array_merge() is a built-in function in PHP, which merge two or more array and return a single array list.

<pre>
<?php
$arr = [1,2,3,4,5];
$arr2 = [5,6,7,8,9];
echo "<b>Intial array List</b><br/>";
print_r($arr);
print_r($arr2);

echo "<b>After applied array_merge() array List</b><br/>";
print_r(array_merge($arr,$arr2));

?>
</pre>
array_merge() in PHP
array_merge() in PHP

36. how to sort array in PHP ?

sort() is a built-in function in PHP, which sorts the an indexed array in ascending order.

<pre>
<?php
$num_arr = array(1,44,2,3,100,6);
echo "Before Sort array list<br/>";
print_r($num_arr);

sort($num_arr);

echo "<br/>After Sort array list<br/>";
print_r ($num_arr);

$peoples = array('rahul','ram','pradip','ajay');
echo "<br/>Before Sort array list<br/>";
print_r($peoples);

echo "<br/>After Sort array list<br/>";
sort($peoples);
print_r($peoples);

?>
</pre>
sort in php
sort in php

3. Use of include and require, and differentiate between both

37. How to include file in PHP ?

There are 4 built-in function in PHP, which used to include one file into another file.

  • include()
  • include_once()
  • require()
  • require_once()

Let’s take an example.

<?php
//filename: header.php
echo "I am header file<br>";
?>
<?php
//filename: index.php
include('header.php');

echo "Obove file is included";
?>
include in php
include in php

38. What is difference between include and include_once in PHP ?

  • Using include function, you can include same file multiple times
  • Using include_once function, file can be include at once

include

<?php
// filename: header.php
echo "I am header file<br>";
?>
<?php
// filename: index.php
include('header.php');

include('header.php');
echo "Obove file is included 2 times using include function.";
?>
include function in php
include function in php

include_once

<?php
// filename: header.php
echo "I am header file<br>";
?>
<?php
// filename: index.php
include_once('header.php');

include_once('header.php');
echo "Obove file is included 2 times using include_once function. But included once.";
?>
include_once function in PHP
include_once function in PHP

39. Difference between include() and require() function in php ?

Majorly there is only one difference between both.

  • require will produce a fatal error (E_COMPILE_ERROR) and stop the script
  • include will only produce a warning (E_WARNING) and the script will continue

include

<?php
// filename: index.php
include('header_not_exists.php');

echo "<br/>included, obove file not exists still this page is working.";
?>
included file not exists still page is working
included file not exists still page is working

require

<?php
// filename: index.php
require('header_not_exists.php');

echo "<br/>required, obove file not exists, this page is stopped working";
?>
Require function in php
Require function in php

40. Difference between require and require_once.

  • Using require function, you can include same file multiple times
  • Using require_once function, file can be include at once
<?php
// filename: header.php
echo "I am header file<br>";

?>
<?php
// filename: index.php
require('header.php');

require('header.php');

echo "<br/>required: obove file include 2 times";
?>
require function in PHP
require function in PHP

require_once

<?php
// filename: header.php
echo "I am header file<br>";

?>
<?php
// filename: index.php
require_once('header.php');

require_once('header.php');

echo "<br/>require_once: obove file include 2 times, included once";
?>
require_once in PHP
require_once in PHP

4. How to use GET, POST, REQUEST

41. What is GET in PHP ?

  • $_GET is Super global variable, and Super global variables are built-in variables that are always available in all scopes.
  • $_GET is used to collect form data after submitting an HTML form with method=”get”.
  • $_GET can also collect data sent in the URL.
  • The GET method sends the encoded user information appended to the page request.
  • The GET method is restricted to send upto 1024 characters only.
  • Never use GET method if you have password or other sensitive information to be sent to the server.
  • GET can’t be used to send binary data, like images or word documents, to the server.
  • The data sent by GET method can be accessed using QUERY_STRING environment variable.
  • The PHP provides $_GET associative array to access all the sent information using GET method.

Let’s see an example.

<?php
 //filename: form.php 
   if(isset($_GET['submit'])) {

      echo "Welcome <b>". $_GET['name']. "</b><br />";
      echo "You are <b>". $_GET['age']. "</b> years old.<br />";
      echo "Your Mobile numbet is <b>". $_GET['mobile']. "</b><br />";
      
      exit();
   }
?>
<html>
   <body>
   
      <form action = "" method = "GET">
         Name: <input type = "text" name = "name" /> <br/>
         Age: <input type = "text" name = "age" /><br/>

         Mobile: <input type = "text" name = "mobile" /><br/>

         <input type = "submit" value="Submit" name="submit" />
      </form>
      
   </body>
</html>
HTML Form
HTML Form
After submitted form
After submitted form

42. What is $_POST in PHP ?

  • $_POST is Super global variables are built-in variables that are always available in all scopes.
  • The POST method transfers information via HTTP headers
  • The POST method does not have any restriction on data size to be sent.
  • The POST method can be used to send ASCII as well as binary data.
  • The data sent by POST method goes through HTTP header so security depends on HTTP protocol. By using Secure HTTP you can make sure that your information is secure.
  • The PHP provides $_POST associative array to access all the sent information using POST method.
  • Variables are not visible in the URL so users can’t bookmark your page.

Let’s take an example

<?php
 //filename: form.php 
   if(isset($_POST['submit'])) {

      echo "Welcome <b>". $_POST['name']. "</b><br />";
      echo "You are <b>". $_POST['age']. "</b> years old.<br />";
      echo "Your Mobile numbet is <b>". $_POST['mobile']. "</b><br />";
      
      exit();
   }
?>
<html>
   <body>
   
      <form action = "" method = "POST">
         Name: <input type = "text" name = "name" /> <br/>
         Age: <input type = "text" name = "age" /><br/>

         Mobile: <input type = "text" name = "mobile" /><br/>

         <input type = "submit" value="Submit" name="submit" />
      </form>
      
   </body>
</html>
Form using POST Method
Form using POST Method
Form submitted  using POST Method
Form submitted using POST Method

43. What is request in PHP ?

PHP $_REQUEST is a PHP super global variable which is used to collect data after submitting an HTML form, it’s doesn’t matter which method you have used.

Used GET Method for form submission

<?php
 //filename: form.php 
   if(isset($_REQUEST['submit'])) {

      echo "Method: <b>".$_SERVER["REQUEST_METHOD"]." </b> <br/>";

      echo "Welcome <b>". $_REQUEST['name']. "</b><br />";
      echo "You are <b>". $_REQUEST['age']. "</b> years old.<br />";
      echo "Your Mobile numbet is <b>". $_REQUEST['mobile']. "</b><br />";
      
      exit();
   }
?>
<html>
   <body>
   
      <form action = "" method = "GET">
         Name: <input type = "text" name = "name" /> <br/>
         Age: <input type = "text" name = "age" /><br/>

         Mobile: <input type = "text" name = "mobile" /><br/>

         <input type = "submit" value="Submit" name="submit" />
      </form>
      
   </body>
</html>
Request in PHP
Request in PHP

Used POST Method for form submission

<?php
 //filename: form.php 
   if(isset($_REQUEST['submit'])) {

      echo "Method: <b>".$_SERVER["REQUEST_METHOD"]." </b> <br/>";

      echo "Welcome <b>". $_REQUEST['name']. "</b><br />";
      echo "You are <b>". $_REQUEST['age']. "</b> years old.<br />";
      echo "Your Mobile numbet is <b>". $_REQUEST['mobile']. "</b><br />";
      
      exit();
   }
?>
<html>
   <body>
   
      <form action = "" method = "POST">
         Name: <input type = "text" name = "name" /> <br/>
         Age: <input type = "text" name = "age" /><br/>

         Mobile: <input type = "text" name = "mobile" /><br/>

         <input type = "submit" value="Submit" name="submit" />
      </form>
      
   </body>
</html>
Request in PHP
Request in PHP

5. Define SESSION & Cookies and different between both

44. What is SESSION in PHP ?

  • PHP session is used to store and pass data from one page to another temporarily across the application. (until user close the website).
  • A session creates a file in a temporary directory on the server where registered session variables and their values are stored. And This data will be available to all pages on the site during that visit.
  • Sessions are a simple way to store data for individual users against a unique session ID, A cookie called PHPSESSID is automatically sent to the user’s computer to store unique session identification string.
  • PHP first creates a unique identifier for that particular session which is a random string of 32 hexadecimal numbers.
  • PHP session creates unique user id for each browser to identified the user and avoid conflict between multiple browsers.

Simple Syntax

<?php
// Start the session
session_start();
// store information
$_SESSION["name"] = "Pradip Mehta";  

// pirnt stored session data
echo "Print data using session: <b>".$_SESSION['name'];

?>
SESSION in PHP
SESSION in PHP

45. What is cookies in PHP ?

  • PHP cookie is a small piece of information which is stored at client browser. It is used to recognize the user.
  • Cookie is created at server side and saved to client browser
<?php  
setcookie("name", "This is cookies testing");

echo $_COOKIE['name'];  
?> 
PHP Cookies
PHP Cookies

46. What is different between session and cookies ?

  • Cookies are client-side files that contain user information, whereas Sessions are server-side files that contain user information.
  • Cookie expires depending on the lifetime you set for it, while a Session ends when a user closes his/her browser.
  • The maximum cookie size is 4KB whereas in session, you can store as much data as you like.
  • Cookie does not have a function named unsetcookie() while in Session you can use Session_destroy(); which is used to destroy all registered data or to unset some.

6. Different type of errors

47. Define errors in PHP

Error is the fault or mistake in a program. It can be several types. Error can occur due to wrong syntax or wrong variable or something happened with our code. It is a type of mistakes or condition of having incorrect knowledge of the code. Basically we consider 4 types of errors.
Syntax Error or Parse Error
Fatal Error
Warning Error
Notice Error

Let’s see an example.

<?php

$a = 5;
$b = 10;
$c = 15;

echo "<br/>sum of a b  = ".($a+$B);


echo "<br/><br/>sum of a b c is = ".($a+$b+$c);

?>

errors in PHP
errors in PHP

PHP error and their description :

  • E_ERROR : A fatal error that causes script termination
  • E_WARNING : Run-time warning that does not cause script termination
  • E_PARSE : Compile time parse error.
  • E_NOTICE : Run time notice caused due to error in code
  • E_CORE_ERROR : Fatal errors that occur during PHP’s initial startup (installation)
  • E_CORE_WARNING : Warnings that occur during PHP’s initial startup
  • E_COMPILE_ERROR : Fatal compile-time errors indication problem with script.
  • E_USER_ERROR : User-generated error message.
  • E_USER_WARNING : User-generated warning message.
  • E_USER_NOTICE : User-generated notice message.
  • E_STRICT : Run-time notices.
  • E_RECOVERABLE_ERROR : Catchable fatal error indicating a dangerous error
  • E_DEPRECATED : Run-time notices.

7. Use of Curl and Explanation about an API

48. What is cURL ?

cURL, which stands for client URL and can be written as curl

curl is a tool to transfer data from or to a server, using one of the
supported protocols (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP,
IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS,
SMTP, SMTPS, TELNET and TFTP). The command is designed to work without
user interaction.

  • An endpoint, which is the address (URL) to which we are sending the request.
  • An HTTP method. The most common methods used are GET, POST, PUT and DELETE.
  • Headers, which contain metadata about the request, such as content type, user agent, and so on.
  • Body, which is the message body and contains the data that we want to send, if any. Generally, the body is used with POST and PUT methods.

49. What is rest API in php?

API is the acronym for Application Programming Interface, which is a software intermediary that allows two applications to talk to each other

A REST API (also known as RESTful API) that conforms to the constraints of REST architectural style and allows for interaction with RESTful web services. REST stands for representational state transfer and was created by computer scientist Roy Fielding.

You can use API using cURL in php, let’s see an example

curl --request GET 'http://ip.jsontest.com/'
Curl in php using command

note: You can use postman to run this

8. Global Variables

50. What is global variables ?

There are predefine PHP variable called “superglobals” variables, which means that they are always accessible, from anywhere. And you can access them from any function, class or file without having to do anything special. some of below are:

  • $GLOBALS
  • $_SERVER
  • $_REQUEST
  • $_POST
  • $_GET
  • $_FILES
  • $_ENV
  • $_COOKIE
  • $_SESSION

9. Logical Programmes

10. Magic functions

Magic functions in PHP are special functions that are designed to perform certain tasks conditionally. These functions are named with double underscore (__) as prefix. click here for more details.

Some magic function listed below

__sleep
__set
__get
__isset
__unset

11. History of PHP

PHP is a programming language especially suited to web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1994. And it’s originally stood for Personal Home Page. But it now stands for the recursive initialism PHP: Hypertext Preprocessor. click here for more details.

Pradip Mehta

I am a well-organized professional in Drupal Development and PHP web development with strong script handling knowledge or automation process with PHP. I have advanced computer skills and am a proficient multitasker.