php interview questions

1.What is PHP ?

  1. Server Side Scripting Language
  2. Client Side Scripting Language
  3. Pascal Programming
  4. Logic Programming

Ans: a

  1. Can we Develop Desktop applications using PHP
  1. Yes
  2. No

Ans: a

  1. Find the Standard Tag below

 

  • <?php  ?>

 

  • <?   ?>

 

  • <%  %>

 

  • <script language=”PHP”> </script>

Ans: a

  1. Find the Scalar Data types in the below list
  1. Integer
  2. String
  3. Float
  4. Boolean
  5. All of the Above

Ans: e

  1. Check the Output for the following code ?
<?php

$x =  “true”;

echo $x;

?>

  1. true
  2. 1
  3. boolean

Ans : a

  1. Output for the Following Code

 

<?php

$x=TRUE;

$y=FALSE;

$z=$y || $x;

echo $z;

?>

  1. TRUE
  2. FALSE
  3. 1
  4. Error

Ans: c

  1.   <?php $x=” 200 ”;  ?> Write the Code to convert the String Variable into Integer

Ans:  $x =  (int)$x;

  1. What is the differences between isset() and unset() functions

Ans:

isset(): using this method we can check whether the variable is set with value or              not. If variable is set with value this method returns true.

unset(): using this method we can remove the value of a variable

  1. Output for the following code
<?php

$x = 8 – 6.4;

$y = 1.6;

var_dump($x == $y);

?>

  1. Syntax Error
  2. 1
  3. 0
  4. bool(false)
  5. bool(true)

Ans: d

  1. What is an Array

_____________________________________________________

______________________________________________________

Ans:  An array is a collection of values of different data types

 

  1. What is the index of the element “ d ” in the below array
$array = array(

“a”,

“b”,

6 => “c”,

“d”,

);

  1. 4
  2. 0
  3. 7
  4. 5

Ans: c

  1. Output for the following code

<?php

echo “<pre>”;

$array = array(

1    => “a”,

“1”  => “b”,

1.5  => “c”,

true => “d”,

);

print_r($array);

?>

 

A. 

Array

(

[1] => d

[1] => b

[1.5] => c

[true] => d

)

B.

Array

(

[1] => d

[1] => b

[1.5] => c

[true] => d

)

C.

Array

(

[1] => d

[1] => b

[1] => d

)

D. 

Array
(
[1] => d
)

Ans: D

  1. Find the Total no.of elements in the array
<?php

$arr = array(5 => 1, 12 => 2);

$arr[] = 56;

$arr[“x”] = 42;

unset($arr[5]);

?>

  1. 4
  2. 3
  3. 0
  4. 2

Ans: b

  1. What is the difference between array_combine() and array_merge() functions

________________________________________________________________

Ans:

array_combine():

  • this function creates a new array by combining the elements of exactly two arrays.
  • And this function returns an array with first array values as keys and second array values as values
  • Both arrays should have an equal no.of elements

array_merge():

  • this function creates a new array by combining the elements of two or more arrays.
  • No need to have equal no.of elements both arrays
  1. What is the difference between array_search() and in_array() functions

__________________________________________________________

Ans:

array_search(): 

  • Using this method we can search for an element in an array. If element founds, this method will return the position of the element

in_array():

  • Using this method we can check  for an element in an array. If element founds, this methods will return true ( 1 )
  1. What is the method to check whether the specified key is available in an array or Not ?
  1. array_key()
  2. array_key_search()
  3. array_key_exists()
  4. array_find_key()

Ans: c

  1. How to Sort the below array values in descending order ?

$colors = array(‘red’, ‘blue’, ‘green’, ‘yellow’);

  1. sort() method
  2. asort() method
  3. arsort() method
  4. rsort() method

Ans: d

 

  1. What is the value of a ?
<?php

$b = 6;

$a = $b++;

echo $a;

?>

  1. 6
  2. 7

Ans: 6

  1. What is the value of $e ?

$e = false || true

  1. true
  2. false
  3. none

Ans: true

  1. What is the value of $c ?
$a = array(“a” => “apple”, “b” => “banana”);

$b = array(“a” => “pear”, “b” => “strawberry”, “c” => “cherry”);

$c = $a + $b;

print_r($c);

 

  1. Array ( [a] => apple [b] => banana [d] => pear [e] => strawberry [c] => cherry )
  2. Array ( [a] => apple [b] => banana [c] => cherry )
  3. Syntax Error
  4. array()

Ans: b

  1. What is the value of $a in the below program

 

    $Bar = “a”;

$Foo = “Bar”;

$World = “Foo”;

$Hello = “World”;

$a = “Hello”;

$a;

$$a;

$$$a;

$$$$a;

$$$$$a;

$$$$$$a;

echo $a;

 

  1. Hello
  2. World
  3. Foo
  4. Bar
  5. a

Ans: a

  1. Find the Magic Constants ?
  1. _ _LINE_ _
  2. _ _FILE_ _
  3. _ _ DIR_ _
  4. _ _CLASS_ _
  5. All of the Above

