PHP Variables

Variables in PHP:

Variables are the logical name given by the programmer which refers to a physical location in the memory. Variables are used for storing data or informations in computer memory.

Example: $phpVariable;

 

Rules for constructing php variables:

  1. PHP variable starts with $ sign like $var1
  2. Variable name must start with alphabet or underscore after $.
    Ex – $var (right)
             $34var (wrong)
  3. No special symbol other than the underscore can be used.
    Ex – $var_1 (right)
             $var-1 (wrong)
  4. No comma or blank space can be used in variable name.
    Ex – $var1 (right)
             $var, 1 (wrong)
  5. It can contain digits but not as starting.
    Ex - $var34 (right)
            $34var (wrong)
  6. variables are case sensitive, means $var and $VAR are different from each other.

 

PHP Variable Declaration:

php variable starts with a $ sign and followed by a user define name as follows:

Example: $var1, $var2,..............

 

PHP Variables Initialization:

php variables are initialized using an assignment operator “=” as follows

Example:

<?php 

$var=12;

$var1=”Hello”;

echo $var1.’ ’.$var; // here ‘.’ is a concatenation operation in php.

?>

Note: echo is a php command, used to display or print messages in the output screen.

 

Escape-sequence:

  • \n is replaced by the newline character.
  • \r is replaced by the carriage return character.
  • \t is replaced by the tab character.
  • \$ is replaced by a dollar sign itself($).
  • \” is replaced by a single backslash(“).
  • \\ is replaced by a single backslash(\).
Check Page Rank