PHP Variable – int, boolean, null, double variable types with examples

What is variable?

Just like in any programming language, PHP Variables are the way to store information in the middle of the PHP programs.

Important things to know about variables

  • Variables in PHP are declared/denoted by using a leading $ sign. e.g.

$variablename

  • The most recent assignment to a variable is the value of that variable.
  • The assignment operator used in variables is ‘=’ sign where a variable is on the left and expression to be at the right side.

e.g.

$variablename = “variable value”;

  • In PHP, the variables can be declared before assignment but it is not necessary.
  • Variables are not intrinsic types i.e. a variable does not know if it will be used to store a number or string.
  • Before assigning any value, the variables have default values.
  • When necessary, PHP can automatically convert types from one to another.

Types of variables

There is a total of 8 data types that are used to construct variables in PHP.

PHP Integers

PHP int is the whole numbers without a decimal point. An example of int variable is:

The Double type

The Double are the numbers with decimal point e.g. 1.234 or 50.1.

Boolean data type

The Boolean variables may be assigned only two possible values. Either true or false. The example of PHP Boolean variable is:

PHP NULL type

The Null type is a special variable type that has only one value i.e. Null

PHP Strings

Strings are the combination of characters e.g. ‘PHP supports string operations.’

 

The escape sequence in strings

You can use the following escape sequence within strings for respective results:

  1. \n means adding a new line.
  2. \r meant to be replaced by the carriage-return character.
  3. \t in a string means to replace by the tab character.
  4. \$ is replaced by the dollar sign ($).
  5. \” means a single double-quote (“).
  6. \\ means to add a single backslash (\).

Arrays

Arrays in PHP are names and the indexed collection of values. Please see the dedicated chapter for declaring and using Arrays in PHP.

Objects

Object variables store instances of classes. This can package another kind of values and functions specific to use a class.

Resources

These are the special type that holds references to the resources external to PHP like a database connection.

This chapter explained only first 5 types that are simple in nature. Arrays and Objects are the compound in nature that can package up other arbitrary values of arbitrary types. The later types will be explained in their respective chapters in this PHP tutorial.

Naming conventions of variables

For choosing the name of your PHP variables, a few rules should be followed:

  • The variable should start with a letter or an underscore “_”.
  • The variable names can be combinations of a-z, A-Z, 0-9 or _ i.e. alpha-numeric characters, and underscores.
  • If your variable is more than one word then it should be separated with an underscore e.g. $first_second.