Data Types

The PHP data types are: boolean, integer, float, string, array, object, resource, null PHP will auto-identify the data type if the author chooses not to declare the data type when creating a variable. boolean – represents either true(1) or false(0, null) $alive = true; // let php decide
$alive = (boolean) true; // explicit data type declaration
$alive = (boolean) "1"; // type cast a string to a boolean
$alive = (boolean) 1; // type cast an integer to a boolean

integer – positive/negative whole number $n = 3; // let php decide
$n = (integer) 3; // explicit data type declaration
$n = (integer) "3"; // type cast a string to an integer

float – floating point number $price = 5.99; // let php decide
$price = (float) 5.99; // explicit data type declaration
$price = (float) "5.99"; // type cast a string or integer to a float

string – any possible letters, numbers or symbols wrapped in quotes $str = "There are 12 in a dozen."; // let php decide
$str = (string) "There are 12 in a dozen."; // explicit data type declaration
$str = (string) 12; // type cast an integer to a string

array – container for compartmentalizing data $arr = array('Joe', 23 , 'USA', true); echo serialize($arr); // a:4:{i:0;s:3:"Joe";i:1;i:23;i:2;s:3:"USA";i:3;b:1;}
echo print_r($arr); // Array ( [0] => Joe [1] => 23 [2] => USA [3] => 1 ) 1
object – establishing a new object instance $myobj = new myobj();
resource – reference to an external resource or document $con = mysqli_connect(); echo get_resource_type($con);
null – value for missing, empty, or unset variables. If null… it has no value and is not set. $var1 = NULL; // Casting a variable to null will remove the variable and unset its value

Get Data Type

$n = 3; echo gettype($n); // integer
$country = "United States"; echo gettype($country); // string

Check Data Type

PHP has functions to test whether or not a variable is a certain “type”. This is known as the is_* function family. <?php // set the variable and its value
$myvar = "My phone number is 555-5555."; // Run a conditional statement to see if it is a string or not
if (is_string($myvar)) { echo "Yes"; } else { echo "No"; } ?>

Type Casting

Authors can convert data from one type to another. It is called “type casting“. An integer can be cast as a string, a string can be cast as an integer, a string or integer can be cast as a boolean, and so on. Examples are shown in the code examples above.

renewdev

I Am Developer And Want To Share My Knowledge With The World..:) ;) . I Have Another Social Networking Website (Renewchat) And For Designing Or Developing Of Website Purpose You Can Contact Me On Renew Coder .. :) ;)

3 thoughts on “Data Types

  • September 27, 2016 at 11:29 am
    Permalink

    Thanks …:) 😉

    Reply
  • September 27, 2016 at 12:40 pm
    Permalink

    Wow cuz this is great work! Congrats and keep it up

    Reply
  • September 27, 2016 at 12:45 pm
    Permalink

    Thanks..:) 🙂 Any Suggestion For Another Good Stuff..:) 😉

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *