In this tutorial, I am going to explain what meant by php variables and are their types with programming examples for beginners. Php variables are unknown amounts that could be called by any name starting with "$" dollar sign. The php allows to set a value to the variable and automatically detects the type of set value.
For example, the username and password in a sign in form are variables and are set by the user.
How to declare php variable with and without value
In general, php allow to declare variable without value as below:
$name1;
?>
This means it contains nothing or a value of null, also it is referred to as a global variable across files as long as it is out of a php function or class code blocks, where php functions or classes will be explained in a later tutorial.
Php variables types and each variable type declaration
There are several php variables types and each variable type is declared as follow:
- Php integer variable : refer to a real number with positive or negative sign. As an example:<?php
$name1 = 123;
$name2 = -123;
?> - Php floating point number variable : refer to decimal or exponential numbers with positive or negative sign. As an example:<?php
$name1 = 1.23;
$name2 = -1.23;
$name3 = 1.2e3;
$name4 = -1.2e3;
?> - Php string variable : refer to any characters starting and ending with quote or double quote. As an example:<?php
$name1 = 'a string may be abc or 123 or !@#';
$name2 = "a string may be abc or 123 or !@#";
?> - Php array variable : refer to a series of values of any type that could be stored in one variable and each value is identified by a different key. As an example:<?php
$name1 = array(123,-1.23,"abc123");
$name2 = [123,-1.23,"abc123"];
?>This will be more explained in a later tutorial.
How to properly name a php variable
It is important to know that a php variable could be only named with a minimum of one letter character after "$" sign and not a number character like $4name but can be $n4ame or $na4me and so on.
Ways to display a php variable
Php variable could be displayed using "echo" keyword for all types containing single value and "print_r" keywords for array type containing series of values. As an example:
$name1 = 'a string may be abc or 123 or !@#';
echo $name1;
$name2 = [123,-1.23,"abc123"];
print_r($name2);
?>
How to display a php variable in string
Note that a php variable could displayed in string between only double quotes and not between single quote to complete any value. As an example:
$name1 = 'a string may be abc ';
$name2 = 'or 123 or !@#';
echo "$name1 $name2";
?>
This will show in browser:
How to set and alter a value to a php variable
The variable could be set several times and maintain only the last set value. As an example:
$name1 = 'a string may be abc ';
$name1 = 'or 123 or !@#';
echo $name1;
?>
This will show in browser:
References:
- Practical Experience
Related Articles
Latest Articles
- What are Php Arithmetic and Comparison Operators ? | Php Tutorial
- What are Php Variables and Types ? | Php Tutorial
- How to write your first php web page with code example | Php Tutorial
- Introduction to Php Learning Course | Php Tutorial
- How to Secure your JavaScript Code from being Copied by Competitors | Javascript Tutorial
- How to Promote JavaScript To Speed Your Website Loading Time For Higher SEO Performance | Javascript Tutorial