PHP Excersise
PHP Excersise
**************************************************************************
1. <?php
2. $num = "1";
3. $num1 = "2";
4. print $num+$num1;
5. ?>
a) 3
b) 1+2
C) Error
d) 12
1. <?php
2. $total = "25 students";
3. $more = 10;
4. $total = $total + $more;
5. echo "$total";
6. ?>
a) Error
b) 35 students
c) 35
d) 25 students
View Answer
Answer: c
Explanation:The integer value at the beginning of the original $total string is used in the
calculation. However if it begins with anything but a numerical value, the value will be 0.
Answer: b
Explanation: a += b is an addition assignment whose outcome is a = a + b. Same can be done
with subtraction,multiplication,division etc.
Answer: a
Explanation:A backslash is used so that the dollar sign is treated as a normal string character
rather than prompt PHP to treat $x as a variable. The backslash used in this manner is known as
escape character.
1. <?php
2. $a = "clue";
3. $a .= "get";
4. echo "$a";
5. ?>
a) get
b) true
c) false
d) clueget
View Answer
Answer: d
Explanation: .= is a concatenation-assignment. $a equals its current value concatenated with
“get”.
1. <?php
2. $a = 5;
3. $b = 5;
4. echo ($a === $b);
5. ?>
a) 5 === 5
b) Error
c) 1
d) False
View Answer
Answer: c
Explanation: === operator returns 1 if $a and $b are equivalent and $a and $b have the same
type.
Answer: b
Explanation:PHP treats \n as newline character.
1. <?php
2. echo 'can i go there? \n no i cant.';
3. ?>
a) can i go there? \n no i cant.
b) can i go there?
no i cant.
c) can i go there? no i cant.
d) none
View Answer
Answer: a
Explanation: When a string is enclosed within single quotes both variables and escape sequences
will not be interpreted when the string is parsed.
a) I love arsenal
b) Error
c) I love arsenalI love manc
d) I love arsenalI love mancI love manu
View Answer
Answer: c
Explanation:If a break statement isn’t present, all subsequent case blocks will execute until a
break statement is located.
1. <?php
2. $user = array("Ashley", "Bale", "Shrek", "Blank");
3. for ($x=0; $x < count($user); $x++) {
4. if ($user[$x] == "Shrek") continue;
5. printf ($user[$x]);
6. }
7. ?>
a) AshleyBale
b) AshleyBaleBlank
c) ShrekBlank
d) Shrek
View Answer
Answer: b
Explanation: The continue statement causes execution of the current loop iteration to end and
commence at the beginning of the next iteration.
Answer: d
Explanation: ?: is known as ternary operator. If condition is true then the part just after the ? is
executed else the part after : .
Which one of the following is the very first task executed by a session enabled page?
a) Delete the previous session
b) Start a new session
c) Check whether a valid session exists
d) Handle the session
View Answer
Answer: c
Explanation:None.
Answer: d
Explanation:Its prototype follows: session.save_handler = files|mm|sqlite|user.
What is the default number of seconds that cached session pages are made available before the
new pages are created?
a) 360
b) 180
c) 3600
d) 1800
View Answer
Answer: b
Explanation:The directive which determines this is session.cache_expire.
Answer: b
Explanation:None
Answer: c
Explanation: Like many programming languages, the first element of an array has an index value
of 0.
Answer: a
Explanation:A variable name should start with $ symbol which is not present in i) and you need
not put the square brackets when you use the array() constructor.
1. <?php
2. $fruits = array ("apple", "orange", "banana");
3. echo (next($fruits));
4. echo (next($fruits));
5. ?>
a) orangebanana
b) appleorange
c) orangeorange
d) appleapple
View Answer
Answer: a
Explanation: The next() function returns the array value residing at the position immediately
following that of the current array pointer.
Answer: c
Explanation:None
1. <?php
2. $x = 5;
3. $y = 10;
4. $z = "$x + $y";
5. echo "$z";
6. ?>
a) 15
b) 10 + 5
c) $z
d) $x + $y
View Answer
Answer: b
Explanation: Variable z will store 10 + 5 because 10 + 5 is given in double-quotes.
1. <?php
2. $x = 4;
3. $y = 3;
4. $z = 1;
5. echo "$x = $x + $y + $z";
6. ?>
a) 4 = 4 + 3 + 1
b) 8
c) 8 = 4 + 3 +1
d) Error
View Answer
Answer: a
Explanation: Again since the variables are inside double quotes we get this result.
1. <?php
2. $x = 4;
3. $y = 3
4. $z = 1;
5. $z = $z + $x + $y;
6. echo "$z";
7. ?>
a) $z
b) 15
c) 8
d) 1
View Answer
Answer: c
Explanation: Normal addition of variables x, y and z occurs and result of 8 will be displayed.
1. <?php
2. $x = 3.3;
3. $y = 2;
4. echo $x % $y;
5. ?>
a) 0
b) 1
c) 2
d) Error
View Answer
Answer: b
Explanation: % is the modulo operator. Unlike in C we can use it get reminder or floating point
numbers in PHP.
1. <?php
2. $x = 10;
3. $y = 4;
4. $z = 3;
5. echo $x % $y % $z;
6. ?>
a) 0
b) 1
c) 2
d) Error
View Answer
Answer: c
Explanation: The expression is considered as ($x%$y)%z in this case (10%4)%3 which is 2.
1. <?php
2. $x = 10;
3. $y = 4;
4. $z = 3;
5. echo ($x % ($y) + $z);
6. ?>
a) 5
b) 3
c) 0
d) 1
View Answer
Answer: a
Explanation: The inner most bracket is evaluated first, since it covers only variable y it is as good
as not using brackets.
What will be the output of the following PHP code ?
1. <?php
2. $x = 30;
3. $y = 20;
4. $z = 10;
5. echo $x + $y - $z / ($z - $y);
6. ?>
a) 41
b) -4
c) -5
d) 51
View Answer
Answer: d
Explanation: First ($z – $y) is evalulated then -$z/($z – $y) is evaluated this results in 1 which is
added to $x + $y therefore we get 51.
1. <?php
2. $x = -1;
3. $y = 1;
4. $z = $x * $y + $z;
5. echo $z;
6. ?>
a) Undefined variable z
b) -1
c) Undefined variable z
-1
d) None of the above
View Answer
Answer: c
Explanation: Since the variable z is not defined it returns the error also it takes z as 0 and returns
the value -1.
1. <?php
2. $x = 4;
3. $y = -3;
4. $z = 11;
5. echo 4 + $y * $z / $x;
6. ?>
a) 4.25
b) 3.25
c) -3.25
d) -4.25
View Answer
Answer: d
Explanation: First the * is evaluated then / followed by + therefore we can rewrite this expression
as 4 +((- 3 * 11) / 4) which results in -4.25.
1. <?php
2. $x = 3.5;
3. $y = 2;
4. $z = 2;
5. echo $x / $y / $z;
6. ?>
a) 1.75
b) 0.875
c) 3.5
d) Error
View Answer
Answer: b
Explanation: First $x / $y is evaluated then this is divided by $z therefore we get 0.875