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.

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.

PHP Variable's Features

  • PHP is a loosely typed language. It means that a variable does not need to be declared before adding a value to it.Php automatically converts the correct data type based on the available variable values.
  • Variable name should be meaningful.
  • It can store information of different data types.
  • It can also be reused throughout the code.
  • The variable value can be changed easily.
  • The assignment operator (=) is used to assign a value to a variable.

General Syntax

      $variableName = value;
    
Try it now

Source Code

          <?php  
$php_tutorial_by = "sudhakarinfotech";
echo $php_tutorial_by;    
?>
        
Try it now

PHP Variable Naming Convention

Followings are the most important rule regarding the variable naming convention.

  • Please keep in mind that PHP variables start with a $ sign, followed by the name of the variable.
  • PHP variable name must be started with a letter or underscores, followed by any number of letters, numbers, or underscores.
  • PHP variable names should not start with a number.
  • PHP declared variable must be reused throughout the code.
  • PHP variable names should not contain white spaces.
  • Please keep in mind that the PHP variable can contain alpha-numeric characters and underscores (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
?>
        
Try it now

PHP Variable - case sensitive

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;
?>
        
Try it now

PHP Variable Declaration: String, Integer , Float

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/>";  
?>
        
Try it now

PHP Valid/Invalid Syntax

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";  
?>
        
Try it now

PHP Variable Declaration

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.

class="my-3"PHP variable declaration rule:

  • PHP variable name always lies on the left side of the assignment operator.
  • The symbol = is known as the assignment operator that is used to assign provided value inside the variable.
  • =The variable lies on the right side of the assignment operator(=).

Source Code

          <?php
$fruit = "Mango";
$totalcost = 1000;
?>
        
Try it now

Please keep in mind that string value is assigned within quotes that will be either single quote or double quote.

PHP Reassigning Values to Variables

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;
?>
        
Try it now

Basically, reassigning means replacing the new variable value in place of the old value.

Displaying Output

The PHP echo statement is used to display output to the screen.

Source Code

          <?php
$learningwebsite = "sudhakarinfotech.com";
echo "I love $learningwebsite !";
?>
        
Try it now

PHP variable($) and variables($$)

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; 
?>
        
Try it now

Web Tutorials

PHP Variable Declaration
Html Tutorial HTML
Javascript Tutorial JAVASCRIPT
Css Tutorial CSS
Bootstrap 5 Tutorial BOOTSTRAP 5
Bootstrap 4 Tutorial BOOTSTRAP 4