Developers Archive for the 'php5 programming' Category

array_combine

array_combine Monday, March 19th, 2007

array_combine

array_combine

- It creates an array by using one array for keys and another for its values

Syntax:

array array_combine ( array keys, array values )

It returns an array by using the values from the keys array as keys and the values from the values array as the

corresponding values.

It returns FALSE if the number of elements for each array isn’t equal or if the arrays are empty.

Example:

$a = array(’one’, ‘two’, ‘three’);
$b = array(’1′, ‘2′, ‘3′);
$c = array_combine($a, $b);

print_r($c);

The content in the array $c is

Array
(
[one]  => 1
[two]    => 2
[three] => 3
)

Disadvantages of global variables

Disadvantages of global variables Tuesday, March 6th, 2007

The following example demonstrates use of the global keyword:

<?php
$my_var = ‘Hello World’;test_global();

function test_global() {
// Now in local scope
// the $my_var variable doesn’t exist

// Produces error: “Undefined variable: my_var”
echo $my_var;

// Now let’s important the variable
global $my_var;

// Works:
echo $my_var;
}

?>

As you can see in the above example, the global keyword is used to important variables from the global scope. Seems to work fine, and it’s nice and simple, so why should you worry about using the global keyword?

There are three good reasons:

1. Reusing parts of the script is impossible
If a certain function relies on global variables, it becomes almost impossible to use that function in a different context. Another problem is that you can’t take that function, and use it in another script.

2. Solving bugs is much harder
Tracking a global variable is much harder than a non-global variable. A global variable could be declared in some obscure include file, which could take hours to find, although a good text editor / IDE could help with this.

3. Understanding the code in a year will be much more difficult
Globals make it difficult to see where a variable is coming from and what it does. You might know about every global during development, but after a year or so you’ll probably have forgotten at least half of them, and then you’ll be kicking yourself for using so many globals

Overloading the Array Access Syntax

Overloading the Array Access Syntax Tuesday, February 20th, 2007

1. To overload array access syntax we have to implement ArrayAccess interface. That interface has four abstract function, so we should have implement all four functions.Those functions are
1. offsetExist - This function is used to check whether offset exists
2. offsetGet - This function is used to get the offset value
3. offsetSet - This function is used to set the offset value
4. offsetUnset - This function is used to unset the offset value

Example:

class Person implements ArrayAccess{
private $name;
private $id;

function offsetExists($name) {
;
}

function offsetGet($id) {

if($id == $this->id)
return $this->name;
else
return “not found”;
}

function offsetSet($name, $id) {
;
}
function offsetUnset($name) {
;
}

}

$objPerson = new Person();
$objPerson->offsetSet(”test”,”12″);

print “Person’s id is ” . $objPerson[”12″];

Above code is not a typical example for overloading array access syntax. Just implemented offsetGet() function whic will return person’s name.

In the above code
print “Person’s id is ” . $objPerson[”12″]; this will print the test.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.