05 June 2012

PHP Interview Questions - VI

 

1. What will be the out put of the above code?

 <?php

    $s = 2;

    function Test()
    {
        echo "s = $s";
    }
    Test();
?>
 

Answers :

 
 
a)  1
 
b)  2
 
c)  3
 
d)  No value
 

Explanation :

 
The variable $s have only local scope. In order to reference 
 
a global variable from a function the "global" keyword must 
 
be used inside the function.
 

2. What will be the out put of the above code?

<?php

      $x=array("aaa","ttt","www","ttt","yyy","tttt","www");

     $y=array_count_values($x);

     echo $y[ttt];
?> 

Answers :


a)  2

b)  3

c)  1

d)  4



Explanation : 

 

print_r(array_count_values($x )) will print the following array go through it then

you can understand what is happening with  array_count_values($x)

Array
(
    [aaa] => 1
    [ttt] => 2
    [www] => 2
    [yyy] => 1
    [tttt] => 1
 
 )

I hope you get the working if you are not clear with this answer mail me



3. php code can be directly embeded to XHTML document?

 

Answers :

 

a)  true

 

b)  false

 

 

4. What will be printed?

 

<?php

    FUNCTION TEST()
    {
        ECHO "HELLO WORLD!\n";
    }

    test();
?>
 

Answers :

 

a)   HELLO WORLD!

b)   nothing 

c)    its a compiler error, the code won't run 

d)    hello world!  
 

 

Explanation :

 

User defined functions and language key words are case in sensitive



4. In php which method is used to get the browser properties?

 

Answers :

 

a)  $_SERVER['HTTP_USER_AGENT']

b)  $_SERVER['PHP_SELF'] 

c)   $_SERVER['SERVER_NAME'] 

d)  $_SERVER['HTTP_VARIENT']

 

4.  Which of the following function is used to pick one or more random values from an array?

 

Answers :

 

a)  array_rand() 
b)  array_random() 
c)  Random_array() 
d)  Rand_array()

 

Explanation : 

 

array_rand(source_array , number);
source_array is the array from which the values are selected, it is compulsory 
and number is optional default value taken as 1 we can give the number as we wish
<?php
$a=array("a"=>"Prem","b"=>"Aag","c"=>"Horse");
print_r(array_rand($a,2));
?>   
 
the out put will be as follows and may different 
Array ( [0] => c [1] => b ) 

 

 

No comments:

Post a Comment