Variable Scope In PHP

Filed in php-lesson1(variables) 2 comments

Scope is a technical term which exist in every programming language, that provide a rule for name of the variables has the same meaning in two difrent places

any php variable not inside a function has global scope,meaning that if you assign avariable inside of a PHP file it will have the same meaning for the rest of the file, but if you assignethe variable inside the function it will loose its value when it go out of the function execution.

when you assign a variable inside a PHP file it will not affect other variables with the same name in other PHP files.Forexample lets say you have a file next_page.php and the toher footer.php and we have a variable $username that is in one of the lines in footer.php in the body of the file so it has the global scope for that file
$username = “mike”
so you my think after visiting that footer.php page and then going to the nex_page.php, the value of the variable will still existed.but you are wrong, because each time a PHP page executes and assigns and reassigns variabels, those variables disappear at the end of the page.

so assigning variables in one page will not affect the variables with the same name in other pages or even for other reuests for the same file

there are some situations you may want to hold the information for longer to cary to other pages even with the same page such is username or all the rest so how to accomplsih this you may ask yourself. there are methods in php to hod the such information,one is to use database to save the info on the database,another is to use coockis save on the machine of the client,using PHP’s session mechanism,or use the GET and POST methods to pass information.these that I mentioned are the main usage of the PHP that without them PHP is such a disabled person. I will discuss each of them in more detal later on.

voshka

Related posts:

  1. testing if a variable is assigned in PHP with isset() function
  2. PHP Variables Basic
  3. Reassigning Variables in PHP
Posted by voshka   @   4 March 2010 2 comments

Share This Post

RSS Digg Twitter StumbleUpon Delicious Technorati

2 Comments

Comments
Mar 9, 2010
6:01 pm

Hm hm.. that’s interessting but honestly i have a hard time visualizing it… I’m wondering what others have to say….

Author Mar 9, 2010
6:33 pm
#2 voshka :

what section you couldn’t be undrstand or visualize it in your mind :D so that i could be able to demonstrate that point better

Leave a Comment

Previous Post
«
Next Post
»
CrossBlock designed by DeltaManual.Com  |  In conjunction with Web Hosting   |   Web Hosting   |   Reverse phone