Values stored in a variable or in a constant are stored as a specific type of data. PHP provides eight data types.
int: A whole number or integer; numeric values without a decimal point, for example, 2 or 5
float: Floating-point number is a numeric value with decimal point, for example, 2.3
string: A series of characters which are surrounded by either single or double quotes
bool: A Boolean value that can be either true or false
null: A value that represents no value
array: A group of values in one variable
object: A structure created with a class
resource: A reference that identifies a connection
PHP determines the data type automatically. When writing PHP scripts, you don't need to specify which data type you’re storing. The following two statements store different data types:
$var1 = 123;
$var2 = "123";
The value for $var1 is stored as an integer. The value for $var2 is stored as a string because it is enclosed in quotes. You can see when printing the content of the variable with var_dump.
Integers are whole numbers, such as 1, 10, and 333. Integers in PHP are always signed and can therefore store both positive and negative values.
$myInt = 1234; // decimal number
Floating-point numbers, also called real numbers, are numbers that contain a decimal value, such as 3.1 or .667. These can be assigned using either decimal or exponential notation.
$myFloat = 1.234;
$myFloat = 3e2; // 3*10^2 = 300
A character string is a series of characters. Characters are letters, numbers, and punctuation. When a number is used as a character, it is just a stored character, the same as a letter. It can’t be used in arithmetic. For example, a phone number is stored as a character string because it needs to be only stored - not added or multiplied.
When you store a character string in a variable, you tell PHP where the string begins and ends by using double quotes or single quotes.
Escaping the Character
If you need to tell PHP to interpret the single quote (’) as a part of string instead of as the end of the string, you can do so by using a backslash (\) in front of the single quote. The backslash tells PHP that the single quote doesn’t have any special meaning. This is called escaping the character.
Single-quoted strings are stored literally, with the exception of \', which is stored as an apostrophe. In double-quoted strings, variables and some special characters are evaluated before the string is stored.
Case 1: If you enclose a variable in double quotes, PHP uses the value of the variable. However, if you enclose a variable in single quotes, PHP uses the literal variable name. For example,
$month = 12;
$result1 = "$month";
$result2 = '$month';
echo "<br />";
The output is
Case 2: Starting a new line
The special characters \n tell PHP to start a new line. When you use double quotes, PHP starts a new line at \n; with single quotes, \n is a literal string.
Case 3: Inserting a tab
The special characters \t tell PHP to insert a tab. When you use double quotes, PHP inserts a tab at \t, but with single quotes, \t is a literal string.
Concatenation: Joining Strings
You can join strings, a process called concatenation, by using a dot (.).
A Boolean data type can only be either true or false. You can assign a Boolean value to a variable as follows:
$myBool = true;
Boolean values are used when comparing values and expressions for conditional statements.
The following values are evaluated as false by PHP:
If a variable contains a value that is not evaluated as false, it is assigned the value true.
The case-insensitive constant null is used to represent a variable with no value.
$myNull = null; // variable is set to null
The null value evaluates differently, depending on the context in which the variable is used. If evaluated as a bool, it becomes false; as a number, it becomes zero (0); and as a string, it becomes an empty string ("").
In PHP, it is possible to use variables that have not been assigned a value. Such undefined variables are then automatically created with the null value.
echo $myUndefined; // variable is set to null