PHP Basics: DataTypes

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.

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

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Leave Comment