revision:
Different data types can do different things, and PHP supports the following data types:
string,
integer,
float (floating point numbers - also called double),
boolean,
array,
object,
NULL,
resource.
A string is a sequence of characters (e.g. "Hello world!"), where a character is the same as a byte. This means that PHP only supports a 256-character set, and hence does not offer native Unicode support. A string can be any text inside quotes, single or double quotes.
An integer data type is a non-decimal number between -2,147,483,648 and 2,147,483,647.
Rules for integers:
- an integer must have at least one digit;
- an integer must not have a decimal point;
- an integer can be either positive or negative;
- integers can be specified in: decimal (base 10), hexadecimal (base 16), octal (base 8), or binary (base 2) notation.
A float (floating point number) is a number with a decimal point or a number in exponential form.
A Boolean represents two possible states: TRUE or FALSE. Booleans are often used in conditional testing.
An array stores multiple values in one single variable.
Classes and objects are the two main aspects of object-oriented programming.
A class is a template for objects, and an object is an instance of a class.
When the individual objects are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.
If you create a __construct() function, PHP will automatically call this function when you create an object from a class.
Null is a special data type, which can have only one value: NULL. A variable of data type NULL is a variable that has no value assigned to it. If a variable is created without a value, it is automatically assigned a value of NULL. Variables can also be emptied by setting the value to NULL.
The special resource type is not an actual data type. It is the storing of a reference to functions and resources external to PHP. A common example of using the resource data type is a database call.
If you assign an integer value to a variable, the type of that variable will automatically be an integer. Then, if you assign a string to the same variable, the type will change to a string.
This automatic conversion can sometimes break your code.
2, 256, -256, 10358, -179567 are all integers.
An integer data type is a non-decimal number between -2147483648 and 2147483647 in 32 bit systems, and between -9223372036854775808 and 9223372036854775807 in 64 bit systems.
A value greater (or lower) than this, will be stored as float, because it exceeds the limit of an integer.
Even if 4 * 2.5 is 10, the result is stored as float, because one of the operands is a float (2.5).
Rules for integers:
- an integer must have at least one digit;
- an integer must NOT have a decimal point;
- an integer can be either positive or negative;
- integers can be specified in three formats: decimal (10-based), hexadecimal (16-based - prefixed with 0x) or octal (8-based - prefixed with 0).
PHP has the following predefined constants for integers:
PHP_INT_MAX - the largest integer supported;
PHP_INT_MIN - the smallest integer supported;
PHP_INT_SIZE - the size of an integer in bytes.
PHP has the following functions to check if the type of a variable is integer:
is_int();
is_integer() - alias of is_int();
is_long() - alias of is_int()
2.0, 256.4, 10.358, 7.64E+5, 5.56E-5 are all floats. The float data type can commonly store a value up to 1.7976931348623E+308 (platform dependent), and have a maximum precision of 14 digits.
PHP has the following predefined constants for floats (from PHP 7.2):
PHP_FLOAT_MAX - the largest representable floating point number;
PHP_FLOAT_MIN - the smallest representable positive floating point number;
- PHP_FLOAT_MAX - the smallest representable negative floating point number;
PHP_FLOAT_DIG - the number of decimal digits that can be rounded into a float and back without precision loss;
PHP_FLOAT_EPSILON - The smallest representable positive number x, so that x + 1.0 != 1.0
PHP has the following functions to check if the type of a variable is float:
is_float();
is_double() - alias of is_float().
A numeric value that is larger than PHP_FLOAT_MAX is considered infinite.
PHP has the following functions to check if a numeric value is finite or infinite:
is_finite(),
is_infinite()
NaN stands for Not a Number and is used for impossible mathematical operations.
PHP has the following functions to check if a value is not a number: is_nan()
The PHP is_numeric() function can be used to find whether a variable is numeric.
The function returns true if the variable is a number or a numeric string, false otherwise.
From PHP 7.0, the is_numeric() function will return FALSE for numeric strings in hexadecimal form (e.g. 0xf4c3b00c), as they are no longer considered as numeric strings.
Sometimes you need to cast a numerical value into another data type. The (int), (integer), or intval() function are often used to convert a value to an integer.
PHP has a set of math functions that allows to perform mathematical tasks on numbers:
the pi() function returns the value of PI.
the min() and max() functions can be used to find the lowest or highest value in a list of arguments.
the abs() function returns the absolute (positive) value of a number.
the sqrt() function returns the square root of a number.
the round() function rounds a floating-point number to its nearest integer.
the rand() function generates a random number.
abs() - returns the absolute (positive) value of a number;
acos() - returns the arc cosine of a number;
acosh() - returns the inverse hyperbolic cosine of a number;
asin() - returns the arc sine of a number;
asinh() - returns the inverse hyperbolic sine of a number;
atan() - returns the arc tangent of a number in radians;
atan2() - returns the arc tangent of two variables x and y;
atanh() - returns the inverse hyperbolic tangent of a number;
base_convert() - converts a number from one number base to another;
bindec() - converts a binary number to a decimal number;
ceil() - rounds a number up to the nearest integer;
cos() - returns the cosine of a number;
cosh() - returns the hyperbolic cosine of a number;
decbin() - converts a decimal number to a binary number;
dechex() - converts a decimal number to a hexadecimal number;
decoct() - converts a decimal number to an octal number;
deg2rad() - converts a degree value to a radian value;
exp() - calculates the exponent of e;
expm1() - returns exp(x) - 1;
floor() - rounds a number down to the nearest integer;
fmod() - returns the remainder of x/y;
getrandmax() - returns the largest possible value returned by rand();
hexdec() - converts a hexadecimal number to a decimal number;
hypot() - calculates the hypotenuse of a right-angle triangle;
intdiv() - performs integer division;
is_finite() - checks whether a value is finite or not;
is_infinite() - checks whether a value is infinite or not;
is_nan() - checks whether a value is 'not-a-number';
lcg_value() - returns a pseudo random number in a range between 0 and 1;
log() - returns the natural logarithm of a number;
log10() - returns the base-10 logarithm of a number;
log1p() - returns log(1+number);
max() - returns the highest value in an array, or the highest value of several specified values;
min() - returns the lowest value in an array, or the lowest value of several specified values;
mt_getrandmax() - returns the largest possible value returned by mt_rand();
mt_rand() - generates a random integer using Mersenne Twister algorithm;
mt_srand() - seeds the Mersenne Twister random number generator;
octdec() - converts an octal number to a decimal number;
pi() - returns the value of PI;
pow() - returns x raised to the power of y;
rad2deg() - converts a radian value to a degree value;
rand() - generates a random integer;
round() - rounds a floating-point number;
sin() - returns the sine of a number;
sinh() - returns the hyperbolic sine of a number;
sqrt() - returns the square root of a number;
srand() - seeds the random number generator;
tan() - returns the tangent of a number;
tanh() - returns the hyperbolic tangent of a number;
NF - value: INF The infinite;
M_E - value: 2.7182818284590452354 - description: returns e;
M_EULER - value: 0.57721566490153286061 - description: returns Euler constant;
M_LNPI - value: 1.14472988584940017414 - description: returns the natural logarithm of PI: log_e(pi);
M_LN2 - value: 0.69314718055994530942 - description: returns the natural logarithm of 2: log_e 2;
M_LN10 - value: 2.30258509299404568402 - description: returns the natural logarithm of 10: log_e 10;
M_LOG2E - value: 1.4426950408889634074 - description: returns the base-2 logarithm of E: log_2 e;
M_LOG10E - value: 0.43429448190325182765 - description: returns the base-10 logarithm of E: log_10 e;
M_PI - value: 3.14159265358979323846 - description: returns Pi;
M_PI_2 - value: 1.57079632679489661923 - description: returns Pi/2;
M_PI_4 - value: 0.78539816339744830962 - description: returns Pi/4;
M_1_PI - value: 0.31830988618379067154 - description: returns 1/Pi;
M_2_PI - value: 0.63661977236758134308 - description: returns 2/Pi;
M_SQRTPI - value: 1.77245385090551602729 - description: returns the square root of PI: sqrt(pi);
M_2_SQRTPI - value: 1.12837916709551257390 - description: returns 2/square root of PI: 2/sqrt(pi);
M_SQRT1_2 - value: 0.70710678118654752440 - description: returns the square root of 1/2: 1/sqrt(2);
M_SQRT2 - value: 1.41421356237309504880 - description: returns the square root of 2: sqrt(2);
M_SQRT3 - value: 1.73205080756887729352 - description: returns the square root of 3: sqrt(3);
NAN - value: NAN - description: Not A Number;
PHP_ROUND_HALF_UP - value: 1 - description: round halves up;
PHP_ROUND_HALF_DOWN - value: 2 - description: round halves down;
PHP_ROUND_HALF_EVEN - value: 3 - description: round halves to even numbers;
PHP_ROUND_HALF_ODD - value: 4 - description: round halves to odd numbers;
Constants are like variables except that once they are defined they cannot be changed or undefined.
A constant is an identifier (name) for a simple value. The value cannot be changed during the script.
A valid constant name starts with a letter or underscore (no $ sign before the constant name).
Unlike variables, constants are automatically global across the entire script.
To create a constant, use the define() function. Syntax: define(name, value, case-insensitive)
name : specifies the name of the constant;
value : specifies the value of the constant;
case-insensitive : specifies whether the constant name should be case-insensitive; default is false.
In PHP7, you can create an Array constant using the define() function.
PHP divides the operators in the following groups:
- arithmetic operators;
- assignment operators;
- comparison operators;
- increment/decrement operators;
- logical operators;
- string operators;
- array operators;
- conditional assignment operators.
operator: +; name: addition; example: $x + $y; result: sum of $x and $y
operator : -; name: subtraction; example: $x - $y; result: difference of $x and $y
operator : *; name: multiplication; example: $x * $y; result: product of $x and $y
operator : /; name: division; example: $x / $y; result: quotient of $x and $y
operator : %; name: modulus; example: $x % $y; result: remainder of $x divided by $y
operator : **; name: exponentiation; example: $x ** $y; result: result of raising $x to the $y'th power
The basic assignment operator in PHP is "=". It means that the left operand gets set to the value of the assignment expression on the right.
assignment: x = y; same as: x = y; description: the left operand gets set to the value of the expression on the right
assignment: x += y; same as: x = x + y; description: addition
assignment: x -= y; same as: x = x - y; description: subtraction
assignment: x *= y; same as: x = x * y; description: multiplication
assignment: x /= y; same as: x = x / y; description: division
assignment: x %= y; same as: x = x % y; description: modulus
operator: ==; name: equal; example: $x == $y; result: returns true if $x is equal to $y
operator: ===; name: identical; example: $x === $y; result: returns true if $x is equal to $y, and they are of the same type
operator: !=; name: not equal; example: $x != $y; result: returns true if $x is not equal to $y
operator: <>; name: not equal; example: $x <> $y; result: returns true if $x is not equal to $y
operator: !==; name: not identical; example: $x !== $y; result: returns true if $x is not equal to $y, or they are not of the same type
operator: >; name: greater than; example: $x > $y; result: returns true if $x is greater than $y
operator: <; name: less than; example: $x < $y; result: returns true if $x is less than $y
operator: >=; name: greater than or equal to; example: $x >= $y; result: returns true if $x is greater than or equal to $y
operator: <=; name: less than or equal to; example: $x <= $y; result: returns true if $x is less than or equal to $y
operator: <=>; name: spaceship; example: $x <=> $y; result: returns an integer less than, equal to, or greater than zero, depending on if $x is less than, equal to, or greater than $y. Introduced in PHP 7.
operator: ++$x; name: pre-increment; description: increments $x by one, then returns $x
operator: $x++; name: post-increment; description: returns $x, then increments $x by one
operator: --$x; name: pre-decrement; description: decrements $x by one, then returns $x
operator: $x--; name: post-decrement; description: returns $x, then decrements $x by one
operator: and; name: And; example: $x and $y; result: true if both $x and $y are true
operator: or; name: Or; example: $x or $y; result: true if either $x or $y is true
operator: xor; name: Xor; example: $x xor $y; result: true if either $x or $y is true, but not both
operator: &&; name: And; example: $x && $y; result: true if both $x and $y are true
operator: ||; name: Or; example: $x || $y; result: true if either $x or $y is true
operator: !; name: Not; example: !$x; result: true if $x is not true
PHP has two operators that are specially designed for strings.
operator: . ; name: concatenation; example: $txt1 . $txt2; result: concatenation of $txt1 and $txt2
operator: .= ; name: concatenation assignment; example: $txt1 .= $txt2; result: appends $txt2 to $txt1
operator: +; name: union; example: $x + $y union of $x and $y
operator: ==; name: equality; example: $x == $y; result: returns true if $x and $y have the same key/value pairs
operator: ===; name: identity; example: $x === $y; result: returns true if $x and $y have the same key/value pairs in the same order and of the same types
operator: !=; name: inequality; example: $x != $y; result: returns true if $x is not equal to $y
operator: <>; name: inequality; example: $x <> $y; result: returns true if $x is not equal to $y
operator: !==; name: non-identity; example: $x !== $y; result: returns true if $x is not identical to $y
>operator: ?: ; name: ternary; example: $x = expr1 ? expr2 : expr3; result: returns the value of $x. The value of $x is expr2 if expr1 = TRUE. The value of $x is expr3 if expr1 = FALSE
operator: ??:; name: null coalescing $x = expr1 ?? expr2; result: returns the value of $x. The value of $x is expr1 if expr1 exists, and is not NULL. If expr1 does not exist, or is NULL, the value of $x is expr2. Introduced in PHP 7.