PHP Tutorial
Home
PHP Install Xampp
PHP Syntax & Comments
PHP Variables
PHP Constants
PHP Data Types
PHP Echo & Print
PHP Strings
PHP If...Else...Elseif
PHP Ternary Operator
PHP Loops
PHP Functions
PHP Arrays
PHP GET & POST
PHP Advanced
PHP Date and Time
PHP Include and Require
PHP File Upload
PHP Sessions
PHP Cookies
PHP Send Email
PHP JSON Parsing
PHP MySQL Database
PHP MySQL Introduction
PHP Connect to MySQL
PHP MySQL Create DB
PHP MySQL Create Table
PHP MySQL Insert Data
PHP MySQL Select Data
PHP MySQL Delete Data
PHP MySQL Update Data
PHP MySQL Where
What is Variable in PHP
Variables are used to store data/information like string of text, numbers, etc.
PHP variable does not declared before adding a value to it. PHP automatically converts the variable to the correct data type depending on it's value.
Declaring a variable it can be reused the code.
Variables are assigned with the ( = ) operator and the variable on the left side and the expression to be evaluated on the right.
All variables in PHP are denoted with a leading the dollar sign ( $ ).
Create PHP Variable
Syntax :
<?php $variable_name = value; ?>
Example :
The following example with output the sum of two variables :
<?php $a = 5; $b = 10; echo $a + $b; ?>
Variable names in PHP are case-sensitive it means $colour is not same as $CLOUR.
<?php $colour = "Red"; echo "My favorite colour is $str"; // below statement will give error echo "My favorite colour is $STR"; ?>
Output Variable
My favorite colour is Red
Notice: Undefined variable: STR