Ans: e

  1. What is the difference between include() and require() function

__________________________________________________

Ans:

include():

  • Using this method we can include an external file into the current file.
  • If the specified file is not available for inclusion it will give Warning Error

require():

  • Using this method we can include an external file into the current file.
  • If the specified file is not available for inclusion it will give Fatal Error

 

  1. How to get the IP address of a user in PHP
  1. $_SERVER[“SERVER_IP“]
  2. $_SERVER[“SERVER_ADDRESS”]
  3. $_SERVER[“IP_ADDRESS”]
  4. $_SERVER[“SERVER_ADDR”]

 

Ans: d

  1. What is a Query String?

___________________________________________

Ans: A Query String is a combination of key and values pairs of data, which is always appended in URL followed by ? (Question mark)

Each pair separated with & symbol

Using Query String we send some data from one location to another location

Ex:  http:// example.com/test.php ? id=10&key=strore

  1. What is meant by persistent cookie?

__________________________________________________________

 

Ans: The cookies which are created expiration time are called Persistent Cookie

  1. Write a logic to print Prime Numbers from 1 to 20

____________________________________________________

Ans:

Answer1:
<?php

$count = 0;

$num = 2;

while ($count < 15 )

{

$div_count=0;

for ( $i=1; $i<=$num; $i++)

{

if (($num%$i)==0)

{

$div_count++;

}

}

if ($div_count<3)

{

echo $num.” , “;

$count=$count+1;

}

$num=$num+1;

}

?>

Answer2:

<?php

$num =23;

for( $j = 2; $j <= $num; $j++ )

{

for( $k = 2; $k < $j; $k++ )

{

if( $j % $k == 0 )

{

break;

}

}

if( $k == $j )

echo $k.”,”;

}

?>

 

  1. If i disable Cookies can we work with sessions in php? 
  1. No
  2. Yes
  3. Yes we can work, but we will get Warning Error

Ans: a

 

  1. What is the Difference between write mode( w ) and append mode( a ) 

________________________________________________________

Ans:

Write Mode( W ):

  • We can open a file to write the content
  • For every write operation, the existing content overrides with new content

Append Mode( a ):

  • We can open a file to write the content
  • For every write operation, new content will appends to the existing content

 

  1. Print the below date format using Date function

Saturday, 28th March 2019

________________________________

Ans:   date(“ l, dS F Y ”);

  1. How to find the total no.of days in current month?
  1. date(“ n ”);
  2. date(“ t ”);
  3. date(“ d ”);
  4. date(“ D ”);

Ans: b

  1. What is the Difference between implode() and explode() functions

_______________________________________________________

Implode():

  • Using this method we can convert an array into string

explode():

  • Using this method we can convert a string into array

 

  1. In File uploading, How can we move a file from temporary location permanent location
  1. move_file()
  2. move_temp_file()
  3. move_uploaded_file()
  4. is_uploaded_file()

Ans: c

  1. What is a Stored Procedure? Write the basic syntax to create procedure

_______________________________

Ans:

A stored procedure is a set of SQL statements.

Synatax:

create procedure <procedure-name>()

BEGIN

//Some Statements

END;

  1. What is the difference between primary key and candidate key?

_________________________________________________

Ans:

Primary key in MySQL is use to identify every row of a table in unique manner. For one table there is only one primary key. One of the candidate keys is the primary key and the candidate keys can be used to reference the foreign keys.

  1. Difference between DELETE TABLE and TRUNCATE TABLE commands in MySQL?

___________________________________________________

Ans:

Basically DELETE TABLE is logged operation and every row deleted is logged. Therefore the process is usually slow. TRUNCATE TABLE also deletes rows in a table but it will not log any of the rows deleted.  The process is faster in comparison. TRUNCATE TABLE can be rolled back and is functionally similar to the DELETE statement using no WHERE clause.

  1. Constructor functions are called automatically at the time of Object creation
  1. Yes
  2. No

Ans: a

  1. How can we access a non-static member inside a function ?

 

  1. self::
  2. Using class name
  3. $this->
  4. static

Ans: c

  1. What is the use of Scope Resolution Operator (::) ?

_____________________________________

Ans:  used access to static, constant, and overridden properties or methods of a class.

 

  1. Can we declare class properties using final keyword ?
  1. No
  2. Yes

Ans: a

  1. Write an SQL query to find second highest salary from the below emp table?

__________________________________________

Ans:    SELECT  MAX(salary) AS salary   FROM emp WHERE salary < (SELECT MAX(salary) FROM emp);

 

  1. How can we access static members of a class ?
  1. By using Object
  2. By using Class Name
  3. By using $this keyword
  4. By Using final keyword

Ans: b

  1. How can we declare constants in a class ?
  1. define() method
  2. constant() method
  3. const keyword
  4. None of the Above

Ans: c

  1. Can we create an object for Abstract classes?
  1. Yes
  2. No

