• Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Matt Doyle | Elated Communications

Web and WordPress Development

  • About Me
  • Blog
    • Design & Multimedia
      • Photoshop
      • Paint Shop Pro
      • Video & Audio
    • Online Marketing
      • E-Commerce
      • Social Media
    • Running a Website
      • WordPress
      • Apache
      • UNIX and Linux
      • Using FTP
    • Web Development
      • HTML
      • CSS
      • JavaScript
      • PHP
      • Perl and CGI Scripting
  • Portfolio
  • Contact Me
  • Hire Me
Home / Blog / Web Development / PHP / PHP Data Types

PHP Data Types

9 June 2009 / 3 Comments

Most PHP scripts deal with data in one form or another, usually stored in variables. PHP can work with different types of data. For example, a whole number is said to have an integer data type, while a string of text has a string data type.

In this article you explore PHP’s data types; look at loose typing; learn how to discover the type of a given value; and see how to change data types.

PHP’s scalar data types

Scalar data is data that only contains a single value. As of version 6, PHP features 6 scalar data types:

Type Description Example values
integer A whole number 7, -23
float A floating point number 7.68, -23.45
string A sequence of characters "Hello", "abc123@#$"
unicode A sequence of Unicode characters "Hello", "abc123@#$"
binary A sequence of binary (non-Unicode) characters "Hello", "abc123@#$"
boolean Either true or false true, false
The unicode and binary types were added in PHP 6. Earlier PHP versions just have a string type.

PHP’s compound data types

Compound data can contain multiple values. PHP has 2 compound data types:

array
Can hold multiple values indexed by numbers or strings
object
Can hold multiple values (properties), and can also contain methods (functions) for working on properties

An array or object can contain multiple values, all accessed via one variable name. What’s more, those values can themselves be other arrays or objects. This allows you to create quite complex collections of data.

PHP’s special data types

As well as scalar and compound types, PHP has 2 special data types that don’t fall into the other categories:

resource
Used to access an external resource (for example, a file handle or database connection)
null
Can only contain the value null, which means “no value”

Loose typing in PHP

Some languages, such as Java, are strongly typed: once you’ve created a variable, you can’t change the type of data stored in it. PHP, in contrast, is loosely typed: you can change a variable’s data type at any time.

In the following PHP example, $x starts off by holding integer data (3). The next line appends the string “hello” to the data using the concatenation operator, which changes $x‘s value to “3hello” (a string data type). Finally, 5.67 is assigned to $x, changing its data type to a float:


$x = 3;
$x .= "hello";
$x = 5.67;

Finding out the data type of a value

You can check a value’s data type using one of the following PHP functions:

is_int( value )
Returns true if value is an integer, false otherwise
is_float( value )
Returns true if value is a float, false otherwise
is_string( value )
Returns true if value is a string, false otherwise
is_unicode( value )
Returns true if value is a Unicode string, false otherwise
is_binary( value )
Returns true if value is a binary string, false otherwise
is_bool( value )
Returns true if value is a Boolean, false otherwise
is_array( value )
Returns true if value is an array, false otherwise
is_object( value )
Returns true if value is an object, false otherwise
is_resource( value )
Returns true if value is a resource, false otherwise
is_null( value )
Returns true if value is null, false otherwise
is_unicode() and is_binary() are only available in PHP 6 and later.

For example, the following code displays 1 (true):


$x = 3.14;
echo is_float( $x );

Changing data types with settype()

As you’ve already seen, you can change a variable’s data type simply by assigning a new value of a different type to the variable. However, sometimes it’s a good idea to explicitly set the type of the data held by a variable. For example, if you’re handing data entered by a visitor, or passing data to another program, then it’s good to ensure that the data is of the correct type.

PHP has a function called settype() that converts a variable’s value from one data type to another:


$x = 3.14;
settype( $x, "integer" );

In the above example, $x‘s data type is changed from float to integer (with the result that $x ends up containing the value 3).

Be careful when changing data types, since you may end up losing information — for example, when changing a float (3.14) to an integer (3).

Changing data types with casting

If you just want to change a value’s type at the time you use the value, then you can use casting. This merely changes the type of the value that is to be used; the original variable remains untouched.

To cast a value, place the data type in parentheses before the value at the time you use it:


$x = 3.14;
echo (integer) $x;

The above code displays 3 (3.14 cast to an integer). However, note that $x still contains the value 3.14 after the cast operation.

You can, if you prefer, use (int) instead of (integer), and (bool) instead of (boolean).

You now know all about PHP data types. You’ve looked at PHP’s various scalar, compound and special data types, learned about PHP’s loose typing, and discovered how to read and change data types in your PHP scripts. Happy coding!

Filed Under: PHP Tagged With: array, binary, boolean, cast, casting, float, integer, is_array, is_binary, is_bool, is_float, is_int, is_null, is_object, is_resource, is_string, is_unicode, loosely typed, null, object, php data types, resource, settype, string, Unicode

Reader Interactions

Comments

  1. nothingchen says

    13 October 2012 at 11:29 am

    Thank you. very much helpful 🙂

    Reply
  2. olidev says

    17 July 2017 at 2:18 am

    Objects are also a type of data in PHP, but it is not mentioned in this article. Arrays and function are some examples of an Object. Here is how a new Object is created.

    $car = new ferrari();

    Source: https://www.cloudways.com/blog/php-data-types-and-variable-concepts/

    Reply
  3. chrishirst says

    17 July 2017 at 5:24 am

    Objects are NOT really data ‘types‘ which is why they are not covered as simple data ‘types’

    ‘Objects’ are an abstract structure of variables which are containers of scalar data types, arranged as an associative array

    PLUS;

    ” but it is not mentioned in this article.”

    Had you actually read the article all the way through you would have found arrays and objects covered in the section headed with

    “PHP’s compound data types“

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

To include a block of code in your comment, surround it with <pre> ... </pre> tags. You can include smaller code snippets inside some normal text by surrounding them with <code> ... </code> tags.

Allowed tags in comments: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre> .

Primary Sidebar

Hire Matt!

Matt Doyle headshot

Need a little help with your website? I have over 20 years of web development experience under my belt. Let’s chat!

Matt Doyle - Codeable Expert Certificate

Hire Me Today

Call Me: +61 2 8006 0622

Stay in Touch!

Subscribe to get a quick email whenever I add new articles, free goodies, or special offers. I won’t spam you.

Subscribe

Recent Posts

  • Make a Rotatable 3D Product Boxshot with Three.js
  • Speed Up Your WordPress Website: 11 Simple Steps to a Faster Site
  • Reboot!
  • Wordfence Tutorial: How to Keep Your WordPress Site Safe from Hackers
  • How to Make Awesome-Looking Images for Your Website

Footer

Contact Matt

  • Email Me
  • Call Me: +61 2 8006 0622

Follow Matt

  • E-mail
  • Facebook
  • GitHub
  • LinkedIn
  • Twitter

Copyright © 1996-2023 Elated Communications. All rights reserved.
Affiliate Disclaimer | Privacy Policy | Terms of Use | Service T&C | Credits