Saturday, September 29, 2007

PHP learning--part 4

PHP Types:

About the primitive types, PHP has 4 primitive type:
+ boolean
+ integer
+ float
+ string

Two compound types:
+ array
An array in PHP is actually an ordered map. A map is a type that maps values to keys. This type is optimized in several ways, so you can use it as a real array, or a list (vector), hashtable (which is an implementation of a map), dictionary, collection, stack, queue and probably more. Because you can have another PHP array as a value, you can also quite easily simulate trees.
Note: It is so powerful, but complex, not easy to understand.

Example1: 1-dimension array ( or map)
<?php
$arr
= array("foo" => "bar", 12 => true);
echo
$arr["foo"]; // bar
echo $arr[12]; // 1
?>

Example 2: 2 dimension array

<?php
$arr
= array("somearray" => array(6 => 5, 13 => 9, "a" => 42));
echo
$arr["somearray"][6]; // 5
echo $arr["somearray"][13]; // 9
echo $arr["somearray"]["a"]; // 42
?>

Array do's and don'ts

Why is $foo[bar] wrong?

If there are no constant define bar, PHP will automatically change bar into 'bar' and use that.
You will mismatch value define.


error_reporting
(E_ALL);
ini_set('display_errors', true);
ini_set('html_errors', false);
// Simple array:
$array = array(1, 2);
$count = count($array);
for (
$i = 0; $i < $count; $i++) {
echo
"\nChecking $i: \n";
echo
"Bad: " . $array['$i'] . "\n";
echo
"Good: " . $array[$i] . "\n";
echo
"Bad: {$array['$i']}\n";
echo
"Good: {$array[$i]}\n";
}
?>

The above example will output:

Checking 0:  Notice: Undefined index:  $i in /path/to/script.html on line 9 Bad:  Good: 1 Notice: Undefined index:  $i in /path/to/script.html on line 11 Bad:  Good: 1  Checking 1:  Notice: Undefined index:  $i in /path/to/script.html on line 9 Bad:  Good: 2 Notice: Undefined index:  $i in /path/to/script.html on line 11 Bad:  Good: 2 So array is flexible, but using it carefully.

+ Object

Object Initialization
<?php
class foo
{
function
do_foo()
{
echo
"Doing foo.";
}
}

$bar = new foo;
$bar->do_foo();
?>

Converting to object:

Rule: If an object is converted to an object, it is not modified. If a value of any other type is converted to an object, a new instance of the stdClass built in class is created. If the value was NULL, the new instance will be empty. Array converts to an object with properties named by array keys and with corresponding values. For any other value, a member variable named scalar will contain the value.

Example:

Convert string to object.

<?php
$obj
= (object) 'ciao';
echo
$obj->scalar; // outputs 'ciao'
?>

Resource

A resource is a special variable, holding a reference to an external resource. Resources are created and used by special functions


Converting to resource

As resource types hold special handlers to opened files, database connections, image canvas areas and the like, you cannot convert any value to a resource.

NULL

The special NULL value represents that a variable has no value

The type of a variable is usually not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which that variable is used.-->Varible type in PHP equal type variant in many other programming language.

That's all. We will go to detail in next day.

No comments:

Google