Ans: b

  1. What is the default uploaded filesize in php?
  1. 2MB
  2. 4MB
  3. 8MB
  4. 12MB

Ans: a

  1. What is function to remove HTML tags from a string?
  1. remove_html_tags()
  2. strip_tags()
  3. strip_html_tags()
  4. htmlspecialchars()

Ans: b

  1. What is the default storage engine in MySQL ?
  1. InnoDB
  2. MyISAM
  3. Memory
  4. CSV

Ans: a

  1. Can use Magic Methods in Static Context ?
  2. Yes
  3. No

Ans: b

 

  1. How can we read Form data posted by POST method in php
  1. $_POST
  2. $_GET
  3. $_REQUEST
  4. Both a and c

Ans: d

  1. What is the default port number of apache server?
  1. 90
  2. 8080
  3. 80
  4. 9090

Ans: c

  1. PHP programming was developed by

C and PERL Languages

 

  1. What are the Features of PHP ?
  1. PHP is Cross Platform:

It can run under any type of Operating System

Eg:  Linux, Unix, Windows etc.

 

b.PHP is Cross Server:

It  can supports different types of web servers

Eg: IIS(Internet Information Server, Tomcat, Apache etc.)

 

c.PHP is Cross Database:

It all types of databases . Eg: Mysql, Oracle, sql Server etc..

d.PHP is Open Source.

e.PHP supports Object Oriented Concepts.

  1. Faster  in performance
  2. Within less time and less cost we can develop the applications

 

54. What are the  PHP Coding Rules?

  1. In php every line should end with semi colon ( ; )
  2. Php file extension should be .php
  3. For example  index.php
  4. All variables are declared with $ symbol

Eg: $a=10;

$v=”Hello”;

  1. Declaration style tags in php

Standard Tag:

<?php

echo “Hello World”;

?>

Script tag:

<script  language=”PHP”>

echo “Hello”;

</script>

Note: <script> tag will never supported in latest versions of php

Short-Open Tag

<?

Echo “Hello”;

?>

Asp Tags

<%

echo “Hello”;

%>

 

  1. How can we  work with asp tags and short-open tags

The php configuration settings file( php.ini ), In this file we have two configuration settings

a).  asp_tags        b). short_open_tag

by default these value settings are ‘off’. If we want to work with above tags we need to change the value as ‘on’.

The location of  php.ini file is: C:/xampp/php/         (for XAMPP)

C:/wamp/bin/php/     (  for WAMP)

  1. What are the Output Statements in php ?

There are four types of output statements in php

Print :

Using this function we can print the output to the browser

On successfully  written the output to browser this function returns true (true means 1)

Eg:        <?php

$a=print “Hello”;//Hello

echo $a;//1

?>

Echo :

It is also used for printing the output to the browser.but it won’t return any value.

So that echo function is very fast when compared to print

eg:        <?php

$a= “Hello”;

echo $a;//Hello

?>

Print_r() :

Using this function we can print the array values and object values

<?php

$ar=array(10,20,30);

Print_r($ar)//array([0]=>10[1]=>20[2]=>30)

?>

Var_dump() :

This function will print a variable with its data type

<?php

$a=10;

$b=10.5;

$c=”Welcome”;

var_dump($a);// int(10)

var_dump($b);// float(10.5)

var_dump($c);// string(7) “Welcome”

?>

 

57 . <?php $x=” 200 ”;  ?>  Write the Code to convert the String Variable into Integer 

Ans:  $x =  (int)$x;

 

  1. How many data types are there in PHP. whar are they?

There are 3 data types:

1.Scalar types

2.Compound types

3.Special types

 

  1. What is the default port for MySQL Server?

The default port for MySQL Server is 3306.

 

60.What is the difference between the primary key and the candidate key?

The primary key in MySQL is used to identify every row of a table in a unique manner.

there is only one primary key per table.

The candidate keys can be used to reference the foreign keys. One of the candidate keys is the primary key.

 

  1. What are the TRIGGERS that can be used in MySQL tables?

MySQL TRIGGERS are :

BEFORE INSERT

AFTER INSERT

BEFORE UPDATE

AFTER UPDATE

BEFORE DELETE

AFTER DELETE

  1. What is the difference between primary key and unique key?

The primary key does not allow ‘NULL’, and duplicate values but the unique key does.

The unique key does not allow duplicate values but it allow one null value

 

  1. How can we get the version of MySQL by using Command Line?

mysql> SHOW VARIABLES LIKE “%version%”;

  1. What are the string types available for a column in MySQL?

Following are the string types:

SET

BLOB

ENUM

CHAR

TEXT

VARCHAR

 

  1. Write a Query to find all the indexes defined for a table?

SHOW INDEX FROM <tablename>;

 

  1. Write a query to display top 10 records in MySQL?

SELECT * FROM LIMIT 0,10;

 

  1. Write A Query To Select All Teams That Won Either 2, 4, or 6 Games?

SELECT teamname FROM teams WHERE team_won IN (2, 4, 6);