reset and begin
8/14/2009

Variable Types in php

PHP supports eight primitive types.

Four scalar types:

  • boolean : expresses truth value, TRUE or FALSE. Any non zero values and non empty string are also counted as TRUE.
  • integer : round numbers (-5, 0, 123, 555, ...)
  • float : floating-point number or 'double' (0.9283838, 23.0, ...)
  • string : "Hello World", 'PHP and MySQL, etc


Two compound types:

  • array
  • object

And finally two special types:

  • resource ( one example is the return value of mysql_connect() function)
  • NULL

In PHP an array can have numeric key, associative key or both. The value of an array can be of any type. To create an array use the array() language construct like this.

<?php
$numbers = array(1, 2, 3, 4, 5, 6);
$age = array("mom" => 45, "pop" => 50, "bro" => 25);
$mixed = array("hello" => "World", 2 => "It's two";

echo "numbers[4] = {$numbers[4]}
";
echo "My mom's age is {$age['mom']}
";
echo "mixed['hello'] = {$mixed['hello']}
";
echo "mixed[2] = {$mixed[2'}";
?>

When working with arrays there is one function I often used. The print_r() function. Given an array this function will print the values in a format that shows keys and elements

<?php
$myarray = array(1, 2, 3, 4, 5);
$myarray[5] = array("Hi", "Hello", "Konnichiwa", "Apa Kabar");

echo '

';
print_r($myarray);
echo '
';
?>

Don't forget to print the preformatting tag

      and 
before and after calling print_r(). If you don't use them then you'll have to view the page source to see a result in correct format.

Type Juggling

In PHP you don't need to explicitly specify a type for variables. A variable's type is determined by the context in which that variable is used. That is to say, if you assign a string value to variable $var, $var becomes a string. If you then assign an integer value to $var, it becomes an integer.

An example of PHP's automatic type conversion is the addition operator '+'. If any of the operands is a float, then all operands are evaluated as floats, and the result will be a float. Otherwise, the operands will be interpreted as integers, and the result will also be an integer. Note that this does NOT change the types of the operands themselves; the only change is in how the operands are evaluated.

Example :

<?php
$myvar = "0"; // $myvar is string (ASCII 48)
$myvar += 2; // $myvar is now an integer (2)
$myvar = $foo + 1.3; // $myvar is now a float (3.3)
$myvar = 5 + "10 Piglets"; // $foo is integer (15)
?>

Type Casting

To cast a variable write the name of the desired type in parentheses before the variable which is to be cast.

<?php
$abc = 10; // $abc is an integer
$xyz = (boolean) $abc; // $xyz is a boolean

echo "abc is $abc and xyz is $xyz
";
?>

The casts allowed are:

  • (int), (integer) - cast to integer
  • (bool), (boolean) - cast to boolean
  • (float), (double), (real) - cast to float
  • (string) - cast to string
  • (array) - cast to array
  • (object) - cast to object

Bloger Comments
G+ Comments
Comments FaceBook

No comments:

List

Profiles Information


About me : Nothing is 1 vài thứ - 1985

Places I've Lived : I Hà Nội

Home Page : http://www.shimivn.blogspot.com/

Think : 1:1000000000

Languages spoken : Vietnamese,English.

Mobile : sony C2305

dell : i3-Ram 3GB- HDD 250GB .