RSS

Variables in PHP

Fri, Jan 15, 2010

PHP & MySql

Variables are very important in PHP and are used often to store text into which need to be displayed or used several times again in the script. Variables consist of a name and a value and are indicated by a $ (dollar sign). When a variable is set, they can be called anywhere in the script (by their name) to use the value of that variable without having to type or generate the value again. So you just call the variable again and you got the value of it (which is very usefully when you have to use the same value over and over). I’ll show you how you can create a variable which is pretty easy:

$variable1_name = "variable value1";
  $variable2_name = 10;

As you can see a variable always starts with a dollar sign followed by the name of the variable. You can use the = (equal to) sign to set a variable equal to a value (in this case we set the value “variable value1″ for $variable1_name). As you can see in this case the value of the first variable is put between quotes. This is needed because the value we gave to the variable is a, so called, string. A string contains characters and therefore needs to be put between quotes unlike numbers (see $variable2_name). You could also use single quotes instead of double quotes in this case, but we’ll get back to that later for variables because it needs to be done carefully to not cause syntax errors.
Now we’re going to make it somewhat more complex. We’ll use variables inside the value of a variable. May sound confusing but lets just have a look at it:

$variable1  = "variable value1";
$variable2   = 10;

$variables_combined = $variable1.$variable2;

$variables_combined is now holding the values of both $variable1 and $variable2 which is those two values combined: “variable value1″ and 10, so “variable value1″10 which is just shown as variable value110 when echoing it. You see the variables are binded with a dot.

  $variable1  = "variable value1";
  $variable2   = 10;

  $variables_combined = "Variable1 holds value: ".$variable1." \n Variable2 holds value: ".$variable2;

Now $variables_combined contains (echoed):

[QUOTE]Variable1 holds value: variable value1
Variable2 holds value: 10[/QUOTE]As you can see the variables are keept out of quotes while the text is between quotes. To ‘bind’ the text and the variable the dot is used again (just as in the previous example where we combined 2 variables, which were as well binded by a dot). So in other words: text is put between quotes and variables aren’t, and you can bind variables with variables or text by using the dot character.
You can also put the variables with the text, both, between quotes without having to close the quotes for the text to keep the variables out of quotes and then having to bind them by a dot. But then you’ll need to put the variables between { and } to seperate them.

$variable1  = "variable value1";
  $variable2   = 10;

  $variables_combined = "Variable1 holds value: {$variable1} \n Variable2 holds value: {$variable2}";

As you can see everything is between quotes here, but the variables are still seperated from the rest of the text by { and }. This however is a not as stable way for more complex variables (which we’ll discuss later though).

To echo a variable and show its value on the screen, it works exactly the same with the quotes and non-quotes:

$variable1  = "variable value1";
$variable2   = 10;
echo "Variable1 holds value: ".$variable1." \n Variable2 holds value: ".$variable2;
// or:
echo "Variable1 holds value: {$variable1} \n Variable2 holds value: {$variable2}";

But you could also first put that sentence into a variable ( like we done above – we put it into $variables_combined ) and then echo it:

$variable1  = "variable value1";
$variable2   = 10;

$variables_combined = "Variable1 holds value: ".$variable1." \n Variable2 holds value: ".$variable2;

echo $variables_combined;

In that case you can just echo the variable like that, without any quotes or hassle as you just echo 1 variable which always is written without any quotes.

Alert: When actually writing a script like this, it ALWAYS needs to be between the php tags ( see Chapter 1 – Basic PHP file ) which are as discussed in Chapter 1. In these examples though I didn’t put them as they were just examples of using variables, not of creating the files themselves.

Cheers,
Admin.