Ques: How to create object without class ?
Ans: You can create it by using empty class. You can use PHP stdClass (which is empty class in PHP) to create its object. It is useful if you are creating dynamic object or if you want to set some dynamic property.
For example –
$object = new stdClass();
$object->name = ‘test’;
Ques: What is armstrong number ? Write a program to check whether the number is armstrong or not.
Ans: Armstrong Number –
It is a number such that the sum of cube of each digit is equal to the number. For example – 153 is an armstrong number because sum of cube of each digit(1*1*1+5*5*5+3*3*3) is 153 which is same as number.
Program –
$n = 153; $sum = 0; $tmp = $n;
while($tmp!=0){
$rem = $tmp%10;
$sum = $sum+($rem*$rem*$rem);
$tmp=$tmp/10;
}
if($n==$sum) {
echo “It is armstrong number”;
}
else {
echo “Not an armstrong number”;
}
Ques: What is difference between object and array ?
Ans: Array is a collection of heterogeneous data(they may be with key value pair or without key value pair) and object is collection of key value pair.
Ques: What will be the output of following :
$a = 1;
foreach($a as $b){
echo $b;
}
Ans: It will give error because foreach works on Array not on string.
Ques: Write a program to find the missing number from an array of number from 1 to 99(There is not empty value if number is missing).
array(1,2,3,4,5,………..,99);
Ans: $array = array(1,2,3,4,5,……….,99);
for($i=0;$i<count($array)-1;$i++){
if($array[$i+1]!=$array[$i]+1){
$missing_number = $array[$i]+1;
echo “Missing Number is “.$missing_number;
}
}
Ques: Can we use session without cookies in PHP ?
Ans: Yes, in PHP, Session can be used without Cookies.
Way to use Session without Cookie –
There are two ways to do it –
1.) For a HTML form, PHP will add a hidden input whose name will be PHPSESSID. The value of PHPSESSID will be based on what PHP assigns to your session ID.
For Example –
<form>
<input type=”hidden” name=” PHPSESSID” value=”12345678″>
</form>
So, when form will be submitted, PHP will get Session id value from form and will get information about session.
2.) PHP will look for all links in HTML, and will modify them and will append PHPSESSID with them.
For Example –
<a href=”http:www.example.com”>Test</a>
PHP will change it to –
<a href=”http:www.example.com? PHPSESSID =12345678″>Test</a>
Ques: Write a program to show duplicate values in array and find the sum between duplicate values.
array(1,2,3,2,5,4,7,5,8).
Output – Duplicate Values are 2,5.
2+3+2 = 7
5+4+7+5 = 21
Ans:Program –
$array = array(1,2,3,2,5,4,7,5,8);
$arr1 = array(); $arr2 = array();
for($i=0;$i<count($array);$i++){
if(in_array($array[$i],$arr1)){
$arr2[$i] = $array[$i];
}else{
$arr1[] = $array[$i];
}
}
echo “Duplicate Values are “.implode(“,”,$arr2);
echo “<br>”;
foreach($arr2 as $k=>$v){
$flag = 0;
for($i=0;$i<count($array);$i++){
if($array[$i]==$v && $flag==0){
$sum=0;
for($j=$i;$j<=$k;$j++){
$sum = $sum+$array[$j];
}
echo $sum;
echo “<br>”;
$flag = 1;
}
}
}
Ques: Write a program to check whether a number is palindrome or not.
Ans: Palindrome Number – Palindrome number is that number whose reverse is same as the number.
For Example – Number 131 is palindrome but number 57 is not palindrome.
Program –
$number = 131; $tmp = $number;
$rev = 0;
while(floor($tmp)){
$rem = $tmp%10;
$rev = $rev*10+$rem;
$tmp = $tmp/10;
}
if($number==$rev){
echo “Number is palindrome”;
} else{
echo “Number is not palindrome”;
}
Ques: Write a program to reverse an array at a position.
Array – array(1,2,3,4,5,6,7);
Position – 2
Output – array(4,5,6,7,1,2,3)
Ans: Program –
$array = array(1,2,3,4,5,6,7); $pos = 2;
for($i=0;$i<count($array);$i++){
if($i<=$pos){
$arr1[] = $array[$i];
}else{
$arr2[] = $array[$i];
}
}
$final_array = array_merge($arr2,$arr1);
echo implode(“,”,$final_array);
Ques: Write a program to print table of a number(say 8) without using loop.
Ans: Program –
$n = 8;
function print_table($n,$i){
if($i<=10){
echo $n*$i;
echo “<br>”;
return print_table($n,$i+1);
} else {
return;
}
}
print_table($n,1);
Ques: What is abstract function ?
Ans: It is the function that is defined but not implemented in the code.
Ques: How can i create the object if that class whose constructor is defined as private ?
Ans: You can not create object of that class. But if you have to use the function inside it then you should make function as static.
Example –
Class Test(){
private function __construct(){
echo “constructor called”;
}
public static function display($message){
echo $message;
}
}
Test::display(‘Hello I am in the function’);
Ques: Write a program to find the sum of a matrix.
Matrix – 1 2 3 4
5 6 7 8
1 2 3 4
5 6 7 8
Ans:
function diagonalSums($matrix, $n)
{
$left_diagonal_sum = 0;
$right_diagonal_sum = 0;
for ($i = 0; $i < $n; $i++)
{
for ($j = 0; $j < $n; $j++)
{
// principal diagonal
if ($i == $j){
$left_diagonal_sum += $matrix[$i][$j];
}
// secondary diagonal
if (($i + $j) == ($n – 1)){
$right_diagonal_sum += $matrix[$i][$j];
}
}
}
echo “Principal Diagonal: “.$left_diagonal_sum. “<br>”;
echo “Secondary Diagonal: “.$right_diagonal_sum.”<br>”;
}
$array = array (array ( 1, 2, 3, 4 ),
array ( 5, 6, 7, 8 ),
array ( 1, 2, 3, 4 ),
array ( 5, 6, 7, 8 ));
diagonalSums($array, 4);
Ques: Suppose there is an array with collection of objects, say
array(
{
‘key’ : ‘1’,
‘name’ : ‘test’,
‘value’ : 1
},
{
‘key’ : ‘2’,
‘name’ : ‘test2’,
‘value’ : 2
},
{
‘key’ : ‘2’,
‘name’ : ‘test2’,
‘value’ : 3
}
);
Print the object that is unique and if the objects key and name matches, then, their value is summed up and shown only once.
Output –
array(
{
‘key’ : ‘1’,
‘name’ : ‘test’,
‘value’ : 1
},
{
‘key’ : ‘2’,
‘name’ : ‘test2’,
‘value’ : 5(2+3)
}
);
Ans:
$arr1 = array(“key”=>’1′, ‘name’ => ‘test’, ‘value’ => 1);
$obj1 = (object)$arr1;
$arr2 = array(“key”=>’2′, ‘name’ => ‘test2’, ‘value’ => 2);
$obj2 = (object)$arr2;
$arr3 = array(“key”=>’2′, ‘name’ => ‘test2’, ‘value’ => 3);
$obj3 = (object)$arr3;
$main_array = array($obj1, $obj2, $obj3); //main array containing the objects
$final_array = array();
function search($key, $name, $array){
$response = array();
foreach($array as $k=>$v){
if($v->key==$key && $v->name==$name){
$response[0] = ‘1’;
$response[1] = $k;
break;
}else{
$response[0] = ‘0’;
$response[1] = ”;
}
}
return $response;
}
foreach($main_array as $main_key=>$main_value){
$res = search($main_value->key, $main_value->name, $final_array);
if(!empty($final_array) && $res[0]==1){
$final_array[$res[1]]->value = $final_array[$res[1]]->value+$main_value->value;
}else{
$final_array[] = $main_value;
}
}
echo “<pre>”;
print_r($final_array);
Ques: What is default session time in PHP ?
Ans: 24 minutes.
Ques: What is difference between unset() and unlink() ?
Ans: The unset() function will unset the variable i.e. it will make the variable undefined while the unlink() function is used to delete the files.
Ques: What is type casting ?
Ans: Changing the value from one format to another.
For example – There is an array and we have to convert it an object then –
Suppose – $array = array(“1″,”2”);
To convert it into object –
(object)$array;
Ques: What is difference between Abstraction and Interface ?
Ans: 1.) In abstraction, it is not necessary that all methods should be abstracted but in Interface every method should be abstracted.
2.) Interface methods should be public but abstraction methods can be public or protected.
3.) Inheritance supports both multiple and multilevel interface but abstraction supports single and multilevel inheritance.
4.) In interface, methods can only be defined. But in abstraction, methods can be defined as well as declared.
Ques: What is difference between PHP 5 and PHP 7 ?
Ans: 1.) Return Type – In PHP 7, type(float, int, other) of returning value can be defined.
For example –
public function subtract(float $a, float $b) : float
{
return $a-$b;
}
2.) Null Coalesce Operator (??) – The coalesce operator(??) return result of its first operand if it exists or null.
For example –
if(isset($_GET[‘id’])){
$id = $_GET[‘id’];
}else{
$id = null;
}
In PHP 7 –
$id = $_GET[‘id’]?? null;
Ques: What is PDO ?
Ans: PDO stands for PHP Data Objects. It is consitent interface to access database.
Ques: What is persistent cookie ?
Ans: Persistent cookies help to remember info., settings, preferences or login details.
Persistent cookie have some expiration date.
Example – Google Analytics cookie is the best example of persistent cookie which tracks the visitors when they move through the website.
Ques: How to get the ip address of the client ?
Ans: $_SERVER[‘REMOTE_ADDR’] can be used to get that.
Ques: Write a program to check that a given string is palindrome or not without using second variable.
Ans: Program –
function palindrome($str){
if(strlen($str)==0 || strlen($str)==1){
return “It is palindrome”;
}else{
if(substr($str,0,1) == substr($str,(strlen($str)-1),1)){
return palindrome(substr($str,1,(strlen($str)-2)));
}else{
return “It is not palindrome”;
}
}
}
$string = “NITIN”;
echo palindrome($string);
Ques: How will you reverse the words in a given string ?
Input – “garry’s giraffe gobbled goosberries greedily”
Output – “greedily goosberries goobled giraffe garry’s”
Ans: Program –
$input = “garry’s giraffe gobbled goosberries greedily”;
$inputarray = explode(” “,$input);
$output = ”;
for($i=count($inputarray)-1;$i>=0;$i–){
$output.= ‘ ‘.$inputarray[$i];
}
echo $output;
Ques: Convert string into new string :
Input : abcD
Output: cdeF
Ans: Program –
$input = “abcD”;
$output = “”;
for($i=0; $i<strlen($input);$i++){
$ascii=ord($input[$i]); //find ascii value of character
$output.= chr($ascii+2); //add 2 to ascii value and then print character at that value
}
echo $output;
Ques: What is difference between array_merge() and array_combine() ?
Ans: array_merge() function merges the elements of the array in a single array.
For Example –
$array1 = array(1,”name”=>”Ram”);
$array2 = array(1,”position”=>”God”);
$final_array = array_merge($array,$array2);
When you print $final_array – print_r($final_array),
Output –
Array([0]=>1 [name]=>”Ram” [1]=>2 [position]=>”God”);
array_combine() function takes two array and creates the final array from them by using one array values as key and other array values as values of that keys.
$array1 = array(“name”,”position”);
$array2 = array(“Ram”,”God”);
$final_array = array_combine($array,$array2);
When you print $final_array – print_r($final_array),
Output –
Array([name]=>”Ram” [position]=>”God”);
Ques: What will the output of $x = true and false; var_dump($x) ?
Ans: true. In this, = takes precedence over and operator i.e. it becomes –
$x = true; //set value of x to be true
true and false.
Ques: What will be he output of –
$x = ‘php’; echo $x++;
Ans: php
Ques: Can we add third party site in our site in iframe or any code ?
Ans: No. There are server setting which the site owner can change that if site origin is their site then it will work.
Ques: Difference between http and https.
Ans: 1.) Http uses port number 80 while https uses 443.
2.) Http works at Application layer while Https works at transport layer.
3.) In Http, Encryption is absent while in Https, encryption is present.
Ques: How to define constant in PHP ?
Ans: define is used to define constant.
define(“constant name”,”constant value”);
Ques: Write a program to print –
***********
*********
*******
*****
***
*
Ans: $space = 0; $star = 11;
for($i=1; $i<=6; $i++){
for($j=1; $j<=$space+$star; $j++){
if($j<=$space){
echo ” “;
}else{
echo “*”;
}
}
echo “<br>”;
$space++; $star = $star-2;
}
For more related queries, please visit.
In case, if you did not find your solution or have any query/question, please contact us.