Download - Php variables
![Page 1: Php variables](https://reader036.vdocuments.net/reader036/viewer/2022083003/558ff5671a28abed798b45d0/html5/thumbnails/1.jpg)
PHP Variables
![Page 2: Php variables](https://reader036.vdocuments.net/reader036/viewer/2022083003/558ff5671a28abed798b45d0/html5/thumbnails/2.jpg)
TOC
• Declaring variables
• Variable scope
• Static Variable
• Reserved words
![Page 3: Php variables](https://reader036.vdocuments.net/reader036/viewer/2022083003/558ff5671a28abed798b45d0/html5/thumbnails/3.jpg)
What is a variable
• Stores value• myName is “Joe” is $myName = “Joe”;
//strings• UserId is 086AFTG is $UserId = 086AFTG; //
numbers
![Page 4: Php variables](https://reader036.vdocuments.net/reader036/viewer/2022083003/558ff5671a28abed798b45d0/html5/thumbnails/4.jpg)
Rules of PHP Variable naming
• starts with a letter (A-Z, a-z) or underscore (_), followed by any number of letters, numbers, or underscores.
• variable name is case-sensitive. So $MyName and $MYNAME are not same.
• You don’t need to mention what kind of value you are going to store.
![Page 5: Php variables](https://reader036.vdocuments.net/reader036/viewer/2022083003/558ff5671a28abed798b45d0/html5/thumbnails/5.jpg)
Variable Scopes
• Local Scope function myFunction ()
{$FirstName = “Joe”; // local
//do something}
• Global Scope $department = “Marketing”; // global function myFunction ()
{$FirstName = “w3resource”;
//do something}
![Page 6: Php variables](https://reader036.vdocuments.net/reader036/viewer/2022083003/558ff5671a28abed798b45d0/html5/thumbnails/6.jpg)
Static Variables
function test_count() { static $x=1; echo $x; $x++; } test_count(); test_count(); test_count(); //Displays 123
![Page 7: Php variables](https://reader036.vdocuments.net/reader036/viewer/2022083003/558ff5671a28abed798b45d0/html5/thumbnails/7.jpg)
Reserved Words
• Cannot be used as constants, class names, function or method names.
• May be used as variable names, but confusing so avoidable.
• Examples: case, namespace, __METHOD__ etc.
![Page 8: Php variables](https://reader036.vdocuments.net/reader036/viewer/2022083003/558ff5671a28abed798b45d0/html5/thumbnails/8.jpg)
Further Reading
• W3resource PHP Variables Tutorial
• PHP: Variables - Manual