PHP Variables
TOC Declaring variables Variable scope Static Variable Reserved words
What is a variable Stores value myName is “Joe” is $myName = “Joe”;  //strings UserId is 086AFTG is $UserId = 086AFTG; // numbers
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.
Variable Scopes Local Scope function myFunction () { $FirstName = “Joe”; // local  //do something } Global Scope $department = “Marketing”; // global  function myFunction () { $FirstName = “w3resource”;  //do something }
Static Variables function test_count()  {  static $x=1;  echo $x;  $x++;  }  test_count(); test_count(); test_count();  //Displays 123
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.
Further Reading W3resource  PHP Variables  Tutorial PHP :  Variables  -  Manual

Php variables

  • 1.
  • 2.
    TOC Declaring variablesVariable scope Static Variable Reserved words
  • 3.
    What is avariable Stores value myName is “Joe” is $myName = “Joe”; //strings UserId is 086AFTG is $UserId = 086AFTG; // numbers
  • 4.
    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.
  • 5.
    Variable Scopes LocalScope function myFunction () { $FirstName = “Joe”; // local //do something } Global Scope $department = “Marketing”; // global function myFunction () { $FirstName = “w3resource”;  //do something }
  • 6.
    Static Variables functiontest_count() { static $x=1; echo $x; $x++; } test_count(); test_count(); test_count(); //Displays 123
  • 7.
    Reserved Words Cannotbe used as constants, class names, function or method names. May be used as variable names, but confusing so avoidable. Examples: case, namespace, __METHOD__ etc.
  • 8.
    Further Reading W3resource PHP Variables Tutorial PHP :  Variables  -  Manual