PHP Interview Questions

1.PHP

  • PHP is a server side scripting language commonly used for web applications
  • It is used to manage session tracking, databases, dynamic content, even build entire e-commerce sites.
  • PHP it means PHP: Hypertext Preprocessor.

2.In how many ways you can write php script

<?php code goes here ?>

<? code goes here ?>

<script language=”php”> —Code Content—</script>

Most common tag is the <?php…?>

3.”echo” in php

  • To print a data in the webpage you can use the “echo”.
  • <?php echo Basicofy; ?>

4.Errors in PHP?

  • Parse Error –It will stop execution of complete script. If there is any syntax mistake the out put will parse error
  • Fatal Error – It will stop execution from the line where error is occurred.
  • Notice Error-It is the simple message to end user it does not stop execution of script.
  • Warning Error –It is same as notice does not stop execution of script.

5.Require_once(), require(), include() difference between them?

  • Include() is function that can include external file many time in current page.if file does not exist it will give some error and it will execute rest of the script.
  • Require() is same as include but it returns the fatal error if external file does not exist and it will stop execution.
  • Require_Once() – It is same as the require but it include external life only one time.

6.GET, POST and REQUEST methods?

  • Get transfer information through http head location. Get is not secured.
  • Post transfer information through http body location. Post is highly secured.
  • Request is the way to get posted values of GET and POST Method. We also can get cookie and query string values using request.

7.How to declare an array in php?

  • var $arr = array(‘Nokia’, ‘Samsung’, ‘lemon’);

8.Session?

  • A session is a logical object permit us to preserve temporary data across multiple PHP pages.

9.Session and Cookie?

Cookie Session
It store limited amount of data It store large amount of user data
cookie store data on client system session store data on server system
Cookie data visible on client system Client can’t see data of session
Cookie consume memory of client Session consume memory of server

10.Types of cookies

(1) In-memory Cookies.

(2) Persistent cookies.

11.In Which way  cookies is set & How to Retrieve a Cookie Value?

Setcookie(“example”, “kedar”, time()+3600);

echo $_COOKIE[“user”];

12.Why session 

  • When there is a issue regarding request and response from a same user to a server, the server cannot identify from which user it is getting requests. Because HTTP is a stateless protocol.
  • When there is situation to maintain the conversational state, session is needed.

13.syntax to declare session and cookies

  • Session syntax : $_SESSION[‘uname’]=’Alex’;
  • cookies syntax : setcookie(name,value,expire);

14.Way to destroy a session

  • To delete a specific session variable we use

session_unset($_SESSION[‘variable_name’])

15.loops in php?

  • for,while,do while and foreach

16.How to get current session ID

<?php

session_start();

echo session_id();

?>

17.How to convert string to array in php?

$string=”cakephp and zend”;

$array =explode(‘and’,$string);print_r($array);

18.How to convert array to string in php?

$array = array(‘cakephp’,’zend’);$string =implode(‘ and ‘,$array);echo $string;

19.syntax for ‘foreach’ loop.

foreach (array as value){ code to be executed;}

20.How to create a mysql connection?

mysql_connect(servername,username,password);

21.How to select a database?

mysql_select_db(DatabaseName);

22.How to execute an sql query? How to fetch its result ?

$sql = mysql_query(“SELECT * FROM `student` WHERE `std_id`=’123′; “);
$res = mysql_fetch_array($sql);
echo $res[‘Fname’];

23.How many way you can retrieve the data in the result set of MySQL using PHP?

  • mysql_fetch_row()
  • mysql_fetch_array()
  • mysql_fetch_object()
  • mysql_fetch_assoc()

24.what is the use of isset() in php?

This function is used to determine if a variable is set and is not NULL

25.Differences between mysql_fetch_row(),mysql_fetch_object(),mysql_fetch_array()?

  • Mysql_fetch_array :-Read record from result set and return o/p as both numeric and associative array
  • mysql_fetch_object :- Read record from result set and return o/p as object
  • mysql_fetch_row ():- Using this function we can fetch data/record from result set. It read all column of record and return as numeric array.

26.What is PDO classes?

The PHP Data Objects (PDO) extension defines a consistent interface and lightweight for accessing databases in PHP. Using PDO drivers we can connect to database like DB2, Oracle, PostgreSQL etc. It contains data-access abstraction layer, so no matter what database we use for function to issue queries and fetch data will be same.

27.Why do we use GROUP BY and ORDER BY function in mysql?

Group By is used for retrieving information about a group of data. It can sort column in both ascending and descending order. It is generally used with some aggregate function like AVG ,SUM etc. ORDER BY it is used to sort the records using column name.

28.What is JOIN in mysql?

  • Join is use to get information to multiple table base on condition.
  • According to our requirement just splitting of information  into multiple table using joins we can combine those table to get data

.

29.LEFT JOIN, RIGHT JOIN and INNER JOIN

INNER Join compares two DIFF tables and only returns results where a SAME match exists. INNER joins tend to make result sets smaller, but because records can be duplicated this isn’t guaranteed. Records from the 1st table are duplicated to the 2nd table when they match multiple results in the 2nd.

 LEFT JOIN  returns all rows from the left table(1st table) , with the matching rows in the right table(2nd table). when there is no match the result is NULL in the right side.

