PHP Data Types
Variables can store data of different types, and different data types can do different things.
PHP supports the following data types:
PHP String
A string is a sequence of characters, like "My First PHP Program!".
A string can be any text inside quotes. You can use single or double quotes:
PHP String Functions
1.strlen() - Return the Length of a String
The PHP strlen() function returns the length of a string.
Return the length of the string:
2.str_word_count() - Count Words in a String
The PHP str_word_count() function counts the number of words in a string.
Count the number of word in the string "Welcome To Tutorial Codeplay ":
3.strrev() - Reverse a String
The PHP strrev() function reverses a string.
Reverse the string "Tutorial Codeplay"
4.strpos() - Search For a Text Within a String
The PHP strpos() function searches for a specific text. If a match is found, the function returns the character position of the first match. If no match is found, it will return FALSE.
Search for the text "Tutorial" in the string "Tutorial Codeplay!":
5.str_replace() - Replace Text Within a String
The PHP str_replace() function replaces some characters with some other characters in a string.
Following example explain Replace the text "dot" with "Codeplay"
PHP Integer
An integer data type is a non-decimal number between -2,147,483,648 and 2,147,483,647.
The PHP var_dump() function returns the data type and value.
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
PHP Float
A float (floating point number) is a number with a decimal point or a number in exponential form.
In the following Example $x is a float.
PHP Boolean
A Boolean represents two possible states: TRUE or FALSE.
A boolean value represents a truth value. In other words, a boolean value can be either true or false. PHP uses the bool type to represent boolean values.
$x = true;
$y = false;
PHP Array
An array stores multiple values in one single variable.
In the following Example $colors is an array. The PHP var_dump() function returns the data type and value:
PHP Object
A class is a template for objects, and an object is an instance of a class.
PHP NULL Value:
Null is a special data type which can have only one value: NULL.
PHP Resource
A resource is a special variable, holding a reference to an external resource.
Resource variables typically hold special handlers to opened files and database connections.