ankitweblogic logo
W e b t u t o r i a l s

Define PHP

PHP Installation


PHP Case Sensitivity

PHP Variable

PHP Echo & Print

PHP Operators

PHP Receiving Input

PHP Decision Making

PHP Loops

PHP Jumping Statement

PHP Image Gallery

PHP File Upload

PHP Arrays

PHP String Functions

PHP Math Functions

PHP Functions

PHP Variable Scope

PHP Constant Variable

PHP Superglobals

PHP Form Validation

PHP Include Statement

PHP Filter

PHP File Handling

PHP Date Functions

PHP Cookies

PHP Session

PHP Send Emails

PHP Captcha

PHP-MySQL Connectivity

PHP MySQL Select

PHP MySQL Insert

PHP MySQL Delete

PHP MySQL Update

PHP MySQL Injection

PHP Variable

A Variable is a small temporary memory area use to store value, value can be number, decimal number, text, date etc. Variables act as a "containers" for storing information.
Rules to declare a PHP variable are:
A variable starts with the $ sign, followed by the variable name.
A variable name can contain numbers, letters and underscores.(A-Z, a-z, 0-9, and _ )
A variable name must begin with a letter or the underscore character.
A variable name cannot start with a number and cannot contain special characters.
Variable names are case-sensitive ($age and $AGE are different variables)

Data Types in PHP

PHP has a total of 8 Data Types which we use to declare variables:

  1. Integer: are the whole number, without a decimal point, eg. $var = 12345;
  2. Double: are floating-point number can contain decimal number, eg. $num = 2.2888800;
  3. Boolean: can have only two possible values, true or false, eg. $x = true. Boolean value are often used in condition testing.
  4. NULL: is a special type that only has one value:NULL, eg. $my_var = NULL.
  5. String: is a collection of characters, eg. $str = "This is a string". A string can be any text inside quotes. You can use single or double quotes to represent a string.
  6. Arrays: An array stores multiple values in one single variable, eg. $colors = array("Red","Green","Blue");
  7. Objects: are instances of a class, which can package up both kinds of values and functions that are specific to the class.
  8. Resources: are special variables that hold references to resources external to PHP (such as database connections).

Example: Valid Variable Declaration:
$txt = "Programming is Fun!"; //variable with string value
$a = 5; // variable with integer value
$b = 10.5; //variable with float value
$_city = "Delhi"; //variable with underscore character
$marks2 = 50; //variable name contain number

Example: In-Valid Variable Declaration:
$2marks = 50; //variable name cannot contain number.
$@city = "Delhi"; //variable name cannot contain special characters like @.
$area of circle = 50.5; //variable name cannot contain spaces.
A variable can have a short name (like a and b) or a more descriptive name (like name, age, areaofcircle, total_marks).