recent posts

PHP Program 2 : Write a PHP script to demonstrate use of global and local and static and constant variables.

 Software Required: - Notepad, WordPad, Notepad++, Dreamweaver,Wamp/Xampp

Pre-requisite: - Basic knowledge of editor

Theory/Logic: -

 v Constants: -

Ø Syntax: -

       define (name, value, case-insensitive)

Ø name: Specifies the name of the constant

Ø value: Specifies the value of the constant

Ø Case-insensitive: Specifies whether the constant name should be case-insensitive.

Ø Default is false.


v Static Variable

Ø Syntax: -

       Static $VariableName = value;


 v Code: -

<html>

    <head><title>PHP Program</title></head>

    <body>

            <?php

            $x = 40;

            define("PIE", "3.14");

            function inc()

            {

                        static $y = 1;

                        echo "Inside inc() y is now : $y<br /><br />";

                        $y++;

            }

            function myTest() {

            $x = 19;

            echo "<p>Local Variable x inside function is: $x</p>";

            echo "Static Variable y inside function, ";

            inc();

            echo "Constant Variable z inside function is: ".PIE;

            }

            myTest();

            echo "<p>Global Variable x outside function is: $x</p>";

            echo "Static Variable y outside function, ";

            inc();

            echo "Constant Variable z outside function is: ".PIE;

            ?>

    </body>

</html>


 v Output: -





















PHP Program 2 : Write a PHP script to demonstrate use of global and local and static and constant variables. PHP Program 2 : Write a PHP script to demonstrate use of global and local and static and constant variables. Reviewed by Abhishek Nalla on February 07, 2022 Rating: 5

No comments:

Powered by Blogger.