How to declare PHP variable

What is PHP variable

A PHP variable is a term used in PHP to store data of various data types. There are many data types like string, integer, float, etc. You don’t need to assign different conventions for these data type as of you are using in other languages like C++, Java, etc.

PHP is a Loosely Typed Language by which you just need to assign the data to a variable. After you assign a value to PHP variable, PHP automatically converts the variable to the correct data type.

Example: Suppose you have a variable called $var. If you give $var = 10, the variable automatically converts to an integer. If you give $var= “this is a variable”, PHP automatically converts it to a string variable. Same things apply to all other data types of PHP.

How to declare PHP variable

You can declare or define a PHP variable using the $(dollar) sign at the start of the name by which you call the variable. Use dollar sign with all the variable of different data types.

However, there are different declaration string data type. For a string value, you need to put a string inside a double quote.
For Example: If you want to assign a string value to variable $var and suppose the string value is ‘This is my string value’. You need to put it inside the single quote or double quote.

Data types of variables in PHP

There are 8 data types to use PHP declare a variable:

Array: An array is a PHP variable type used to store more than one value in one single variable. The array is a collection of similar data type but not the same one. You can call array as a homogeneous collection of data of similar data types.

Boolean: A boolean holds only two value for a variable. These two values are ‘true’ and ‘false’. You can use 1 for true and 0 for false. These possible values can be called as operators you can use to manipulate your data.

Double or Float: A float or double is decimal point numbers. These are a fractional numeric value which you can use to represent a floating point number.

Integer: An integers are the whole number values which you can use without the decimal point. It can be positive as well as negative depend upon your task you want to perform with them.

There can be possible specification of integers in decimal(base 10), hexadecimal(base 16 with prefix 0x) or octal(base 8 with prefix 0). You can use negative as well as positive with the specification too.

NULL: The NULL is used for variables that have no value. To assign a null value to a variable, you need to use null for that. For Example: $x = NULL. If you do not assign any value to a variable, the variable gets automatically assigned a null value.

Object: This PHP variable type used to store data and some information on how to process that data. An object is an instance of a class that has both methods and properties. They all independent with its properties and methods. For Example $obvar = new bike().

Resources: You can use this to assign or reference an external resource. For Example $res = fopen(“myfile.xls”, “r”).

String: You can use PHP variable type as a sequence of character to assign the value of this data type. Store your string value to double quotes or a single quote. There can be any size of the character you can use to assign a value to a string.

PHP Variable is Case Sensitive

A PHP declare a variable is a case sensitive. If two variables contain the same alphabet letters. Now, if you use camel case for one variable and another variable without camel case. Then both the same variable will be considered as different variables.
For Example: If there are two variables, first is $abc and second is $Abc. Now, if you use numeric value 10 for $abc and 20 for $Abc as given below:

Here, you have given $abc to echo the value of the output. The output will print the small letter variable value. To print both, you need to echo them separately as given below:

How PHP Declare Variable And Assign By Reference

You can point the new variable to the old one. This can be useful for somewhat when you want to save your time with the output value and the value to print.

Here, The changes we made to $assign will not affect the original variable $z.

Dynamic PHP Variable

To PHP declare variable dynamic, you need to call a variable inside a variable. This can be done by using the other variable inside another $(dollar) sign.
For Example: Suppose if you have a variable $a and you want to make it dynamic variable, then you must use $(Dollar) sign before $a. The final dynamic variable will be ${$a}. Let us take a below-given example:

This will print the output ‘my new var’ and $$a will become $hello.

Resource

Leave a Reply

Your email address will not be published. Required fields are marked *