Values stored in a variable or in a constant are stored as a specific type of data. PHP provides eight data types.
Integer: A whole number; numeric values without a decimal point, for example, 2 or 5
Floating-point Number (float): A numeric value with decimal point, for example, 2.3
String: A series of characters which are surrounded by either single or double quotes
Boolean: A 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 and Floating Numbers
Integers are whole numbers, such as 1, 10, and 333. Floating-point numbers, also called real numbers, are numbers that contain a decimal value, such as 3.1 or .667. PHP stores the value as an integer or a float automatically.
Character Strings in PHP
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 vs Double Quotes
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 (.).
Boolean Data Type
A Boolean data type takes on only the values of true or false. You can assign a Boolean value to a variable as follows:
$var1 = true;
Boolean values are used when comparing values and expressions for conditional statements.
The following values are evaluated as false by PHP:
- The word false
- The integer 0
- The floating-point number 0.0
- An empty string
- A string with the value 0
- An empty array
- An empty object
- The value NULL
If a variable contains a value that is not evaluated as false, it is assigned the value true.