PHP Questions & Answers – Basics – 2

This set of PHP Questions and Answers focuses on “Basics – 2”.

1. 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)
View Answer

Answer: d
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.
advertisement
advertisement

2. What will be the output of the following PHP code?

  1.     <?php 
  2.     $foo = 'Bob';              
  3.     $bar = &$foo;              
  4.     $bar = "My name is $bar";  
  5.     echo $bar;
  6.     echo $foo;
  7.     ?>

a) Error
b) My name is BobBob
c) My name is BobMy name is Bob
d) My name is Bob Bob
View Answer

Answer: c
Explanation: Firstly, the line $bar = &$foo; will reference $foo via $bar. So $bar is assigned value Bob. Therefore $bar = “My name is $bar”; will print My name is Bob($bar=Bob as said before).

3. Which of the following PHP statements will output Hello World on the screen?

advertisement
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)
View Answer

Answer: b
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.
advertisement

4. What will be the output of the following PHP code?

  1.     <?php
  2.     $color = "maroon";
  3.     $var = $color[2];
  4.     echo "$var";
  5.     ?>

a) a
b) Error
c) $var
d) r
View Answer

Answer: d
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.

5. What will be the output of the following PHP code?

  1.     <?php
  2.     $score = 1234;
  3.     $scoreboard = (array) $score;
  4.     echo $scoreboard[0];
  5.     ?>

a) 1
b) Error
c) 1234
d) 2
View Answer

Answer: c
Explanation: The (array) is a cast operator which is used for converting values from other data types to array.

6. What will be the output of the following PHP code?

  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.

7. 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
View Answer

Answer: b
Explanation: a += b is an addition assignment whose outcome is a = a + b. Same can be done with subtraction, multiplication, division etc.

8. Which statement will output $x on the screen?
a) echo “\$x”;
b) echo “$$x”;
c) echo “/$x”;
d) echo “$x;”;
View Answer

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.

9. What will be the output of the following PHP code?

  1.     <?php
  2.     function track() {
  3.     static $count = 0;
  4.     $count++;
  5.     echo $count;
  6.     }
  7.     track();
  8.     track();
  9.     track();
  10.     ?>

a) 123
b) 111
c) 000
d) 011
View Answer

Answer: a
Explanation: Because $count is static, it retains its previous value each time the function is executed.

10. What will be the output of the following PHP code?

  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 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.

Sanfoundry Global Education & Learning Series – PHP Programming.

To practice all areas of PHP, here is complete set of 1000+ Multiple Choice Questions and Answers on PHP.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.