PHP Strings Strings: Xample
PHP Strings Strings: Xample
Strings
Strings in PHP are surrounded by either double quotation marks, or single
quotation marks.
EXAMPLE
echo "Hello";
echo 'Hello';
E.g. when there is a variable in the string, it returns the value of the variable:
EXAMPLE
$x = "John";
Single quoted strings does not perform such actions, it returns the string like it was written,
with the variable name:
EXAMPLE
$x = "John";
String Length
The PHP strlen() function returns the length of a string.
EXAMPLE
Word Count
The PHP str_word_count() function counts the number of words in a string.
EXAMPLE
If a match is found, the function returns the character position of the first
match. If no match is found, it will return FALSE.
EXAMPLE
PHP Numbers
There are three main numeric types in PHP:
Integer
Float
Number Strings
In addition, PHP has two more data types used for numbers:
Infinity
NaN
PHP Integers
2, 256, -256, 10358, -179567 are all integers.
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()
EXAMPLE
CHECK IF THE TYPE OF A VARIABLE IS INTEGER:
$x = 5985;
var_dump(is_int($x));
$x = 59.85;
var_dump(is_int($x));
PHP Floats
A float is a number with a decimal point or a number in exponential form.
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_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()
EXAMPLE
$x = 10.365;
var_dump(is_float($x));
PHP Infinity
PHP has the following functions to check if a numeric value is finite or infinite:
is_finite()
is_infinite()
However, the PHP var_dump() function returns the data type and value:
EXAMPLE
var_dump($x);
PHP NaN
NaN stands for Not a Number.
NaN is used for impossible mathematical operations.
PHP has the following functions to check if a value is not a number:
is_nan()
However, the PHP var_dump() function returns the data type and value:
EXAMPLE
$x = acos(8);
var_dump($x);
$x = 5985;
var_dump(is_numeric($x));
$x = "5985";
var_dump(is_numeric($x));
$x = "59.85" + 100;
var_dump(is_numeric($x));
$x = "Hello";
var_dump(is_numeric($x));
EXAMPLE
$x = 23465.768;
$int_cast = (int)$x;
echo $int_cast;
echo "<br>";
$x = "23465.768";
$int_cast = (int)$x;
echo $int_cast;
PHP Casting
Example
$a = 5; // Integer
$b = 5.34; // Float
$c = "hello"; // String
$d = true; // Boolean
$e = NULL; // NULL
$a = (string) $a;
$b = (string) $b;
$c = (string) $c;
$d = (string) $d;
$e = (string) $e;
//To verify the type of any object in PHP, use the var_dump() function:
var_dump($a);
var_dump($b);
var_dump($c);
var_dump($d);
var_dump($e);