Friday, February 10, 2012

Different Data Types in PHP

PHP supports wide range of Data Types. It supports eight different primitive data types: four scalar types (boolean, integer, float, string), two command types: (array and object) and finally two special types (resource, NULL). All these data types are discussed below:
Boolean:
Boolean is data type whose value is either true or false. True and false in PHP are case insensitive. To specify a boolean literal, use the keywords TRUE or FALSE. For example:
<?php
    $bool1 = true; //assign the value true to $bool1
    $bool2 = false; //assign the value false to $bool2
    if($bool1){
        echo "It is true";
    }else{
        echo "It is not true";
    }
?> 

Integer:
An integer is number of the set I = {…, –3, –2, –1, 0, 1, 2, 3 ,…….} .Integer can be specified in Decimal, Hexadecimal, Octal or Binary. For example:
<?php
    $a = 1234; // decimal number
    $a = -123; // a negative number
    $a = 0123; // octal number (equivalent to 83 decimal)
    $a = 0x1A; // hexadecimal number (equivalent to 26 decimal)
?>

<?php
    $a = 1234; // decimal number
    $a = -123; // a negative number
    $a = 0123; // octal number (equivalent to 83 decimal)
    $a = 0x1A; // hexadecimal number (equivalent to 26 decimal)
?> 

Float:

Floating points numbers are real numbers. There are infinite floating point numbers between any two integer. In PHP, there are different ways to represent floating point numbers.
<?php
    $a = 1.234; 
    $b = 1.2e3; 
    $c = 7E-10;
?> 

 

String:

string in PHP is stream of characters. A statement that is enclosed by single (‘’) or double quote (“ “) is regarded as string. There are various operations on string in PHP. Some are
<?php
    $firstString = "The quick brown fox";
    $secondString = " jumped over the lazy dog";
    $thirdString = $firstString; //assign value of firstString to thirdString
    $thirdString .= $secondString; //concatenate
?>
    Lower case: <?php echo strtolower($thirdString); ?></br>
    Upper case: <?php echo strtoupper($thirdString); ?></br>
    Lowercase first-letter: <?php echo ucfirst($thirdString); ?></br>
    Uppercase Words: <?php echo ucwords($thirdString); ?></br>
    Length: <?php echo strlen($thirdString); ?></br>
    Trim: <?php echo trim($thirdString); ?></br> 

No comments:

Post a Comment