In this tutorial you will learn how store information in a variable in PHP.
Variables are used to store data, like string of text, numbers, etc. Variable values can change over the course of a script. Here're some important things to know about variables:
=
) used to assign value to a variable.In PHP variable can be declared as: $var_name = value;
Example:
<?php
// Declaring variables
$txt = "Hello World! From XDevSpace";
$number = 20;
// Displaying variables value
echo $txt; // Output: Hello World! From XDevSpace
echo $number; // Output: 20
?>
In the above example we have created two variables where first one has assigned with a string value and the second has assigned with a number. Later we've displayed the variables values in the browser using the echo
statement. The PHP echo statement is often used to output data to the browser. We will learn more about this in upcoming chapter.
These are the following rules for naming a PHP variable:
$
sign, followed by the name of the variable._
.A-z
, 0-9
, and _
).