Monday, October 26, 2009

PHP-Interview Questions

1. In PHP, if $x = 'Sunil' what is the output from "echo 'Hello $x'" ?

Ans : Hello $x

2. What value is $x in this PHP section of code "function foo ($i) {return ((($i*2)-3)%3); } $x = foo(6);"

Ans : 0

3. In PHP, what is the value of $x when '$x = 10 - "2 elephants";'

Ans : 8

4. In PHP, if $a is TRUE and $b is TRUE, what is ($a xor $b)?.

Ans : False

5. What will the output of this PHP code be 'function x () { function y()
{ return "y"; } return "x"; } echo y();'

Ans: Fatal error: Call to undefined function: y()

6. If table x has an integer column y and there are three rows in the
table with values 45, 34 and 1, what is the output of this MySQL
statement "select count(*), count(y), sum(y), min(y), round(avg(y),2) from x;"

Ans : 3 3 80 1 26.67

7. With a table x (y int(10), z int(10)) having data of 1 1, 2 1, 3 4, 4 2, 5 1 (where first number is y, second number is z (there are 5 rowsof data)), what is the output of this MySQL statement "select count(y) from x group by z having count(y) > 1;"

Ans: 4

8. What is the MySQL output of this statement "select date_add('2004-10-11',interval -2 month);"

Ams: 2004-08-11

9. If the current date/time is 2004-10-14 13:05:34, what would be the output of the MySQL statement "select date_format(sysdate(),'%b-%Y-%d-%i');"

ANS :October-2004-14-13

10.Is it possible to set a time expire page in PHP.?

Yes it is

Using header(“Expires: fri, 07 mar 2007 05:00:00 GMT”);

11.How can we SAVE an image from a remote web Server to my web server using PHP?

$file_rimg = fopen("http://w3answers /image23.jpg",'rb');
$newfile_name_img = "/tmp/tutorial.file";
$file_wnew = fopen($newfile_name_img,'wb');
while (!feof($file_rimg)) {
$chunk_rd = fread($file_rimg,1024);

12.What is the output of 2^2 in php ?

The answer is 0 (Zero)
Important note
Everyone expected answer would be 4. But answer is zero. How it happened only in php ?

The ^ operator is different in each language.In PHP ^ means the bitwise exlusive or of the two numbers.

13.What is the output of below script?

a. echo ‘line 3′;
b. echo ‘line 2′;
c. Error
d. None of the above

Ans: b (Answer is line2)
14.What is the output here?

$x = ‘raj’;
echo ‘Hello $x’;


a. helloravj
b. Parse error
c. hello $x
d. syntax error

ANS: c

15.What output do you get here?

$list = array(“block”,”cut”,”pens”,”dogs”);
$list[] = “elephant”;
$list[] = “dragon”;
print “$list[4]“;

a. Error
b. elephant
c. dragon
d. nothing
e. dogs

ANS: b (elephant)

16.what is the output for following code?

echo 12+FALSE;

a. 12
b. no
c. parse error
d. T_ECHO error

ANS: 12

17.What is the output ?

if ($x < 2) { echo “11″; }
elseif ($x < 16) { echo “12″; }
elseif ($x < 14) { echo “13″; }
elseif ($x > 14) { echo “14″; }
elseif ($x < 10) { echo “15″; }
else { echo “16″; }




18.What is the result here?$x = '';
switch ($x) {
case '0': echo 'String'; br;
case 0: echo ''; break;
case NULL: echo 'NULL'; br;
case FALSE: echo 'heeder'; br;
case '': echo 'no string'; br;
default: echo 'nothing else'; br;


a. Something else

b. Empty string
c. Integer
d. String

ANS: b[case 0: echo ''];

19.What is the output?

function x ($y) {
function y ($z) {
return ($z*2); }
return($y+3); }
$y = 4;
$y = x($y)*y($y);
echo “$y”;

a. None
b. 54
c. 56
d. 58


Friday, October 23, 2009


Create .xls file using PHP

include('db.php');//db connection
$result = mysql_query('select * from care_billing_bill');
$count = mysql_num_fields($result);

for ($i = 0; $i < $count; $i++){
$header .= mysql_field_name($result, $i)."\t";

while($row = mysql_fetch_row($result)){
$line = '';
foreach($row as $value){
if(!isset($value) || $value == ""){
$value = "\t";
# important to escape any quotes to preserve them in the data.
$value = str_replace('"', '""', $value);
# needed to encapsulate data in quotes because some data might be multi line.
# the good news is that numbers remain numbers in Excel even though quoted.
$value = '"' . $value . '"' . "\t";

$line .= $value;
$data .= trim($line)."\n";
# this line is needed because returns embedded in the data have "\r"
# and this looks like a "box character" in Excel
$data = str_replace("\r", "", $data);

# Nice to let someone know that the search came up empty.
# Otherwise only the column name headers will be output to Excel.
if ($data == "") {
$data = "\nno matching records found\n";

header("Content-type: application/x-msdownload");
# This line will stream the file to the user rather than spray it across the screen
header("Content-Type: application/; name='excel'");
//header("Content-Type: application/; name='word'");
header("Content-Disposition: attachment; filename=excelfile.xls");
//header("Content-Disposition: attachment; filename=excelfile.doc");
header("Pragma: no-cache");
header("Expires: 0");

echo $header."\n".$data;

//print "done";


MySQL Tips

We have seen SQL SELECT command along with WHERE clause to fetch data from MySQL table. But when we try to give a condition which compare field or column value to NULL it does not work properly.

To handle such situation MySQL provides three operators

IS NULL: operator returns true of column value is NULL.

IS NOT NULL: operator returns true of column value is not NULL.

<=> operator compare values, which (unlike the = operator) is true even for two NULL values



id name







mysql> SELECT * FROM tbl WHERE name= NULL;
Empty set (0.00 sec)
mysql> SELECT * FROM tbl WHERE name != NULL;
Empty set (0.01 sec)

mysql> SELECT * FROM tbl WHERE name IS NULL;
Output :
Id Name

mysql> SELECT * from tbl WHERE name IS NOT NULL;
Output :
Id Name

What is the use of i-am-a-dummy-flag in mysql?
It makes mysql engine to refuse UPDATE,DELETE command where WHERE clause is not present.

Friday, August 22, 2008

Extract a RAR file in your linux machine

Inorder to extract a RAR file in your linux machine, it is required to install UNRAR. Hence please follow the below steps:

Under Debian Linux, you need to type apt-get as follows to install unrar program:
# apt-get install unrar

If you are using Fedora core Linux then use yum command :
# yum install unrar

If you are using FreeBSD:
# pkg_add -v -r unrar

If any of above, methods is not working for you, download binary package from official rarlab site:
$ cd /tmp
$ wget

Untar file
$ tar -zxvf rarlinux-3.7.1.tar.gz

Both unrar and rar commands are located in rar sub-directory. Just go to rar directory:
$ cd rar

Now copy rar and unrar to /bin directory:
# cp rar unrar /bin

Task: To open rar (unpack) file in current directory type command:

$ unrar e file.rar

Task: List (l) file inside rar archive:

$ unrar l file.rar

Task: To extract (x) files with full path type command:

$ unrar x file.rar

(D) To test (t) integrity of archive, file type command:
$ unrar t file.rar