Programming - PHP

1.

What will be the output of the following PHP code?

<?php
   $num  = 1;
   $num1 = 2;
   echo $num . "+". $num1;
?>
Answer

Answer :

Option B

Explanation :


.(dot) is used to combine two parts of the statement.
2.

What will be the output of the following PHP code if it is loaded third time in a browser by the same user?

<?php
   session_start();
   if (!array_key_exists('counter', $_SESSION)) {
      $_SESSION['counter'] = 0;
   }
   else {
      $_SESSION['counter']++;
   }
   session_regenerate_id();
   echo $_SESSION['counter'];
?>
Answer

Answer :

Option C
3.

What will be the output of the following PHP code?

<?php 
   $a = array('a', 'b');  
   array_push($a, array(1,2));  
   print_r($a); 
?>
Answer

Answer :

Option B

Explanation :


The array_push() function is used to insert one or more elements to the end of an array.
4.

What will be the output of the following PHP code?

<?php 
   class A  {
      static $word = "hello";
      static function hello() {
         print static::$word;
      }
   }
   class B extends A  {
      static $word = "bye";
   }
   B::hello();
?>
Answer

Answer :

Option A

Explanation :


PHP implements a feature called late static bindings which can be used to reference the called class in a context of static inheritance. More precisely, late static bindings work by storing the class named in the last "non-forwarding call".
5.

What will be the output of the following PHP code?

<?php 
   switch (1) {
     case 1: echo "Book Details";
     case 2: echo "Book Author";
     default: echo "Book Missing";
   }
?>
Answer

Answer :

Option D

Explanation :


The given script will run successfully and will display all the three lines as output. The switch expression has the value 1, this is matched with the literal value specified in each case statement. The statement "Book Details" has the matching value, but since there is no break statement after this, all the statements after matching the literal value get executed. If a break statement was given after case 1:, the statement "Book Details" would have been displayed.
Jump to page number :