PHP Interview Questions And Answers
What Is PHP
PHP is a server-side scripting language, its use to create dynamic web pages, PHP syntax like C & Perl.
What Is the final class and final method
Final Class: The final class means that class can not be extended, it's introduced in PHP5.
Final Method: final method can not be overridden
What is unlink and unset
Unlink: The unlink function is dedicated for file system handling. Its use to delete the file given as entry.
Unset: The unset function is dedicated to variable management. it will make variable undefined
Type of cast variable in PHP
(int),(integer): cast to integer
(bool),(boolean): cast to boolean
(float),(double),(real): cast to float
(string): cast to string
(array): cast to array
(object): cast to abject
How to get number of parameters passed in function
Answer: func_num_atgs()
How to find nth highest salary
select salary from employee order by salary desc limit 1,(n-1)
How to remove a duplicate record from the table
select u1.name,u2.email from users as u1 inner join (select email from users group by email Having count(id)>1) as u2 on u1.email=u2.email
What are traits
Traits are a mechanism that allows you to create reusable code in PHP where multiple inheritances is not supported. To create traits we use keyword traits
trait users{
function getUserType(){}
function getUserRes(){}
function getUserDelete(){}
}
class Employee extends Base{
use users;
}
How to Create a Reverse Pattern
*****
****
***
**
*
<?php
for($i=0; $i<=5; $i++){
for($j=5-$i; $j>=1; $j--){
echo "* ";
}
echo "<br>";
}
?>
How to create a pattern
*
**
***
****
*****
for($i=0; $i<=5; $i++){
for($j=1; $j<=$i; $j++){
echo "* ";
}
echo "<br>";
}
How to get factorial
<?php
$fact=1;
$num=5;
for($i=1; $i<=$num; $i++){
$fact=$fact*$i;
}
echo $fact;
?>
How to check prime
<?php
$check=0;
$num=10;
for($i=2; $i<=($num/2); $i++){
if($num%$i==0){
$check++;
if($check==1){
break;
}
}
}
if($check==0): "Prime";
?>
How to get Reverse Number Without Predefined Function
<?php
$rev=0;
$num=12;
while($num>1){
$re=$num%10;
$rev=(($rev*10)+$re);
$num=$num/10;
}
echo $rev;
?>
How to Swap Two Number without third variable
<?php
$a=$a+$b;
$b=$a-$b;
$a=$a-$b;
?>
How to get Fibonacci Series
<?php
$a=0;
$b=1;
$end=5;
for($i=2;$i<$end; $i++){
$res=$a+$b;
$a=$b;
$b=$res;
}
?>
What is strstr(),stristr()
strstr(): The string function strstr (string all string, string occ ) returns part of all string from the first occurrence of occ to the end of all.
stristr(): This function is case-sensitive, stristr() is identical to strstr() except that it is case insensitive.
How to Calculate days between two dates
$dt1=date('Y-m-d');
$dt2=date('Y-m-d');
$days=((strtotime($dt2-$dt1)-strtotime($dt1))/(60*60*24));
Types of the table in Mysql
- MyISAM
- HEAP
- Merge
- InnoDB
- ISAM
What is Database maximum length
- table name: 64 char
- database name: 64 char
- column name:64 chars
Redirect https to HTTP
RewriteEngine On
RewriteCond %{HTTPS}% on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Redirect HTTP to https
RewriteEngine On
RewriteCond %{HTTPS}% off
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]

Discussion & Feedback