How to Define a Variable in PHP | Variable in php
In PHP, a variable does not need to be declared before adding a value to it. PHP automatically converts the variable to the correct data type, depending on its value.
In PHP, a variable is declared using a $ sign followed by the variable name. Here, some important points to know about variables:
- As PHP is a loosely typed language, so we do not need to declare the data types of the variables. It automatically analyzes the values and makes conversions to its correct datatype.
- After declaring a variable, it can be reused throughout the code.
- Assignment Operator (=) is used to assign the value to a variable.
Syntax of declaring a variable in PHP is given below:
$variablename=value;
You may like
- PHP Array: Indexed,Associative, Multidimensional
- PHP remove duplicates from multidimensional array
- Create Modern Website Using HTML CSS
- How to Convert String to Array in PHP
- Data types in php | php data types in hindi
- How to Learn Programming Languages step by step
Creating PHP Variables
In PHP, a variable starts with the $ sign, followed by the name of the variable:
Example
$txt = "Hello world!";
$x = 5;
$y = 10.5;
PHP Variable: Declaring string, integer, and float
Example to store string, integer, and float values in PHP variables.
File: variablename1.php
Example
$str="any string";
$x=500;
$y=55.6;
echo "string is: $str <br/>";
echo "integer is: $x <br/>";
echo "float is: $y <br/>";
Output:
string is: any string
integer is: 500
float is: 55.6
PHP Variable: Sum of two variables
variablename2.php
$x=5;
$y=6;
$z=$x+$y;
echo $z;
Output:
11
In order to use variable variables with arrays, you have to resolve an ambiguity problem. That is, if you write $$a[1] then the parser needs to know if you meant to use $a[1] as a variable, or if you wanted $$a as the variable and then the [1] index from that variable. The syntax for resolving this ambiguity is: ${$a[1]} for the first case and ${$a}[1] for the second.
Curly braces may also be used, to clearly delimit the property name. They are most useful when accessing values within a property that contains an array, when the property name is made of multiple parts, or when the property name contains characters that are not otherwise valid (e.g. from json_decode() or SimpleXML).
Example Variable property example
class foo {
var $bar = 'I am bar.';
var $arr = array('I am A.', 'I am B.', 'I am C.';
var $r = 'I am r.';
}
$foo = new foo();
$bar = 'bar';
$baz = array('foo', 'bar', 'baz', 'quux');
echo $foo->$bar . "n";
echo $foo->{$baz[1]} . "n";
$start = 'b';
$end = 'ar';
echo $foo->{$start . $end} . "n";
$arr = 'arr';
echo $foo->{$arr[1]} . "n";
Output:
I am bar.
I am bar.
I am bar.
I am r.
Rules for PHP variables:
- A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
- Variable names are case-sensitive ($age and $AGE are two different variables)
- A variable starts with the $ sign, followed by the name of the variable
- A variable name must start with a letter or the underscore character
- A variable name cannot start with a number
PHP is a Loosely Typed Language
In the example above, notice that we did not have to tell PHP which data type the variable is.
PHP automatically associates a data type to the variable, depending on its value. Since the data types are not set in a strict sense, you can do things like adding a string to an integer without causing an error.
In PHP 7, type declarations were added. This gives an option to specify the data type expected when declaring a function, and by enabling the strict requirement, it will throw a “Fatal Error” on a type mismatch.
You will learn more about strict and non-strict requirements, and data type declarations in the PHP Functions chapter.
You may like for Define a Variable in PHP