RIGHT JOIN  returns all rows from the Right table (2nd table), with the matching rows in the left table (1st table).

30.How do we drop a table

DROP table customers; It will drop the table customers

31.What are Heap tables?

HEAP tables are used for high speed storage on temporary basis and they are present in memory. BLOB or TEXT fields are not allowed in heap tables.

  • Only comparison operators can be used <,= >,>,=,=<.
  • AUTO_INCREMENT is not sustained by HEAP tables
  • Indexes should be NOT NULL.

32.What is the default port for MySQL Server?

.3306

33.Differentiate between FLOAT and DOUBLE?

Following are differences for FLOAT and DOUBLE:

  • Float data type can store decimal values.
  • Floating point numbers are stored in DOUBLE with accuracy of 18 places and it has eight bytes.

34.Differentiate CHAR_LENGTH and LENGTH?

  • The numbers are same for Latin characters but they are different for other encodings and Unicode.
  • CHAR_LENGTH  is count the character whereas the LENGTH is byte count.

35.How to represent ENUMs and SETs internally?

ENUMs and SETs are used to represent powers of two because of storage optimizations.

36.What is the usage of ENUMs in MySQL?

ENUM is a string object used to specify set of predefined values and it is used when creating table.

gfnbbxfncnzcf Create table size(name ENUM(‘Small’, ‘Medium’, ‘Large’);

37.Difference between CHAR and VARCHAR?

CHAR and VARCHAR types different in storage and retrieval.

  • CHAR column length is fixed up to
  • Varchar is variable-length

38.What are the drivers in MySQL?

Following are the drivers available in MySQL:

  • JDBC Driver
  • PHP Driver
  • C WRAPPER
  • ODBC Driver
  • PYTHON Driver
  • PERL Driver
  • RUBY Driver
  • CAP11PHP Driver
  • mxj

39.What is a trigger in MySQL?

A trigger is a set of codes that executes in response to some events.

40.What is REGEXP?

REGEXP is a pattern match by using regular expression. It is a powerful way of specifying a pattern for a complex search.

41.Query for to display top 20 rows from table

SELECT * FROM table_name LIMIT =20;

42.Encryption Function in PHP

  1. One way encryption
    • MD5.
    • CRC32.
    • Cryptography.
  1. Two way encryption
    • Base64_ecncode.
    • Base64_decode.

43. How do we Drop a table?

DROP table clients;

It will drop the table clients

44.How do we delete a row in a table?

DELETE FROM clients WHERE cid=121;

In this,  it will delete the record of the clients with clients id 121

45.What is mean by escaping to PHP?

The PHP parsing engine is used to differentiate between PHP code and other elements in the page. All this process is known as ‘escaping to PHP’.

46.PHP is a case sensitive language?

No, PHP is partially case sensitive.

47.What are rules for naming a PHP variable?

Rules for naming a variable are as follows:−

    • Variable names must start with a underscore or letter character.
    • A variable name can consist of numbers, letters, underscores but you cannot use characters like + , – , % , ( , ) . & , etc

48.How to include a file into a php page?

We can include a file using “require()” or “include() ”  function with file path as its parameter.

49.What’s the difference between include and require?

By using require(), if the file is not found, it will cause a fatal error and stops the execution of the script.

By using include(), if the file is not found, execution will continue with a warning.

50.What is the difference between include and require?

It is how they handle failures. If the file is not found by require(), it will cause afatal error and halt the execution of the script. If the file is not found by include(), a warning will be issued, but execution will continue.

51.How to set cookies in PHP?
Setcookie(“example”, “kedar”, time()+3600);

52.How to Retrieve a Cookie Value?
echo $_COOKIE[“clients”];

53.what types of loops exist in php?

for, foreach, while and do while.

54.How to create a mysql connection?

mysql_connect(name of server, your username, your password);

 55.How to select a database?
mysql_select_db($database_name);

56. How to execute sql query? How to fetch its result ?

$query = mysql_query(“SELECT * FROM `clients` WHERE `c_id`=’5′; “);
$result = mysql_fetch_array($query);
echo $result[c_name’];

57.How we can retrieve the data in the result set of MySQL using PHP?
1. mysql_fetch_row
2. mysql_fetch_array
3. mysql_fetch_object
4. mysql_fetch_assoc

58.Why header() function is used in php ?

Before sending the actual out put this function calls. The header() function sends a raw HTTP header to the client browser.e.g.By using this function you can print HTML element.

59.How to redirect a page in php?

The following code can be used for redirecting from one page to another,

Syntax- header(“Location:index.php”);

60.What is the name of default page and how to set a home page in a php?

index.php or home.php is the default names of the home page in php.

61.How to find the length of a string?

To find, the length of a string strlen() function is used.

62.Why isset() is used in php?

It is used to find out a variable is set and is not NULL.

63.What is the use of “action” attribute in a html form?

The action attribute is used to set a path for where to send the form-data after submitting the form.

64.What “method” attribute is used in a html form?

The”method” attribute is used to how to send the form-data to the server.

It contains two types, get and post.

The default method is get.In this, it sends the form information by adding it on the URL.

The another method is POST method is not visible to others and has no limits how many amount of information to send.

65.Why “enctype” attribute is used in a html form?

The enctype attribute is used to, when the form-data submitted to the server, how the encoded it. Because of that we need to set enctype as “multipart/form-data”when we uses a form for files uploading.

66.How do you define a constant?

By using,

define() directive, like define (“CONSTANT”,100);

67.How to send email using php?

In PHP,for sending email you use the mail() function.The mail() function accepts 5 parameters as follows-

You need webserver, you can’t send email from localhost. eg : mail($to,$subject of mail,$message of mail,$headers of mail);

Last two parameters are optional (Message and Headers)

68.How to find current date and time?

To find, the date() function provides you, retrieving the current date and time, applying the format integer parameters indicated in your script.

69.What is x+ mode in fopen() used for?

1.Read/Write.

2.Creates a new file.

3.Returns FALSE and an error if file already exists.

70.What is default session time and path?

1) In PHP, default session time is 1440 seconds or 24 minutes
2) Default session save path id temporary folder /tmp

