PHP - Variable Declaration
A PHP - variable declaration process is used to declare the variable of type text, number, string, etc. It starts with the $ sign followed by the name of the variable.
A PHP - variable declaration process is used to declare the variable of type text, number, string, etc. It starts with the $ sign followed by the name of the variable.
PHP Variable is a container that is used to store information of type string of text, number, boolean, etc. The data stored in the variable can be changed and retrieved when needed.
Every variable is labeled with a meaningful name so that it could be easily understood by a screen reader.
(=)
is used to assign a value to a variable.Source Code
<?php
$php_tutorial_by = "sudhakarinfotech";
echo $php_tutorial_by;
?>
Followings are the most important rule regarding the variable naming convention.
(A-z, 0-9, and _)
.
PHP
variable names are case-sensitive. Please keep in mind that $age and $Age are two different variables.
Source Code
<?php
$backendLanguage = 'php';
// valid
$_cssFramework = 'bootstrap';
// valid - starts with an underscore
$112frontendLanguage = 'javascript';
// invalid - cannot start with a number
?>
In PHP, variable names are case-sensitive. Therefore declaring variable name "learing_resource" is completely different from "learningResources", "learningresources",etc.
Source Code
<?php
$learing_resource = "sudhakarinfotech";
echo "Learning Resources:" . $learing_resource;
?>
Let's see a PHP variable that stores string, integer, and float values.
Source Code
<?php
$str="php learning resource by sudhakarinfotech";
$x=30;
$y=44.6;
echo "string is: $str <br/>";
echo "integer is: $x <br/>";
echo "float is: $y <br/>";
?>
PHP variables must start with a letter or underscore only.
Source Code
<?php
$a="hello world";//letter (valid)
$_b="hello";//underscore (valid)
echo "$a <br/> $_b";
?>
In, PHP creating a variable is known as declaring variables. Please keep in mind that there is no reserve keyword for declaring PHP variables hence it is known as a loose type language.
=
is known as the assignment operator that is used to assign provided value inside the variable.=
).Source Code
<?php
$fruit = "Mango";
$totalcost = 1000;
?>
Please keep in mind that string value is assigned within quotes that will be either single quote or double quote.
PHP variable values can be changed through the source code. Let us understand it with the help of an example.
Source Code
<?php
$username = 'Smith';
$username = 'John Doe';
echo $username;
?>
Basically, reassigning means replacing the new variable value in place of the old value.
The PHP echo statement is used to display output to the screen.
Source Code
<?php
$learningwebsite = "sudhakarinfotech.com";
echo "I love $learningwebsite !";
?>
The $var is a normal variable with the name var
that stores any value like string, integer, float, etc. And the $$var
is a variable that stores the value of the $variable inside it.
Source Code
<?php
$learningPlatform = "sudhakarinfotech";
$$learningPlatform = "https://sudhakarinfotech.com";
echo $learningPlatform."<br/>";
echo $$learningPlatform."<br/>";
echo $sudhakarinfotech;
?>