Variables in PHP

Variables used for storing values in PHP, such as numbers, strings, or function results, so that they can be used many times in a script.

Define variables PHP

  • All variables in PHP start with a $ sign symbol. It’s may contain strings, numbers, or arrays.
  • A variable name must start with a letter or an underscore.
  • PHP variable name shouldn’t contain spaces.
  • A variable name can only contain alpha-numeric characters and underscores.
  • Keywords may not be used as variable names.
  • If a variable name should be more than one word, it should be separated with an underscore. (Ex: $first_name)
  • Variable names are case-sensitive.

Numeric variables example

<?php

$x = 5; //numeric values
$y = 8; //numeric values

?>

String variables example

<?php

$name = "John";    //string values
$city = "Colombo"; //string values

?>

Boolean type variable example

<?php

$bool = False;
if ($bool == True){
   echo "This is a true statement";
}
else {
   echo "This is a false statement";
}

?>

Array variable example

An array can hold more than one value at a time. You can use array() function to create an array in PHP.

<?php

$Vehicles = array("Car", "Van", "Bus");

echo "Array first element is " . $Vehicles[0] .;
echo "Array second element is " . $Vehicles[1] .;
echo "Array third element is " . $Vehicles[2] .;

?>

PHP is a popular open-source general-purpose scripting language. You can learn PHP programming with examples by following our PHP articles sequentially.

 198 total views,  2 views today

Leave a Comment

Your email address will not be published. Required fields are marked *

wpChatIcon