PHP – Data Types

All data stored in PHP variables fall into one of eight basic categories, known as data types . A variable ’ s data type determines what operations can be carried out on the variable ’ s data, as well as the amount of memory needed to hold the data.
PHP supports four scalar data types. Scalar data means data that contains only a single value. Here ’ s a list of them, including examples:

Scalar Data TypeDescriptionExample
IntegerA whole number15
FloatA floating – point number8.23
StringA series of characters“Hello world!”
BooleanRepresents either true or falsetrue

As well as the four scalar types, PHP supports two compound types. Compound data is data that can contain more than one value. The following table describes PHP ’ s compound types:

Compound Data TypeDescription
ArrayAn ordered map (contains names or numbers mapped to values)
ObjectA type that may contain properties and methods

Finally, PHP supports two special data types, so called because they don ’ t contain scalar or compound data as such, but have a specific meaning:

Special Data TypeDescription
ResourceContains a reference to an external resource such as a file or database
NullMay only contain null as a value meaning the variable explicitly does not contain any value

Resource types are often returned by functions that deal with external applications or files. The type NULL is reserved for variables that have not been initialized(that is, variables that have not yet had a value assigned to them).
You can use PHP’s built-in function gettype() to test the type of any variable. If you place a variable between the parentheses of the function call, gettype() returns a string representing the relevant type. In Below assigns five different data types to a single variable, testing it with gettype() each time. The commentsin the code show you where the script is in the process.

 <title>Listing 4.1 Testing the type of a variable</title>
 $testing; // declare without assigning
 echo gettype($testing); // null
 echo “<br>”;
 $testing = 5;
 echo gettype($testing); // integer
 echo “<br>”;
 $testing = “five”;
 echo gettype($testing); // string
 echo “<br>”;
 $testing = 5.0;
 echo gettype($testing); // double
 echo “<br>”;
 $testing = true;
 echo gettype($testing); // boolean
 echo “<br>”;
  • Boolean are the simplest of all PHP data types. Like a switch that has only two states, on and off, it consist of a single value that may be set to either 1 (true) or 0( False). In this listing, $validUser is a Boolean variable set to tru.
  • PHP also support two numeric data types: integers and floating-point values. Floating point value(also known as floats or doubles) are decimal or fractional numbers, while integers are round numbers. Both may be less than, greater than, or equal to zero. In this listing, $size holds an integer value, while $temp holds a floating value.
  • For non- numeric data, PHP offers the string data type , which can hold letters, number, and special characters. String values must be enclosed in either single quotes or double quotes. In the previous listing, $cat is string variable containing the vale ‘Siamese’.
  • You may also encounter NULL data type , which is a “special“ data type first introduced in PHP4. NULLs are used to represent “empty” variable in PHP; a variable of type NULL is a variable without any data. In the preceding listing, $here is NULL.

Recommended Tutorials for you

Related posts

String functions

Anuj Kumar

Array in PHP

Anuj Kumar

Types of Errors in PHP

Anuj Kumar

How to Connect PHP with MySQL Database

Anuj Kumar

How to prevent Cross-Site Request Forgery (CSRF) in PHP

Anuj Kumar

How to Fetch data from database Using OOPS In PHP

Anuj Kumar

PHP Constants

Anuj Kumar

Get City Country By IP Address in PHP

Anuj Kumar

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.