1. Which of the following PHP statements will output Hello World on the screen?
i) echo ("Hello World"); ii) print ("Hello World"); iii) printf ("Hello World"); iv) sprintf ("Hello World");
A. i) and ii)
B. i), ii) and iii)
C. i), ii), iii) and iv)
D. i), ii) and iv)
Answer:-B. i), ii) and iii)
Explanation: echo(), print() and printf() all three can be used to output a statement onto the screen. The sprintf() statement is functionally identical to printf() except that the output is assigned to a string rather than rendered to the browser.
2. What will be the output of the following PHP code?
- <?php
- $color = “maroon”;
- $var = $color[2];
- echo “$var“;
- ?>
A. a
B. Error
C. $var
D. r
Answer:-D. r
Explanation: PHP treats strings in the same fashion as arrays, allowing for specific characters to be accessed via array offset notation. In an array, index always starts from 0. So in the line $var = $color[2]; if we count from start ‘r’ comes at index 2. So the output will be r.
3. What will be the output of the following PHP code?
- <?php
- $score = 1234;
- $scoreboard = (array) $score;
- echo $scoreboard[0];
- ?>
A. 1
B. Error
C. 1234
D. 2
Answer:-C. 1234
Explanation: The (array) is a cast operator which is used for converting values from other data types to array.
4. What will be the output of the following PHP code?
- <?php
- $total = “25 students”;
- $more = 10;
- $total = $total + $more;
- echo “$total“;
- ?>
A. Error
B. 35 students
C. 35
D. 25 students
Answer:-C. 35
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.
5. Which of the below statements is equivalent to $add += $add?
A. $add = $add
B. $add = $add +$add
C. $add = $add + 1
D. $add = $add + $add + 1
Answer:-B. $add = $add +$add
Explanation: a += b is an addition assignment whose outcome is a = a + b. Same can be done with subtraction, multiplication, division etc.
6. Which statement will output $x on the screen?
A. echo “\$x”;
B. echo “$$x”;
C. echo “/$x”;
D. echo “$x;”;
Answer:-A. echo “\$x”;
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.
7. What will be the output of the following PHP code?
- <?php
- function track() {
- static $count = 0;
- $count++;
- echo $count;
- }
- track();
- track();
- track();
- ?>
A. 123
B. 111
C. 000
D. 011
Answer:-A. 123
Explanation: Because $count is static, it retains its previous value each time the function is executed.
8. What will be the output of the following PHP code?
- <?php
- $a = “clue”;
- $a .= “get”;
- echo “$a“;
- ?>
A. get
B. true
C. false
D. clueget
Answer:-D. clueget
Explanation: ‘.’ is a concatenation operator. $a. = “get” is same as $a=$a.”get” where $a is having value of “clue” in the previous statement. So the output will be clueget.
9. What will be the output of the following PHP code?
- <?php
- $foo = ‘Book’;
- $bar = &$foo;
- $bar = “My name is $bar“;
- echo $bar;
- echo $foo;
- ?>
A. Error
B. My name is BookBook
C. My name is BookMy name is Book
D. My name is Book Book
Answer:-C. My name is BookMy name is Book
Explanation: Firstly, the line $bar = &$foo; will reference $foo via $bar. So $bar is assigned value Book. Therefore $bar = “My name is $bar”; will print My name is Book($bar=Book as said before).
10. Which is the right way of declaring a variable in PHP?
i) $3hello ii) $_hello iii) $this iv) $This
A. Only ii)
B. Only iii)
C. ii), iii) and iv)
D. ii) and iv)
Answer:-D. ii) and iv)
Explanation: A variable in PHP can not start with a number, also $this is mainly used to refer properties of a class so we can’t use $this as a user define variable name.
Leave a Reply