Q:

PHP find output programs (static variables, classes, methods) | set 2

belongs to collection: PHP Find Output Programs

0

Find the output of PHP programs | static variables, classes, methods | Set 2: Enhance the knowledge of PHP static variables, classes, methods concepts by solving and finding the output of some PHP programs.

Question 1:

<?php
    class Sample
    {
        private $count = 0;
        public static function AddCount()
        {
            self::$count = self::$count + 1;
        }
    
        public static function PrintCount()
        {
            echo "<br>Count is: " . self::$count;
        }
    }
    
    Sample::AddCount();
    Sample::AddCount();
    Sample::AddCount();
    
    Sample::PrintCount();
?>

Question 2:

<?php
    function fun()
    {
        static $VAR = 0;
    
        $VAR = $VAR + 1;
    
        return $VAR;
    }
    
    fun();
    fun();
    $count = fun();
    
    echo "Function fun() called " . $count . " times";
?>

Question 3:

<?php
    static class sample
    {
        function fun()
        {
            static $VAR = 0;
    
            $VAR = $VAR + 1;
    
            return $VAR;
        }
    }
    
    Sample::fun();
    Sample::fun();
    
    $count = Sample::fun();
    
    echo "Funtion fun() called " . $count . " times";
?>

Question 4:

<?php
    class Sample
    {
        function fun()
        {
            static $VAR = 0;
            $VAR = $VAR + 1;
    
            return $VAR;
        }
    }
    
    Sample::fun();
    Sample::fun();
    
    $count = Sample::fun();
    
    echo "Funtion fun() called " . $count . " times";
?>

Question 5:

<?php
    static $VAR = 0;
    
    function fun()
    {
        $VAR = $VAR + 1;
        return $VAR;
    }
    
    fun();
    fun();
    fun();
    
    echo "Funtion fun() called " . $VAR . " times";
?>

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer 1:

Output:

PHP Fatal error:  Uncaught Error: Access to undeclared static property: 
Sample::$count in /home/main.php:7
Stack trace:
#0 /home/main.php(16): Sample::AddCount()
#1 {main}
  thrown in /home/main.php on line 7

Explanation:

The above program will generate syntax error because we cannot access a non-static member of a class form static function.

Answer 2:

Output:

Function fun() called 3 times

Explanation:

In the above program, we created a function fun() that contains a static variable $VAR and increased by 1 on every function call, and return the value. We called function fun() three times then "Function fun() called 3 times" message will be printed on the webpage.

Answer 3:

Output:

PHP Parse error:  syntax error, unexpected 'class' (T_CLASS), 
expecting :: (T_PAAMAYIM_NEKUDOTAYIM) in /home/main.php
on line 2

Explanation:

The above program will generate syntax error because we cannot create declared a class as static in PHP.

Answer 4:

Output:

Function fun() called 3 times

Explanation:

In the above program, we created a class Sample that contains a function fun(). The function fun() that contains a static variable $VAR and increased by 1 on every function call, and return the value. We called function fun() three times then "Function fun() called 3 times" message will be printed on the webpage.

Answer  5:

Output:

Function fun() called 0 times

Explanation:

In the above program we defined a static variable $VAR, and increased $VAR by 1 in the function fun(), the static variable $VAR and variable $VAR inside the function fun() are different. That's why "Function fun() called 0 times" message will be printed on the webpage.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

PHP Find Output Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
PHP find output programs (Regular Expressions) | s... >>
<< PHP find output programs (static variables, classe...