Variables are used for storing the data like strings, numbers , arrays etc in programs.
In PHP a variable starts with a $ sign. Forgetting to add $ sign is a very common mistake for the beginners.
Example for a PHP variable : $variable
And the declaration will look like $variable = VALUE ;
A PHP variable name can start with a letter or underscore ( _ ) symbol and it shouldn’t contain any space or other special characters like *, % , # etc.
See the valid and invalid variable names
- $employee name ( Invalid variable )
- $employee_name ( Valid variable )
There is no type declaration is needed in PHP variable declaration.variable will automatically converted to the type of assigned value to the variable.
For example :
- $variable = 10 ; ( Here $variable will treat as integer automatically, we dont need to specify type as Integer at the declaration command )
- $variable = “I love you”; ( Here $variable will treat as a string automatically, we dont need to specify type as string at the declaration command )
PHP Sample program with variable declaration
<?php $variable_age = 30; $variable_name= "Syam"; $variable_desp="has aged"; echo $variable_name." ". $variable_desp." ".$variable_age; ?>
The result will be look ke
Syam has aged 30
Related Tutorials
Array in PHP tutorial
Array is a special variable which can store multiple value. ( Note : a normal variable can store only one...
Concatenate strings in PHP using concatenation operator
Concatenation is the process of joining multiple strings into one.In PHP concatenate strings is doing by concatenation operator ( “.”...
PHP search in array
In this tutorial we are going to learn how to search in array. In PHP we can use in_array() and...
strlen() – PHP string length function
While our PHP programming , in some cases we need to get the length of a string for processing. strlen()...
PHP session tutorial
Using PHP session variable is a mechanism to store user’s data in server. And sessions will last till we are...
Split a string and store into an array
This PHP script code is for split a string into the specified length and store it into an array. Here...
PHP cookie tutorial with sample program
Cookie is a variable stored in the user computer. Its a mechanism to store the data for future use. Cookies...
Adding line break using PHP
For the formatting purpose we need to add line breaks in our output or strings to store in MySQL tables...
Automatic type conversion is one of the main advantage and feature of PHP programming.