Home Tutorial Php Phpbeginners Rules for Declaring Variables in PHP.

 
 

Rules for Declaring Variables in PHP.
Posted on: February 3, 2011 at 12:00 AM
Advertisement
This section contain the detail about rules for Declaring Variables in PHP.

Rules for Declaring Variables in PHP

Variable has the same definition in all the programming languages. Their implementation or declaration  or syntax may be different but concept is same for all the languages.

A variable is used for storing value such as text strings, numbers or arrays.

You able to use variable over and over again in your PHP script after declaring it.

Given below example give you a quick look of correct way of declaring PHP variable :

$var_name = value;

Naming Rules

Given below key naming conventions of PHP variable :

  • A variable must have $ sign before its actual name. Example, $first_name.
  • A variable in PHP must start with a letter or underscore "_".
  • A variable in PHP can have only numeric(0-9), letter(a-z, A-Z) or underscore"_".
  • PHP variables are case sensitive. The variables $c_number and $C_number are different variables in PHP.
  • The variable name must not have white spaces between them. You can use underscore "_" to fill them. For example, it shouldn't like this-$first name, it should be-$first_name.

No need of declaring data type in PHP

A PHP variable doesn't need to be declare before before adding a value to it. PHP automatically converts the variable to the correct data type, depending on its value.

In the given below example, you will see that there is no need of mentioning data type before passing value to it.

<?php
$variable_txt="Hello World!";
$variable_num=16;
?>  
Advertisement

Related Tags for Rules for Declaring Variables in PHP.:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 3, 2011

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com