PHP Data Types

0
1735

In this data types tutorial we are going to learn about various data types used in PHP.

But before that we will be learning about print statement which has the same function of displaying data as the echo statement.

  • Print statement:
    • print statement is also used to display something on the webpage or browser.
    • It is similar to echo statement.
    • But unlike echo statement, print statement can display only one string and returns value 1.
    • Like echo statement print statement can also be used with or without parenthesis ().
    • Print like echo statements can be used to display strings, values of variables etc.
    • But compared to echo, print is slower in execution since it returns a value.
    • To view it practically create a new folder datatypes in the htdocs folder of xampp folder and then create an index.php file in the newly created datatypes folder.
    • Write the following code to demonstrate print statement in the index.php file:
    • The above given code contains print statements that has string, HTML tags etc.
    • We can also use concatenation operator period (.) to join two strings in print statement like echo statement.
    • We can even print value of a variable using print statement just like echo statement.
    • The output is shown below:
    • print_output
      fig 1

    • But we had quoted first that print statement can print only one string and not multiple strings. This will be understood by the following example code:
    • Write the following echo statement first and see the output:
    • This echo statement has 5 strings separated with comma. All these strings are displayed in the output shown below:
    • echo_stmt_displaying_multiple_strings
      fig 2

    • But now let us try to display multiple strings at a time using print statement. Write the following code given below:
    • This print statement cannot display multiple strings separated by comma at a time.
    • This statement will give an error as shown below:
    • error_for_displaying_multiple_stms_using_print
      fig 3

      Thus we have learned one more method to display data in the form of print statement.

  • Data Types:
    • Data Types in general are the types that allow storage of a particular type of value.
    • A memory location occupied by a variable is commanded to allow storage of a particular type of values using data types.
    • But PHP is a loosely typed language, hence there is no need to specify about which data type value a variable should hold.
    • PHP has the ability to decide a data type of a particular variable according to the value stored in it.
    • The data types in PHP are,
      • String
      • Integer
      • Float
      • Boolean
      • Array
      • Object
      • NULL
    • Let us study about each data type in detail:
      1. String:
        • A string is a sequence of characters.
        • Any words, characters, numbers, symbols or sentence enclosed in double or single quotes is termed as a string.
        • Eg. “I’m learning Data Types in PHP”,”12345”.
        • The variable storing such values is said to store a String data type value.
      2. Integer:
        • An integer is a number without decimal point.
        • Integer contains at least one number from 0 to 9.
        • Integers don’t have decimal points.
        • You cannot have comma or blanks in an integer number.
        • Integers can be negative or positive.
        • Integers can be specified in 3 formats
          1. Decimal (base 10)
            • Eg: $x=5689;
          2. Hexadecimal (base 16)
            • These numbers are prefixed with 0x.
            • Eg: $y=0x80;
          3. Octal (base 8)
            • These numbers are prefixed with 0.
            • Eg: $z=045;
        • The variable storing such values is said to store an Integer data type value.
      3. Floating Point Numbers:
        • Floating point numbers are the numbers with decimal point or the numbers in exponential format.
        • These are also called as floats, doubles or real numbers.
        • Eg: $a=276.4;
      4. Boolean:
        • Boolean variables can have either true or false values.
        • They can be used mostly in conditional statements.
        • Mostly the value zero (0) evaluates to false and the value other than zero evaluates to true.
        • Eg: $b=true;
        • Eg: $S=false;
      5. Array:
        • Array is a data structure that can store number of similar type values in a single variable.
        • Eg: $fruits=array(“Apple”,”Mango”,”Orange”,”Guava”);
      6. Object:
        • It is a data type used to store objects.
        • Objects are the instances of a program construct called Class.
        • Class is an entity that stores data and methods to process the data.
        • Objects for classes are declared explicitly.
        • Eg: $bus=new bus();
      7. NULL:
        • The variable containing no value contains NULL value.
        • NULL is only possible value of type NULL.
        • This is used to make a variable empty. i.e. NULL value is assigned to a variable to make it empty.
        • The NULL value can be used to identify whether a variable is empty.
        • Eg. $bag=null;
    • These are the data types in PHP.
    • Note that in PHP if a variable is given a string value it becomes a String, if it is given an integer value it becomes an integer and so on.
    • Let us see an example that demonstrates the above statement:
      • Here, in the above code we have used variable $x which is assigned different type of value every time.
      • If it is assigned a string like “Caroline”, it becomes a string. If it is assigned an integer like 123, it becomes an integer. A float value makes it a float.
      • We have seen before that value 0 indicates false and value other than zero indicates true in Boolean data type. It can behave vice-versa also.
      • All the values assigned to variable $x are displayed using echo statement.
      • The output is shown below:
      • different_values_assigned_to_$x
        fig 4

  • We can even get confirmed the data type of a variable containing a particular value by using inbuilt function var_dump().
  • Let us have another example to confirm the types of variables:
  • Write the following code:
    • Here, we have declared various variables with different types of values.
    • These variables are passed to inbuilt var_dump() function each.
    • The task of var_dump() function is to return the data type of the variable passed as a parameter to it.
    • The output is shown below:
    • var_dump_func_output
      fig 5

    • Type Conversion:
      • There are certain situations when we need to convert the variables from one data type to another.
      • There are two ways to convert variables from one type to another.
        1. Implicit conversion
        2. Explicit conversion
      • Let us see both of them in detail:
      1. Implicit Conversion:
        • The most common way of conversion of variable from one type to another is the implicit conversion.
        • This is the conversion which the PHP language engine handles automatically.
        • This conversion takes place automatically when a certain operation containing values of different data types is performed.
        • Examples of most places where implicit conversions take place are:
          • If one float value and one integer value is added the result of the expression will be converted to float data type.
          • In the places where the expressions need to be evaluated to Boolean type, they are automatically converted to Boolean type.
          • Certain methods that expect everything to be converted to string, there each value is converted to string. For eg. In echo statement.
      2. Explicit conversions:
        • In some cases where PHP does not convert variables to your desired data types, there we have to force the variables to be converted to other data types by using type casting.
        • Type casting in PHP is much like that used in C programming language.
        • To explicitly convert a variable you just have to prefix the variable with the desired data type surrounded by parenthesis.
        • Let us see which casts should we use for explicit conversions:
          • For type casting to integer data type use (int) or (integer).
          • For type casting to floating point data type use (float) or (double) or (real).
          • For type casting to string data type use (string).
          • For type casting to Boolean data type use (bool) or (boolean).
          • For type casting to array data type use (array).
          • For type casting to object data type use (object).
        • Examples of some type casts are given below:
          1. Echo (int)3.99;
            • Here the value after decimal point is truncated and 3 is displayed.
          2. Echo (int)”123”;
            • The output here is integer type 123.
          3. $f= (float)”126.56”;
            • The output here will be float type 126.56.
          4. $p=(bool)45;
            • The output here is 1, since all numbers other than 0 returns true and 1 represents true.
        • The code for above examples is given below:
        • The output of all the above examples is shown in the figure below:
        • type_casting_examples
          fig 6

  • Functions to identify, and set data types for variables:
    • There are certain useful functions used to identify data types of variables.
    • The functions are as follows:
      1. is_type function:
        • In is_type function the word type is to be replaced with the desired data type.
        • The is_type function is used to identify whether a particular variable is of the specified type.
        • This function returns true if the variable is of the specified type otherwise it returns false.
        • Is_type is the general form of the function. This function is used as is_integer, is_null, is_string, is_float, is_bool, is_object, is_array etc.
      2. Gettype:
        • This gettype function is similar to is_type function. The only thing is that it doesn’t return Boolean values but it returns names of the data types directly.
        • The types returned are “boolean” for Boolean, “integer” for integer, “string” for string type, “array” for array type, “object” for object type, “double” for float type (float is not returned).
        • But this function is not used frequently because is_type function is mostly used.
      3. Settype:
        • This settype function is used to convert a variable from one type to another.
        • This settype function takes two parameters. First is the variable to be converted and second is the data type to which the variable is to be converted.
        • This function returns a Boolean value that indicates whether the conversion was successful or not.
        • Eg: $no=1000;
          settype($no,”string”);
        • The converted value in the string format i.e. “1000” will be stored in the $no variable itself.
        • But as settype function does the same work like type casting it is not mostly used.

Thus we went through the data types in PHP and details about it in this PHP Data Types tutorial.

LEAVE A REPLY

Please enter your comment!
Please enter your name here