Tags

, , , , , ,

DSCN0727//embedr.flickr.com/assets/client-code.js

For the most part, PHP variables have the single scope. It happens because PHP variables represent pointers that hold the addresses of memory areas with data. These data are actually assigned to the variables.

object-oriented-design-patterns-in-php7 for web

object-oriented-design-patterns-in-php7 for web


   

You cannot retain that value through functions. Any variable, used inside a function, is by default limited to the local function scope.

I’m a Full-Time writer. If you find these articles helpful please consider buying any of my books.
You can buy from either Leanpub or
Amazon or from my publisher
Apress.

However, we can solve these problems.
First, we see how things work locally and globally.


                    $value = 12;
                
                echo "We are calling value : " . $value;
                
                echo "Calling function that could not take outside value.;
                localFunc();
                echo "Calling function that can take outside value;
                globalFunc();
                
                function localFunc() {
                    
                    $value = 24;
                    
                    echo "This is inside value, defined differently : " . $value;
                    
                }
                
                function globalFunc() {
                    
                    global $value;
                    
                    echo "We are accssing outside value through this function : " . $value;
                
                }
                
Here is the output:

We are calling value : 12
Calling function that could not take outside value.
This is inside value, defined differently : 24
Calling function that can take outside value.
We are accssing outside value through this function : 12

Now consider this code again:


                //normally function does not change value
                // the value inside them is reset every time you call it
                
                function NormalFunction() {
                    
                    $value = 0;
                    $value++;
                    return $value;
                    
                }
                
                // now if we call this function many times, each time the value is reset to
                // the old value
                
                echo NormalFunction();
               
                echo NormalFunction();
               
                echo NormalFunction();
               
                echo NormalFunction();
               
                
                // how can we solve this problem?
                // the first way
                
                echo 'Using Global';
                function GlobalFunction() {
                    
                    global $globalvalue;
                    $globalvalue++;
                    return $globalvalue;
                
                }
                
               
                echo GlobalFunction();
               
               
                echo GlobalFunction();
               
               
                echo GlobalFunction();
               
               
                echo GlobalFunction();
               
                
                
                // the second way
                
                echo 'Using Static';
                
                function StaticValueFunction() {
                    
                    static $staicValue = 0;
                    $staicValue++;
                    return $staicValue;
                    
                }
                
               
                echo StaticValueFunction();
               
               
                echo StaticValueFunction();
               
               
                echo StaticValueFunction();
               
               
                echo StaticValueFunction();
                  
Here is the output:

1
1
1
1
Using Global
1

2
3
4
Using Static
1
2
3
4