payment by upi: sinhamit@icici or payment by bank account name: amit kumar sinha, account number: 2646728782 IFSC code: KKBK0005660 SWIFT: KKBKINBB

Please support if you like my work by payment through upi: sinhamit@icici or payment by bank

account name: Amit Kumar Sinha,
account number: 2646728782
IFSC code: KKBK0005660
SWIFT: KKBKINBB


Data Types   in Category: PHP   by amit

🕙 Posted on 2023-04-28 at 21:28:57     Read in Hindi ...


Different Data Types

There are at least 10 data types in PHP.

    You can learn more about these data-types on language.types (official documentation from php.net). Some of following data-types are introduced in later versions of PHP, for example, iterable is a pseudo-type introduced in PHP 7.1.

Four scalar (primitive) types: boolean int float string - these data-types hold only one single value, though a string is sequence (series) of characters,

Four compound types: array object callable iterable - these data-types can hold more than one (or even zero) item of same/different primitive data-types and also compound data-types,

Two special types: resource null - null is a data-type, which holds nothing, and it can be used to remove values from certain variables, when necessary. resource is a special data-type, which holds value (reference) for external resource.

Two psuedo types: mixed void - these two are used only for readability purposes.

Explanation of each Data Type

Boolean

    Boolean means 'yes' or 'no' (in case of a question asked), or (in case of switching Light Bulb) 'ON' and 'OFF'! But, 'yes' and 'no' (or 'ON' and 'OFF') are not boolean values. In previous page, you have seen various reserved keywords. Therefore, you should carefully peruse the contents in this website, because when writing general english sentences, WE also use some of these reserved keywords, for example: 'and', 'or', 'if', 'finally', etc.

    TRUE true FALSE false are reserved keywords, (without a pair of single quotes or a pair of double quotes,) used for boolean values. These boolean values are case-insensitive. However, you should avoid mixture of these literal values, and always have good practice to write them in lowercase. In PYTHON programming language, only True False are only acceptable as boolean values.

    As a programmer, you have to type CODEs from your keyboard, and therefore, certain practice will speed up your scripting skills, for example, writing codes as possible in lowercase letters, commenting with double forward slashes, instead of using multiple line comments, or using less common # (hash) symbol, which requires you to press Shift key!

<?php
  echo TRUE;   echo '<br />';
  echo FALSE;   echo '<br />';
  echo true;   echo '<br />';
  echo false;   echo '<br />';
  echo '<h2>These four are boolean values, but only first and third show output as 1 </h2>';
?>

    First of all, I added the last line of code (before closing PHP tag, in above example,) to explain that third and fifth line of CODE, that is, FALSE or false will display nothing. This is not because of their values in boolean data-type, but the built-in function, echo is designed to display as such.

    Therefore, TRUE or true is converted to "1" and FALSE or false will be converted to "" , because 0 (value of an expression, and not zero integer) is treated as EMPTY STRING in PHP. Later, you will see in Conditional Expressions chapter that 0 -0 "0" 0.0 false null [] array() etc. all are treated as empty. Also, in previous page, you have also seen that following codes will not show ERROR (tested in PHP 8.2.0).

<?php
  echo TrUe;   echo '<br />';
  echo FaLsE;   echo '<br />';
  echo True;   echo '<br />';
  echo False;   echo '<br />';
  echo '<h2>These four are boolean values, but only first and third show output as 1 </h2>';
?>

Type Casting or Type Juggling

    PHP is a dynamic (loose) data-type scripting language. This means that PHP does not require explicit type definition in variable declaration. That is, when a string value is stored in a variableName, for example, $my_data = 'Good Morning'; , then the data-type of $my_data is string.

    But, afterwards when you assign a new value, for example, $my_data = 9876543210; , then the data-type of $my_data is changed from string, to newer data-type, that is, int. Or, you changed it as $my_data = true; , then the data-type of $my_data is changed from int to boolean.

    This is called type juggling and may cause BUG if you are not careful while coding! Thus, PHP developers have also provided other option to CAST one DATA-TYPE into another data-type. This is called (intentional) type-casting.

    (bool) (boolean) (int) (integer) (float) (string) (array) (object) (unset) are reserved keywords with parentheses, used to cast one data-type into another. For example, you can CAST boolean values (true and false) into other data types. You can get the data-type of a value, with gettype() function. Similarly, you can set the data-type of a value, with settype().

<?php // Till now, we have not used a variable
  $my_data = true;
  var_dump( $my_data );   echo '<br />';
  $cast_to_int = (int) $my_data;
  var_dump( $cast_to_int );
?>

    You will get the following output from above example, that is, first line is data-type of $my_data = true; and second line is data-type of $cast_to_int = (int) $my_data;

bool(true)
int(1)

    You can try other options to CAST from boolean value to another data-type. However, unset() will destroy the value of variable $my_data and an ERROR will be displayed that this variable is not defined. The (unset) cast is deprecated as of PHP 7.2.0, and removed as of PHP 8.0.0. Therefore, you should use unset() function instead of (unset) .

<?php // unset a variable
  $my_data = true;
  var_dump( $my_data );   echo '<br />';
  unset( $my_data );
  var_dump( $my_data );
?>

bool(true)

Warning: Undefined variable $my_data in C:\xampp\htdocs\php_project\phptest.php on line 5
NULL

    Some functions in PHP and other programming languages are destructive in nature, and destroys not only the value, but the variableName also. unset() is such a built-in function.


Leave a Comment: