Hello friends, this is my 7th tutorial of PHP and Today I am sharing the concept of using pointers in PHP. Pointers always come handy while manipulating data in a programming language. In this blog post I will share a simple example of using pointers in PHP.
In the first example I will explain how you can use pointers to access array elements using pointers :
 <?php  
$age=array(4,56,76,34,54);
echo "1 . " . current($age); // Points to the first element in the array.
next($age);
echo "2 . " . current($age); // Points to the second element in the array
reset($age);
echo "3 . " . current($age); //Pointer is reset and points to the first element again.
?>
As you can see in the above example, I have used three different methods named current(), next() and reset() for accessing the array elements.
Let us consider another example where we will try to access all the elements in an array by using the above methods and a while loop :
 <?php  
while($var=current($age)) {
 echo $var;
 next($age);
}
?>

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.