Variables in PHP Programming

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

Define variables in PHP Programming

  • 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; //Define numeric value
$y = 8; //Define numeric value

?>

String variables example

<?php

   $name = "John";    //Define string value
   $city = "Colombo"; //Define string value

?>

Boolean type variable example

<?php

$bool = False; //Define boolean value
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"); // Define array variable

    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.

 257 total views,  1 views today

Leave a Comment

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

wpChatIcon