Wednesday, July 3, 2013

Regarding Global Variable

What is the output of the following?

<?php

$a = 100;

function myfunction($b) {
 $a = 200;
 
 global $a;
 return $a;
}

print $a;

The output is 100. Because, the variable $a is a global variable. The global declaration within the function tells the function to use the global value, rather than the local value.

Monday, July 1, 2013

The ways to iterate and modify every element of an array using PHP 5

for($i = 0; $i < count($array); $i++) { /* … */ }
If the array is a numeric array with the keys from 0 to n-1, then it’s a useful way. It only works if all of the keys in the array are numeric.

for($i = 0; $i < count($array); $i++) {
   print $array[$i]; // access
   $array[$i] = “new value $i”; //modify
}

foreach($array as $val) { /* … */ }
It’s useful for both kinds of arrays. But in this way, the value could be accessed only.

foreach($array as $val) {
   print $val; // access
}

foreach($array as $key => &$val) { /* … */ }
PHP5 introudces the possibility of modifying the contents of the array directly by assigning the value of each element to the iterated variable by reference rather than by value. Here, the $val is a reference of the element’s value. So changing $value inside foreach will also change the original value.

foreach($array as $key => &$val) {
   print $val; //access
   $val = “new value $key”; //modify
}

foreach($array as $key => $val) { /* … */ }
Here, $val is a copy of the value. foreach operates on a copy of $array. With the key of the element the value could be accessed and the original value could be changed with $array[$key].

foreach($array as $key => $val) {
   print $val; //access
   print $array[$key]; //access
   $array[$key] = “new value $key”; //modify
}

while(list($key, $val) = each($array)) { /* … */}
Use $array[$key] to access and change the original element.

while(list($key, $val) = each($array)) {
   print $val; //access
   $array[$key] = “new value $key”; //modify
}

Details with example:
//using for($i = 0; $i < count($array); $i++) { /* … */ }

$array = array(‘foo’, ‘bar’);

for($i = 0; $i < count($array); $i++) {
   print $array[$i]; // access
   $array[$i] = “new value $i”; //modify
}

var_dump($array);

//using foreach($array as $val) { /* … */ }

$array = array(‘foo’, ‘bar’);

foreach($array as $val) {
  print $val; // access
}

var_dump($array);

//using foreach($array as $key => &$val) { /* … */ }

$array = array(‘foo’, ‘bar’);

foreach($array as $key => &$val) {
  print $val; //access
  $val = “new value $key”; //modify
}

var_dump($array);

//using foreach($array as $key => $val) { /* … */ }

$array = array(‘foo’, ‘bar’);

foreach($array as $key => $val) {
  print $val; //access
  print $array[$key]; //access
  $array[$key] = “new value $key”; //modify
}

var_dump($array);

//using while(list($key, $val) = each($array)) { /* … */}

$array = array(‘foo’, ‘bar’);

while(list($key, $val) = each($array)) {
  print $val;
  $array[$key] = “new value $key”; //modify
}

var_dump($array);

Internal array pointer to accomplish an action – example of some array finctions

The following functions are used with the internal array pointer to accomplish an action:
key  – Fetch a key from an array
prev  – Rewind the internal array pointer
current  – Return the current element in an array
next  – Advance the internal array pointer of an array
end  – Set the internal pointer of an array to its last element
reset  – Set the internal pointer of an array to its first element
each  -Return the current key and value pair from an array and advance the array cursor