71.What are the Formatting and Printing Strings in PHP?

printf()- It Displays a formatted string
sprintf()- It Saves a formatted string in a variable
fprintf()- It Prints a formatted string to a file
number_format()- It Formats numbers as strings

72.How to call a constructor for a parent class?

 parentclass::constructor($value)

73.What is the use of tags <?= and ?> in PHP?

 Using these tags, the output is displayed directly to the browser.

74.Who is the father of PHP ?
The Father of PHP is Rasmus Lerdorf.

75.What are extensions of PHP File?

  • .php,
  • .phtml,
  • .php4,
  • .php3,
  • .php5,
  • .phps

76. What type of inheritance supported by PHP?

  1. Single inheritance.
  2. Multiple Level inheritance.

77.What are the latest versions of apache, php, and mysql?

  1. PHP: 7.0.4 / March 3, 2016
  2. Apache: 2.4.18 December 14, 2015
  3. MySQL: 5.6.26 / July 24, 2015

78.How to store the uploaded file to the location?

move_uploaded_file( string filename, string destination name)

79.What are encryption functions in PHP?

  1. CRYPT()
  2. MD5()

80.PHP read the hash portion of the URL?

No, PHP can’t read the hash portion of the URL

81.What are web services in PHP?
Web services are the server. The web services are used to store, process and deliver web pages to clients(Browsers are like mozilla, IE, Chrome etc).

82.What are access modifiers in php?
Below are the access modifiers in php-

  1. public
  2. protected
  3.  private

83.What is the maximum length of a table name, a database name, or a column name in MySQL?

  1. Database name: 64 characters
  2. Table name: 64 characters
  3. Column name: 64 characters

84.What is the main difference between the functions unlink and unset?

  1. unlink() deletes the given file from the file system.
  2. unset() makes a variable undefined.

85.What is the difference between Mysql and sql?

Mysql is a open source, relational database management System.Structured Query Language(SQL) is a programming language designed for managing data held in a Relational Database Management System.

86.What is the use of final keyword?

The PHP 5 describes the final keyword, which is used to prevents child classes from overriding. If the class itself is being defined final then it cannot be extended.

87.What is the use of “ksort” in php?

By using ksort you can sort the data in a key order.

88.How to get the value of latest session id?

session_id() function is used to return the session id for the latest session.

89.What is the use of an associative array?

Associative arrays are arrays used for string keys is called associative arrays.

90.How to stop the execution of a php script ?

By using exit() function you can stop the execution of a page.

91.Why mysql_real_escape_string() function is used?

mysql_real_escape_string() it is used to escapes special characters in a string for use in an SQL statement.

92.What is use of count() function in php ?

The count() is used to count all elements in an array.

93.Which library is used for image function?

GD library is used to execute an image functions.

94.Why ‘imagetypes()’ function is used in php?

In php, imagetypes() gives the image format and types supported by the current version of GD-PHP.

95.Which functions to be used to get the image’s properties (like size, width and height)?

The functions are for size-getimagesize() , for width-imagesx()  and for height-imagesy().

96.How information of a variable will be display and readable by human with PHP?

print_r() function is used to display a human-readable result.

97.What is the use of function file_get_contents()?

file_get_contents() is used for, reading a file and storing it in a string variable.

98.How to pass a variable by reference?

To pass a variable by reference, we use an ampersand in front of it, as follows $value1= &$value2.

99.How to cast types in PHP?

The variable which is to be cast in PHP as follows:

* (int), (integer) – cast into integer

* (bool), (boolean) – cast into boolean

* (float), (double), (real) – cast into float

* (string) – cast into string

* (array) – cast into array

* (object) – cast into object

100.What is the default time for session in php?

The default time for session in php is until closing of browser.

Leave a Reply

Your email address will not be published. Required fields are marked *