PHP Variables

In PHP, a variable is declared using a $ sign followed by the variable name.

Important Keys

  • PHP is a loosely typed language, so we do not require to declare the data types of the variables. 
  • When we declare a variable in php we can resused throughout the code.
  • Assignment Operator (=) is used to assign the value to a variable.

Syntax of PHP Variables :

$variablename=value;

PHP Variables Declaration Rule

  • php variable must start with a dollar ($) sign followed by the variable name.
  • we don't use space using php value declaration for eg:- $var name
  • php variable only contain alpha-numeric character and underscore (A-z, 0-9, _).
  • php variable name must start with a letter or underscore (_) character.
  • PHP variables are case-sensitive, so $name and $NAME both are treated as different variable.

Declaring string, integer, and float in PHP

The below example show to store string, integer, and float values in PHP variables.

Example :

<?php  
$str="hello world tring";  
$x=150;  
$y=42.5;  
echo "string is: $str <br/>";  
echo "integer is: $x <br/>";  
echo "float is: $y <br/>";  
?>

OUTPUT

string is: hello world tring
integer is: 150
float is: 42.5

Data types used by PHP to declare or construct variables:

  • Integers
  • Doubles
  • NULL
  • Strings
  • Booleans
  • Arrays
  • Objects
  • Resources

PHP is Loosely typed language

PHP is a loosely typed language, it means PHP automatically converts the variable to its correct data type.