ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

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

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.
Submitted by Nidhi, on January 25, 2021

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();
?>

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.

Question 2:

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

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.

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";
?>

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.

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";
?>

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.

Question 5:

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

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.



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.