PHP Variables

Creating (Declaring) PHP Variables

PHP has no command for declaring a variable.
A variable is created the moment you first assign a value to it:
$txt="Hello world!";
$x=5;
After the execution of the statements above, the variable txt will hold the value Hello world!, and the variable x will hold the value 5.

Rules for PHP variables:

  • A variable starts with the $ sign, followed by the name of the variable
  • A variable name must begin with a letter or the underscore character
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • A variable name should not contain spaces
  • Variable names are case sensitive ($y and $Y are two different variables)

PHP varialbles are the main Parts of a PHP script.
In this Script we will assign our all components for eg. name,age,password,email etc...

Now using PHP Variables we are going to make a Simple Addition Function Which is Shown Below:

<?php
 

$x=5;
$y=6;
$z=$x+$y;
echo $z;


?> 


OUTPUT: 15



Note:-  In this PHP script x and y becomes variables beacuse we start them with $ letter.