Sunday, 9 June 2013

BOOLEAN DATA TYPES AND SOME RELATED FUNCTIONS IN PHP

Boolean data types are the logical data types that are denoted by 'true' or 'false' in php.

Consider the following simple program that prints two Boolean variables.
/********************************************************************************/
 Source code # 1
<html>
                <head>
                                <title>Booleans</title>
                </head>
                <body>
                                <?php
                                                $var1=true;
                                                $var2=false;
                                                echo "true = ".$var1."<br />";
                                                echo "false = ".$var2."<br />";
                                ?>
                </body>

</html>
/********************************************************************************/
Output:-
true = 1
false = 

Note that a false value is not printed in the browser.


Now let's consider some of the boolean functions that determines whether a variable is set to any value or not.
Source Code # 2
/********************************************************************************/
<html>
                <head>
                                <title>Boolean Functions</title>
                </head>
                <body>
                                <?php
                                                $var1=5;
                                                /* if a value is assigned then isset() returns 1 otherwise 0 */
                                                echo "isset var1 = ".isset($var1)."<br />";
                                                echo "isset var2 = ".isset($var2)."<br />";
                                               
                                                unset($var1);
                                                echo "isset var1 after unsetting var 1 = ".isset($var1)."<br />";
                                               
                                                $var1=45;
                                                echo "is var1 empty = ".empty($var1)."<br />";
                                                echo "is var2 empty= ".empty($var2)."<br />";

                                               
                                ?>
                </body>
</html>
/*******************************************************************************/
Output:-
isset var1 = 1
isset var2 =
isset var1 after unsetting var 1 =
is var1 empty =
is var2 empty= 1

No comments:

Post a Comment