Variables in PHP are used to store data such as strings of text, numbers, and more. Variable values can be modified during the execution of a script. Here are some important points about variables:
=
) is used to assign a value to a variable.In PHP, variables are declared as follows: $var_name = value;
<?php
// Declaring variables
$txt = "Hello World!";
$number = 10;
// Displaying variables value
echo $txt; // Output: Hello World!
echo $number; // Output: 10
?>
In the example above, we created two variables: the first assigned a string value and the second assigned a number. Later, we displayed the variable values in the browser using the echo
statement. The PHP echo
statement is commonly used to output data to the browser. We will delve deeper into this in upcoming chapters.
These are the rules for naming PHP variables:
$
sign, followed by the variable name._
.A-z
, 0-9
, and _
).Note: Variable names in PHP are case-sensitive, meaning $x
and $X
are considered as two distinct variables. Therefore, ensure careful consideration when defining variable names.