Programming - PHP

11.

What will be the output of the following PHP code if date is 2021-10-11 ?

<?php
   echo date("t");
?>
Answer

Answer :

Option A

Explanation :


The t parameter is used to determine the number of days in the current month.
12.

What will be the output of the following PHP code?

<?php
   class Content {
       public function publish() {
           $this->published = true;
           $this->article();
           return true;
       }
       protected function article() {
           echo "Article:";
       }
   }
   class Article extends Content {
       public function article() {
           echo "Post:";
       }
   }
   $post = new Article();
   echo $post->publish(); 
?>
Answer

Answer :

Option B

Explanation :


We have a class Content and another class Article which extends Content. When we instantiate a new Article() the function article() becomes our constructor because the method name meets the class name (this is from PHP 4 days but is still true) so we echo "Post:". Then we call publish() on our object, which calls Article::article() again (NOT Content::article()), and returns true. We echoed the output of our call and the boolean becomes a 1 when we echo it.
13.

What will be the output of the following PHP code?

<?php
   echo date("M-d-Y", mktime(0, 0, 0, 12, 32, 1995));
?>
Answer

Answer :

Option B

Explanation :


Since the given input date is out of range, it will automatically correct it. Hence, the correct answer will be Jan-01-1996.
14.

What will be the output of the following PHP code?

<?php
   $s = "This sentence contains many words";
   $r = explode(' ', ucfirst($s));
   sort($r);
   echo implode(' ', $r); 
?>
Answer

Answer :

Option B

Explanation :


Applying ucfirst to $s makes no difference, but exploding on a space splits the sentence into an array with one word in each element. We sort the words but sort is case-sensitive and will sort the capital letter first, then the rest alphabetically.
15.

What will be the output of the following PHP code?

<?php
   $x = 3 + "15%" + "$25";
   echo $x; 
?>
Answer

Answer :

Option C

Explanation :


PHP supports automatic type conversion based on the context in which a variable or value is being used.
If you perform an arithmetic operation on an expression that contains a string, that string will be interpreted as the appropriate numeric type for the purposes of evaluating the expression. So, if the string begins with one or more numeric characters, the remainder of the string (if any) will be ignored and the numeric value is interpreted as the appropriate numeric type. On the other hand, if the string begins with a non-numeric character, then it will evaluate to zero.
With that understanding, we can see that "15%" evaluates to the numeric value 15 and "$25" evaluates to the numeric value zero.
Please note that as of PHP 7.2, this code may produce an error.
Jump to page number :