PHP Questions & Answers – Basics – 3

This set of PHP Questions and Answers for Freshers focuses on “Basics – 3”.

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

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

2. Which of the below symbols is a newline character?
a) \r
b) \n
c) /n
d) /r
View Answer

Answer: b
Explanation: PHP treats \n as a newline character.

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

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
  1.     <?php
  2.     $num = 10;
  3.     echo 'What is her age? \n She is $num years old';
  4.     ?>

a) What is her age? \n She is $num years old
b)

What is her age?
She is $num years old
advertisement

c) What is her age? She is 10 years old
d)

What is her age?
She is 10 years old
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.
 
 

4. Which of the conditional statements is/are supported by PHP?

i) if statements
ii) if-else statements
iii) if-elseif statements
iv) switch statements
advertisement

a) Only i)
b) i), ii) and iv)
c) ii), iii) and iv)
d) i), ii), iii) and iv)
View Answer

Answer: d
Explanation: All are conditional statements supported by PHP as all are used to evaluate different conditions during a program and take decisions based on whether these conditions evaluate to true of false.

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

  1.     <?php
  2.     $team = "arsenal";
  3.     switch ($team) {
  4.     case "manu":
  5.         echo "I love man u";
  6.     case "arsenal":
  7.         echo "I love arsenal";
  8.     case "manc":
  9.         echo "I love manc"; }
  10.     ?>

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.

6. Which of the looping statements is/are supported by PHP?

i) for loop
ii) while loop
iii) do-while loop
iv) foreach loop

a) i) and ii)
b) i), ii) and iii)
c) i), ii), iii) and iv)
d) Only iv)
View Answer

Answer: c
Explanation: All are supported looping statements in PHP as they can repeat the same block of code a given number of times, or until a certain condition is met.

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

  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.

8. If $a = 12 what will be returned when ($a == 12) ? 5 : 1 is executed?
a) 12
b) 1
c) Error
d) 5
View Answer

Answer: d
Explanation: ?: is known as ternary operator. If condition is true then the part just after the ? is executed else the part after : .

9. What will be the value of $a and $b after the function call in the following PHP code?

  1.     <?php
  2.     function doSomething( &$arg ) {
  3.         $return = $arg;
  4.         $arg += 1;
  5.         return $return;	
  6.     }
  7.     $a = 3;
  8.     $b = doSomething( $a );
  9.     ?>

a) a is 3 and b is 4
b) a is 4 and b is 3
c) Both are 3
d) Both are 4
View Answer

Answer: b
Explanation: $a is 4 and $b is 3. The former because $arg is passed by reference, the latter because the return value of the function is a copy of the initial value of the argument.

10. Who is the father of PHP?
a) Rasmus Lerdorf
b) Willam Makepiece
c) Drek Kolkevi
d) List Barely
View Answer

Answer: a
Explanation: PHP was originally created by Rasmus Lerdorf in 1994.

Sanfoundry Global Education & Learning Series – PHP Programming.

To practice all areas of PHP for Freshers, 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.