PHP Data Types (Scalar)

Though there are eight data types in PHP, no keywords are defined for
denoting them explicitly. That means, there is no explicit syntax
for declaring variables in PHP. A variable may hold a value of any
type. The first time the value of a variable is set, the variable is
created. For e.g., the following code declares a variable of type
integer in PHP:
$day = 60*60*24;
echo “There are $day seconds in a day.\n”;
A
variable whose value has not been set has the NULL value. We can
replace a variable’s value with another value of a different type
during the execution of a program as follows:
$what = “Fred”;
echo $what;
$what = 35;
echo $what;
Integers:
Integers are whole numbers, such as 1, 12 and 256. The range of
acceptable values varies according to the details of the platform
used for running PHP, but typically extends from -2,147,483,648 to
+2,147,483,647. Integer literals can be written in decimal, octal
and hexadecimal.
Decimal values are represented by a sequence of digits, without
leading zeros. The sequence may begin with a plus (+) or minus (-)
sign. If there is no sign, positive sign is assumed. Examples of
decimal integers include the following:
1998
-641
+33
Octal numbers consist of a leading 0 and a sequence of digits from 0
to 7. Like decimal numbers, octal numbers can be prefixed with a
plus or minus sign. Here are some example octal values and their
equivalent decimal values:
0755 //decimal 493
+010 //decimal 10
Hexadecimal values begin with 0x, followed by a sequence of digits
(0-9) or letters (A-F). The letters can be upper- or lowercase but
are usually written in capitals. Like decimal and octal values, we
can include a sign in hexadecimal numbers:
0xFF //decimal 255
0x10 //decimal 16
-0xDAD1 //decimal -56017
Use the is_int() function (or is_integer()) to test
whether a variable contains an integer or not:
if (is_int($x))
{
//$x is an integer
}
Flating-point
Numbers:
Floating-point numbers (often referred to as real numbers) represent
numeric values and some decimal digits. Like integers, their limits
depend on the machine’s details. Usually, they allow numbers
between 1.7E-308 and 1.7E+308 with 15 digits of accuracy.
PHP recognizes floating-point numbers written in two different
formats. The first one is the simple notation:
3.14
0.017
-7.1
The second format for representing floating-point number is the
scientific notation:
0.314E1 //0.314*10 or 3.14
17.0E-3 //17.0*10-3 or 0.017
Use the is_float() function to test whether a value is a
floating-point number:
if (is_float($x))
{
//$x is a floating-point number
}
Strings:
A string is a sequence of characters of arbitrary length. String
literals are delimited by either single or double quotes:
‘Hello, World!’
“Hi, $name”
Variables are expanded within double quotes, while within single
quotes, they are not:
$name = “Guido”;
echo “Hi, $name\n”;
echo ‘Hi, $name\n’;
will display the following output:
Hi, Guido
Hi, $name
Double quotes also support a variety of string escape sequence
characters listed in the below table:
Escape
Sequence
|
Character
Represented
|
\”
|
Double
quotes
|
\n
|
Newline
|
\r
|
Carriage
return
|
\t
|
Tab
space
|
\\
|
Backslash
|
\$
|
Dollar
sign
|
\{
|
Left
curly brace
|
\}
|
Right
curly brace
|
Table: List of Escape Sequences (to be used in Double Quotes)
A single-quoted string can recognize only two escape-sequence
character:
\\ - Backslash
\’ - Single quote
To test whether two strings are equal, use the double equals (==)
comparison operator:
If ($a == $b){
Echo “a and b are equal”;
}
Use the is_string() function to test whether a variable is of
type string:
if (is_string($x)){
//$x is a string
}
PHP provides operators and functions to compare, disassemble,
assemble, search, replace and trim strings.
Booleans:
A Boolean value represents a “truth value” – it says whether
something is true or not. Truth and falseness are the outcome of
conditional code such as:
if ($alive)
{……………}
In
PHP, the following values all evaluate to false:
- The keyword false
- The integer 0
- The floating point value 0.0
- The empty string “” and the string “0”
- An array with zero elements
- An object with no values or functions
- The NULL value
Examples for variables containing boolean values are as follows:
$x = 5; //$x has a true value
$x = true;
$y = “”; //$y has a false value
$y = false;
A value that is not false is true; PHP provides true and false
keywords for clarity. Use the function is_bool() to test
whether a value is a Boolean or not as follows:
if (is_bool($x))
{
echo “variable x is a Boolean variable”;
}