PHP Variables
All variables in PHP start with a $ sign symbol. Variables may contain strings, numbers or arrays.
Below, the PHP script assigns the string "Hello World" to a variable called $txt :
<html>
<body>
<?php
$txt="Hello World";
echo $txt;
?>
</body>
</html>
To concatenate two or more variables together, use the dot (.) operator.
<html>
<body>
<?php
$txt1="Hello World";
$txt2="1234";
echo $txt1 . " " . $txt2 ;
?>
</body>
</html>
The output of the script above will be: "Hello World 1234".
Variable Naming Rules
A variable name must start with a letter or an underscore "_".
A variable name can only contain alpha-numeric characters and underscores (a-Z, 0-9, and _ ).
A variable name should not contain spaces. If a variable name should be more than one word, it should be separated with underscore ($my_string) or with capitalization ($myString).
Back to Home
|