Friday, 2010-07-30

Who's Online

We have 7 guests online


Breadcrumbs

Home Variables


Variables-PHP


Variables

                                PHP Variables

 

If you have never had any programming, Algebra, or scripting experience, then the concept of variables might be a new concept to you. A detailed explanation of variables is beyond the scope of this tutorial, but we've included a refresher crash course to guide you.

A variable is a means of storing a value, such as text string "Hello World!" or the integer value 4. A variable can then be reused throughout your code, instead of having to type out the actual value over and over again.

 

In PHP you define a variable with the following form:

                                                                   $variable_name = Value;

 

If you forget that dollar sign at the beginning, it will not work. This is a common mistake for new PHP programmers!

 

Note:  Also, variable names are case-sensitive, so use the exact same capitalization when using a variable. The variables $a_number and $A_number are different variables in PHP's eyes.

 

  • PHP Variable Naming Conventions

There are a few rules that you need to follow when choosing a name for your PHP variables.

 

  • PHP variables must start with a letter or underscore "_".
  • PHP variables may only be comprised of alpha-numeric characters and underscores. a-z, A-Z, 0-9, or _ .
  • Variables with more than one word should be separated with underscores. $my_variable
  • Variables with more than one word can also be distinguished with capitalization. $myVariable

 

A variable is used in programming to represent another value.

In PHP variables all start with a dollar sign ($).

They can be set to a string or a numeric value.

Code:

                                                                   <?php
                                                                             $a = "Alexa";           //This sets the variable $a to a string value (Alexa)
                                                                             $b = 6;                     //This sets the variable $b equal to a number value (6)
                                                                   ?>

 

You will notice that string values are contained with quotations [""] while numeric values are not. Instaed of just a static number, you can also set a variable equal to an equation. Here are some examples:

Code:

                                                                    <?php
                                                                              $a = 3 + 5;              //This sets the variable $a equal to 8 or (3 + 5)
                                                                              $b = 6 + $a;            //This sets the variable $b equal to 14, or (6 + $a)
                                                                    ?>

 

We can combine values using a period [.] This will literally mash the values together. Here are some examples:

 

Code:

                                                                     <?php
                                                                               $a = 1;
                                                                               $b = 2;
                                                                               $c = $a.$b;
                                                                       //The value of $c is now 12, because we smashed the 1 and 2 together
                                                                               $d= $c + 1 ;
                                                                               echo $d;
                                                                       // This will echo 13, our newly created 12 plus 1 is 13
                                                                               $e = "Number";
                                                                               $f = $e.$d;
                                                                               echo $f;
                                                                       //This will echo the string Number13, a combination of $e and $f
                                                                      ?>

 

This is different than just echoing both variables as a new variable is actually created from the other variables.

 

Another thing you can do is make a variable from a string by using {} symbols. So, by using this method you could pass a string from one page to another, and use that string to pull the information from a variable of the same name. Here is an example:

 

Code:

                                                                       <?php
                                                                                 $a= "SomeVar";
                                                                                 $SomeVar= "test";
                                                                                 echo ${$a};
                                                                       ?>


This will return "test", which is the value of $SomeVar because by using the {} symbols we are creating the variable from the string in $a.


Note: PHP does not require variables to be declared before being initialized.

 

 

     PHP DATA TYPE