PHP Basics: DataTypes

February 4, 2009 · Filed Under PHP 

In PHP, a variable’s datatype determines the values it may contain, plus the operations that may be performed upon it. for instance, It will make perfect sense to add variables whose values are numbers (2 + 3), but adding variables whose values are characters (x + y) wont make much sense.

PHP supports many different datatypes, but they are generally broken into three categories:
scalar, composite, and special.

Scalar Datatypes
A scalar value contains only one value at any given time. Several datatypes falls under this category, including integer, float, string, and boolean.

  • Integer: Quite simply a whole number, or a number that doesn’t contain fractional parts, such as 0, 1, 25, -16, or 418475. The range of integers allowed is platform-dependent, but in general, this data type will most likely be large enough for the numbers your program will use. if the maximum supported size is surpassed, the number will be automatically converted to a float.
  • An Integer can be a decimal number (base 10), an octal number (base 8), or an hexadecimal number.

    1. $a = 15; // decimal
    2. $b = -12548;  // decimal
    3. $c = 0345; // octal
    4. $d = 0xF3C // hexadecimal
  • Float: A floating-point number, Also referred to as float, double, or real number, in general, a number that contains fractional parts, such as
    1. $a = 15.5;
    2. $b = 7.0;
    3. $c = 45.3e3;
    4. $d = 3.24E+12
  • String: A sequence of single characters that could be text, but it could also be the contents of an image file, a spreadsheet, or even a music recording. There is no practical limit on the length of a string.
  • We will talk about string more in details in an upcoming post.

  • Boolean: The Boolean datatype has only two possible values: true and false (case insensitive). it’s generally used for simple flags that track true/false conditions.
  • Alternatively, there are other values that can be used as true/false:

    - A number (either integer or float) is evaluated to false if its value is zero, and true otherwise.

    - A string is evaluated to false only if it is empty or if it contains the single character
    0. and evaluated to true if otherwise (even multiple zeros).

    - When converted to a number or a string, a Boolean becomes 1 if it is true, and
    0 otherwise.

Assigning datatypes

Unlike other programming languages, such as C/C++, or Java, PHP is not a strongly-typed language, which means that variables doesn’t need to be declared before they can be used. This involves stating the variable’s type and name, as you’ve already seen in other programming language.

  1. <?php $a = 1; //  integer
  2. $b = 1.25 //  float
  3. $c = $a * $b; // float
  4. ?>

NB: When an expression involves and integer operand and a floating number operand, the result is automatically converted to a floating number.

Type Casting
Type Casting is the process by which we force a variable to behave as a type other than the one originally intended for it. This is done to take advantage of certain features of type hierarchies. For instance, values from a more limited set, such as integers, can be stored in a more compact format and later converted to a different format (float) enabling operations not previously possible, such as division with several decimal places’ worth of accuracy.

PHP explicit conversion is a c-like casting:

  1. <?php
  2. $a = 45;
  3. $b = (int) $a;  // variable
  4. $c = (string) $a;  // string
  5. $d = (float) $a;  // floating number
  6. ?>

Detecting the data type of a PHP variable
To find out the data type of a variable, use PHP gettype() function as shown below:

  1. <?php
  2. $age = 27;
  3. $mile = 15.21;
  4. $flag = TRUE;
  5. $foo = "hello world";
  6.  
  7. // Get datatypes
  8.  echo gettype($age);   // Returns Integer  
  9.  echo gettype($mile);   // Returns Double (for historical reasons "double" is returned in case of a float, and not simply "float")
  10.  echo gettype($flag);   // Returns Boolean  
  11.  echo gettype($foo);   // Returns String  
  12. ?>

Alternatively you can use other functions to check the variable datatype:

  1. is_ array()   // Finds whether a variable is an array
  2. is_ binary()   // Finds whether a variable is a native binary string
  3. is_ bool()   // Finds out whether a variable is a Boolean
  4. is_ double()    // Alias of is_float()
  5. is_ float()    // Finds whether the type of a variable is float
  6. is_ int()   // Find whether the type of a variable is integer
  7. is_ integer()   // Alias of is_int()
  8. is_ lon()   // Alias of is_int()
  9. is_ null()   // Finds whether a variable is NULL
  10. is_ numeric()  //
  11. is_ object()   // Finds whether a variable is a number or a numeric string
  12. is_ real()   // Alias of is_float()
  13. is_ resource()   // Finds whether a variable is a resource
  14. is_ scalar()   // Finds whether a variable is a scalar
  15. is_ string()   // Find whether the type of a variable is string
  16. var_ dump()   // Dumps information about a variable
  17. print_r()   // Prints human-readable information about a variable

To be Continued

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Comments

Comments are closed.