PHP Variables

  • PHP variables are characters that stores value or information such as text or integers in your code. It is important to know that variables in PHP are usually represented by a dollar sign($) followed by the name of the variable. Those variables can be used to hold values or expressions and can have letter names or long phrases that can also represent your variable. The benefits of using variables are that you can use letters and number to create names and labels. There are five basic rules for PHP variables:

    1. As said above variables must have a $ sign in front of the name of the variable.
    2. Variables must begin with a letter or underscore.
    3. Variables can only have alpha-numeric and underscore within your variable name.
    4. Do not use any spaces in your variable name. For instance, capital $Y can be defined differently than lowercase
    5. It is important to be careful with your uppercase and lowercase letters because the the same letter can be defined a different way just by capitalizing that letter.

    Creation of Variables in PHP

    Variables in PHP are created the moment you assign a value to it. Variables are the main way to store information in the middle of the PHP variable. Here is some important information about variables:

  • The value of the variable is always used in your most recent assignment.
  • Variables that are used without being assigned will not be a valid value.
  • some variables can or cannot be declared before assigned.
  • In addition to that, there are seven ways to construct PHP variables:

    1. Integers- are usually used as whole numbers or decimals that can be assigned to that variable.
      Example:$a = -154 or $a = 1234
      Booleans- tell us whether two values are true or false Example:
      $y = false;
      $x = 0;
      if (is_bool($y) == true) {
      echo "looks like a boolean";
      }
      								
    2. NULL- has one value that is used by simply placing the word NULL in capitalized letters in the variable.
      Example:$var=NULL;
    3. Strings- are used by placing variable in double quotes. They are also sequences of characters that creates the string.
      Example: echo 'bobby said: "I will be there" ';
    4. Arrays- are named and indexed collection of other values. Example:
      $array = array(
      	low => "high",
      	high => "low"
      ); 
      
    5. Objects-Objects are variables in PHP that have their own individual methods and attributes. Example:
      								
      class high
      {
      	function go_high()
      	{
      		echo "going high.";
      	}
      }
      		
    6. Resources- re resources and a database for PHP.
    7. Example:$ab = curl_init()

    PHP Variable Scopes

    Variable scopes are scripts that variables uses in PHP function. There are four different types of variable Scopes:

    1. Local Scope- is a variable that is declared within a PHP.
    2. Global Scope- is a variable that must be declared to be a global function inside the created function. You simply must put the word GLOBAL in all capitalized letter in front of the variable you want to make global. Doing this lets the PHP know to use the variable to have that name.
    3. Static Scope-You put the 'static' keyword to declare something as static. Static variables are only created the first time you initialize it, and are usually used inside functions.
    4. Function Parameter-Variables that are passed through parameters of functions.
    Some Examples of each Scope:
    Local Example:
    								
    function myPlan ()
    {
    	echo $x; // local scope
    }
    	
    Global Example:
    function myPlan ()
    {
    $GLOBALS['a']=$GLOBALS['b']+$GLOBALS['a'];
    }
    
    Static Example:
    function myPlan ()
    {
    STATIC $y=2;
    echo $y;
    }
    
    function Parameter Example:
    function myPlan($y)
    {
    echo $y;
    							 
    }