PHP Variables Types


 

PHP Variables Types

This tutorial is the introduction of variables, naming convention of variables in PHP etc. You will also get some useful examples on variables and reference variables etc.

This tutorial is the introduction of variables, naming convention of variables in PHP etc. You will also get some useful examples on variables and reference variables etc.

PHP Variables:

In PHP a variable is declared with a dollar sign followed by a name. The name of the variable is case sensitive means that $var and $Var are not equal and could contain different values.

In general almost every language follows the same rule and that is variable name should start with a letter or underscore, followed by any number or letter or underscore.

Any variable is defined by the value has assigned to it. Whatever value we assign to a particular variable, the data type of the variable changes to that only. It means in PHP there is no specific data type could be assigned to a variable, the data type is dynamic in nature.

PHP Variable Example:

<?php

$Var='india';//valid

$var=23;//valid and different from $Var

$12;//not valid

$_var;//valid and different from $Var and $var

?>

PHP supports assign values by reference that means a new variable simply references to another variable. If we change the value of the first variable then it automatically reflects on the other reference variable. This kind of variables are most suitable when we need to change values in one function and the changes should reflect on each part of the programming.

$this is another kind of variable which is used in OOP PHP and we can not assign any value to it rather it is used to denote an object.

Example:

<?php

$a=33;

$ref=&$a;

echo "Value of \$a is:".$a."<br/>";

echo "Value of \$ref is:".$ref."<br/>";

$a=343;

echo "Value of \$a is:".$a."<br/>";

echo "Value of \$ref is:".$ref."<br/>";

$ref=33333333;

echo "Value of \$a is:".$a."<br/>";

echo "Value of \$ref is:".$ref."<br/>";

?>

Output:

Value of $a is:33
Value of $ref is:33
Value of $a is:343
Value of $ref is:343
Value of $a is:33333333
Value of $ref is:33333333

 

 

0

 

Ads