Professional Documents
Culture Documents
PHP Data Types: Scalar Types
PHP Data Types: Scalar Types
World” within a variable, you choose the type string over the type integer.
Why? A string is a sequence of characters while an integer is a non-decimal
number between -2,147,483,648 and 2,147,483,647. For practicality
purposes, using a string makes more sense, and if you try to assign “Hello
World” to an integer, PHP will kindly tell you that you cannot do such a thing.
Here are the eight basic data types used to create variables in PHP:
50
Chapter 4 Data and Data Types
• Boolean
• Integer
• Float
• String
Boolean
A bool or boolean type is the simplest type. Bool expresses a “truth” value
of either true or false.
To specify a bool literal, use the constants true or false. Both are case-
insensitive.
<?php
$foo = True; // set the value TRUE to $foo
?>
<?php
if ($isTuesday) {
echo "Taco Tuesday!!";
}
?>
51
Chapter 4 Data and Data Types
Integer
An int is a non-decimal number between -2,147,483,648 and 2,147,483,647.
Integers can be specified in four different bases:
1) Decimal (base 10) [ 1, 2, 3, 4, 5, 6, etc.]
<?php
$a = 1234; // decimal number
$a = 0123; // octal number (equivalent to 83 decimal)
$a = 0o123; // octal number (as of PHP 8.1.0)
$a = 0x1A; // hexadecimal number (equivalent to 26 decimal)
$a = 0b11111111; // binary number (equivalent to 255 decimal)
$a = 1_234_567; // decimal number (as of PHP 7.4.0)
?>
Float
A float (floating point number) is a number with a decimal point or a
number in exponential form.
<?php
$a = 1.234;
$b = 1.2e3;
52
Chapter 4 Data and Data Types
$c = 7E-10;
$d = 1_234.567; // as of PHP 7.4.0
?>
String
A string is a sequence of characters, like “Hello World!”.
A string can be any text inside quotes. You can use single or
double quotes.
<?php
$foo = "Hello World!";
$bar = 'Hello World!';
?>
• Single quotes
• Double quotes
• Heredoc syntax
• Nowdoc syntax
53
Chapter 4 Data and Data Types
<?php
echo 'this is a single quoted string';
54
Chapter 4 Data and Data Types
When using double quotes ("), PHP interprets the following escape
sequences for special characters:
Sequence Meaning
55
Chapter 4 Data and Data Types
A third method to create a string data type in PHP is with the heredoc
syntax: <<< . This method is especially useful for large amounts of
preformatted text. To use the heredoc, you simply start with this operator,
<<<, and afterwards it is followed by an identifier that marks the name
or reference of this string and then a newline. The string comes next and
then the same identifier from the beginning is used to close or end the
quotation. It looks something like this:
<?php
echo <<<MYIDENTIFIER
Here is
The text that
I want to display
MYIDENTIFIER;
?>
<?php
echo <<<"FOOBAR"
Hello World!
FOOBAR;
?>
56
Chapter 4 Data and Data Types
<?php
Echo <<<'FOOBAR'
Text that will not get parsed,
This will just show up
FOOBAR
?>
<?php
$tea = "earl grey";
57
Chapter 4 Data and Data Types
<?php
$great = 'fun';
// Works
echo "This square is {$square->width}00 centimeters broad.";
// Works, quoted keys only work using the curly brace syntax
echo "This works: {$arr['key']}";
// Works
echo "This works: {$arr[4][3]}";
58
Chapter 4 Data and Data Types
// Works.
echo "This works: " . $arr['foo'][3];
echo "This is the value of the var named by the return value of
getName(): {${getName()}}";
echo "This is the value of the var named by the return value of
\$object->getName(): {${$object->getName()}}";
<?php
class foo {
var $bar = 'I am bar.';
}
59
Chapter 4 Data and Data Types
• substr()
• strlen()
• str_replace()
• trim()
• strpos()
• strtolower()
• strtoupper()
• is_string()
• strstr()
substr( )
string substr(string string, int start[, int length] );
60