PHP Quizs Question & Answers – Basics 2

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?

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

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?

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

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?

  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

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?

  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

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?

  1. <?php
  2. $a = “clue”;
  3. $a .= “get”;
  4. echo “$a“;
  5. ?>

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?

  1. <?php
  2. $foo = ‘Book’;
  3. $bar = &$foo;
  4. $bar = “My name is $bar“;
  5. echo $bar;
  6. echo $foo;
  7. ?>

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.


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page