PHP – Variables

Variables are one of the most valuable thing in php. Because variables are used to store data which are used in php programming. The variables are initialize using the symbol $ followed by name of the variable.

 

Rules for variables

  • Variables are assigned using underscore and alphabets.
  • Variables are basically case sensitive.
  • In case php variable assigning process the first element is not used as Numbers, boolean, null, array, string, are not used to assign a variable.
  • In variable assignment process we only consider characteristics of the first element.
  • The element followed by the first elements are not as much  as important.

Some of the examples of valid and invalid variables are given below

 

Data store in php variables

Text data, characters, numbers etc are generally stored using php variables. The variables are assign using the equal to operator. In php variable initializing process no need of mentioning which type of data going to store.The programmer can store arrays and strings in a single variable. Generally the data stored in variable to perform a particular operation or operations addition, multiplication and subtraction are some its examples

Text data are stored in php using quotes.

Array storing process is same as that of other element storing process in php only difference is the programmer must mention this is an array after the equal to operator and write the array values inside the array function with the help of an open bracket.

After store an array we must use these values some where we need. For printing the array values we can’t use the echo because echo function do not read the value inside the array function. echo values can read if we specify which key needed to be print. eg. echo $abc[0];

If the developer use echo function to print an array then it produce some error messages with word array. print_r is a solution of this problem print_r function read the value inside the array function and give a proper output.

Output

 

Variable of variable function

Variable of variable function is a single function which is used to store variables inside a variable that is the function take the value of the variable and treat as a variable. The variable of variable is represented using the symbol $$.

 

Types of variables

The function gettype() is used to find the type of variable. The variable types are given below.

  • Boolean
  • Integer
  • Double
  • String
  • Array
  • Object
  • Resource
  • Null

 

Boolean

Is the variable used to return true or false value or 0 or 1. 1 for true value and 0 for false value.

Example 1

Output

The above example returns the variable is boolean or not.

 

Integer

Negative, zero, and positive numbers without any fraction is known as the integer value.

Example 1

 Output

 

Double

Number with fraction is known as double.

Example 1

Output

 

String

String is the sequence of characters.

Example 1

Output

 

Array

Array is an ordered map with keys and values. If the key is not provided they put natural numbers .

N = {0, 1, 2, 3, …n }

n is the number of values.

Example 1 

Output

 

Object

Object refers to a class which the class contain functions.

Example 1

Output

 

Null

Null is a variable with no value.

Example 1

Output

 

Resource

Resource is not an actual data which is reference for  the actual data.

Leave a Reply