PHP variables are just a name for a value and you can simply use it like this $var_name = value;. They usually store sting characters and numeric values for later use in the programming block. Here is a simple example:
PHP<?php
$a = "Welcome";
$b = "Hello";
$first = "John";
$last = "Doe";
$c = ", how are you today?";
echo "$a $first $last <br />"; // Returns: Welcome John Doe
echo "$b $first $c <br />"; // Returns: Hello John, How are you today?
$x=5;
$y=10.1;
echo $x+$y; // Returns: 15.1
?>
Note: There is no need to previously declare the variable before assigning it a value like in C or Java for example. The type of variable is assigned dynamically depending on its value.
In PHP, the scope of a variable is the part of the script where a variable was defined and can be referenced. There are three types of scopes in PHP.
There are cases when you declare a variable outside of a function and you want to use it inside and the other way around.
This is were the global keyword enters.
PHP<?php
$x = 1;
$y = 2;
function sum() {
global $x, $y;
$result = $x + $y;
}
sum();
echo $result; // Outputs: 3
?>
On the other side static keyword is used to store a value of a varible declared inside a function, for consecutive cals. Normally all the local variable are resseted when the fnction closes, unless static keyword is used.
PHP<?php
function count_table() {
static $x = 0;
echo "Value of x is now $x <br />\n";
return $x++;
}
while(count_table() < 10){
// do some other stuff
}
/* Outputs:
Value of x is now 0
Value of x is now 1
Value of x is now 2
Value of x is now 3
Value of x is now 4
Value of x is now 5
Value of x is now 6
Value of x is now 7
Value of x is now 8
Value of x is now 9
Value of x is now 10
*/
?>
Good things:
Bad things:
It is a good to use descriptive-not-to-long names. If your variable name contains various words you can use different techniques to make your programming blocks readable and self-explainable. Regardless of which one you chose, it is a good practice to stick to it.
Just choose one and stick to it across your PHP documents.
Well, I am not kidding. PHP allows you to use a dynamically assigned name for the variable or a function. Take a look at the flowing code:
PHP<?php
$a = 'hello';
$$a = 'world';
echo $a." ".$$a."<br />"; // Returns hello world
echo $a." ".$hello; // Returns hello world
?>
There is a quite cool example of variable on the php.net page that I would like to post it here:
PHP<?php
$Bar = "a";
$Foo = "Bar";
$World = "Foo";
$Hello = "World";
$a = "Hello";
$a; // Contains Hello
$$a; // Contains World
$$$a; // Contains Foo
$$$$a; // Contains Bar
$$$$$a; // Contains a
?>
You can read more on the php manual site: http://www.php.net/manual/en/language.variables.variable.php
Using a variable as function name can be quite usefull in some scenarious, like hooks, filters, callbacks, and meny others. PHP will try to call the function using the value of your variables.
Variable function example:
PHP<?php
function foo() {
echo "I am a string inside foo";
}
$bar = "foo";
$bar(); // outputs: I am a string inside foo
?>
This way you can dynamically call one function or another. There are much more examples of implementations and you can read more about function variable on the php manual site: http://php.net/manual/es/functions.variable-functions.php