In this lesson we are going to learn how to declare a variable in PHP.
A variable is something that can handle a piece of information. Variable in PHP can store values such as text and integers. The characteristic of a variable is what we call a Data Type. The four basic data types in PHP are Boolean, integer, string and floating-point.
Every variable has a name, in PHP the variables start with the dollar sign ($).
Here is an example of declaring variable in PHP:
$varname = value;
- Don’t forget to put a dollar sign ($) at the start of your PHP variable.
- PHP variable must start with an underscore (_) or letter. Don’t start your variable with a number.
- PHP variable is case-sensitive.
- PHP variable can contain alphanumeric value. Special characters are not allowed (<,>,#, etc…).
- Assign a value to every variable you declare.
- PHP can automatically determine the type of variable depending on its value.
Example of variable that contains a string:
<?php $name = "My name is Rolan Pascual"; echo $logo_text; ?>
It will display My name is Rolan Pascual in your browser.
Example of variable that contains a number:
<?php$price = 98.5 ;echo $price; ?>
It will display 98.5 in your browser.