PHP Basics: Variables

What’s a Variable?

A variable is a symbolic representation of a value such as integer, string, or function result. But as its name suggest, it can change over time or vary so that it can be used many times within a script.

Variables in PHP

In PHP, variable names are going to start with a “$” sign, then they are going to be followed by a letter or an underscore “_”. A variable name is case-sensitive and can contain letters, numbers, underscores, or dashes (a-z, A-Z, 0-9, _, or -), but no spaces otherwise PHP will think that it is a new entity.

Possible variable names:

  1. $variable = value;    // lower case
  2. $VARIABLE = value;    // UPPER CASE
  3. $Variable = value;    // Start with a capital character
  4. $myVariable = value;  // camelCase also spelled "camel case"
  5. $my_variable =value;  // with underscore between words
  6. $my-variable = value; // Using “-“. Not used to avoid confusion because the dash sign looks a lot like the minus sign.
  7. $variable1 = value;   // Using numbers
  8. $_variable = value;   // Single underscore at the beginning. Used also by PHP to define a certain type of variables
  9. $__variable = value;  // Multiple underscore at the beginning. Sometimes its hard to tell how many underscores have been used.

My variable naming preferences:

  1. $my_variable = value; // I use this naming style for my variables and the camel case style for my functions.
  2. $myVariable = value;

Using variables in PHP

- In PHP statements, A value can be assigned to a variable using the “=” operator and the statement must end with a semicolon “;” as shown above.

- A PHP variable does not need to be declared before being set. The variable is declared automatically when you use it.

- In PHP there is no need to specify the variable type. PHP automatically converts the variable to the correct data type, depending on the assigned value.

- Variables in PHP may be passed by reference. When a variable is passed by reference, the reference sign,”&” is used.

  1. <?php function foo (&$bar) {} ?>

Using Variable Variables in PHP
A variable variable is a variable whose name is contained in another variable. this method is also called variable reflection. for instance, if I had a variable $desc with the value of “color”, I could set a variable $color to “red” by simply writing $$desc = “red”. This process calls the value of $desc and then creates a variable out of it.

  1. <?php // The first variable is a standard assignment.
  2. $desc = "color";
  3.  
  4. // The second variable is named by "reflecting" the value of the first.
  5. $$desc = "red";
  6. ?>

Therefore $$desc would produce the same output as $color

  1. <?php echo ${$desc};  // outputs: red
  2. echo $color;  // outputs: red
  3. ?>

Please notice the use of curly braces {} symbols around the variable variable in the first echo. Another thing you can do by using the curly braces is to make a variable from a string, doing so will allow you to pass a string from one page to another, and use that string to pull the information from a variable of the same name.

  1. <?php
  2. $uri = $_SERVER;
  3. foreach ($uri as $key => $var)
  4. {
  5.   $$key = $var
  6. }
  7. …..
  8. ?>

Also it is possible to create variables whose names do not follow the constraints listed above.

  1. <?php /* 911 is your variable name, this would normally be invalid. */
  2. $emergency_number = ’911’;
  3. // Again, you assign a value
  4. $$emergency_number = ’18009119110’;
  5. // Finally, using curly braces you can output ’18009119110’
  6. echo ${911}; ?>

There are many applications for variable varaible in PHP depending on the developer preferences. It’s a very powerful tool, and should be used with an extreme care.

If you have any addition, please use the comments form bellow!

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Tags : ,

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

One Comment

Leave Comment