Free Beginner PHP Tutorials - Variables
2016/08/26 / Science and Tutorials
Learn how to handle variables in PHP. See example:-
Variable is defined as a symbol or name that stores information.Variables are used for storing values such as numeric values, characters, character strings, or memory addresses.
Creating PHP Variables
In PHP, a variable begins with the $ sign, followed by the name of the variable:
<?PHP $val="Hello world"; echo $val; ?>
$val is a variable that stores the string Hello World. Unlike other programming languages, in PHP, the data type of the variable is not specified whether it is an integer, double or a string.
For example, in Java,c,c++, we declare variables starting with the data type.
String val="Hello world";
Rules for PHP variables:
1. A variable in PHP must start with the $ sign, followed by the name of the variable. For example ($val)
2. A variable name must begin with a letter or the underscore character. For example, ($val or $_val)
3. A variable name cannot start with a number. For example, ( $23 ) not allowed
4. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ). Special characters (/, *, &, #) are not permitted in PHP.
5. Variable names are case-sensitive ($val and $VAL are two different variables).
6.A variable cannot contain spaces
Variables in PHP are outputted using the echo statement as shown below.
<?php $val="Hello World"; echo "Welcome ".$val." Thankyou"; ?>
The following example will output: Welcome Hello World Thankyou.