Variables in PHP

A variable in PHP is a name of memory location that holds data. All variables in PHP are declared using $ sign followed by variable name.

Syntax of declaring a variable in PHP is given below :
$variablename=value;


Rules for defining variables in PHP :
  • A variable can have alphabets, digits, and underscore.
  • A variable name can start with the alphabet, and underscore only. It can't start with a digit.
  • Upper and lowercase letters are distinct because PHP is case-sensitive.
  • No whitespace is allowed within the variable name.
  • A variable name must not be any reserved word or keyword, e.g. int, float, etc.

Declaring string, integer and float variables in PHP :

File name : variables.php
<?php 
$str="Welcome to PHP"; 
$x=2; 
$y=3.14; 
echo "string is: $str <br/>"; 
echo "integer is: $x <br/>"; 
echo "float is: $y <br/>"; 
?>

Output will be :
string is: Welcome to PHP
integer is: 2
float is: 3.14

0 Comments