Jump to content

Recommended Posts

I'm afraid that I'm very much the beginner where PHP is concerned but I'm slowly working my was through a "Teach Yourself ..." book and have grasped the basics of variables, loops and functions and an now learning about arrays. I hope that it's ok to ask beginner's questions here.

 

I wrote a very simple script to set up an array, populate it with a few string values and use a For loop to print out the values. I then decided to be clever and write a function to print out the array elements, passing the array name and iteration value as arguments with the For loop; it actually worked. The function itself was defined:-

 

function printelement($array_to_print,$pointer) {

From my limited understanding of PHP I assume that when I pass an array to  a function as an argument, a local copy is made within the function. In order to test this out I added another element to the array from within the function and printed it out from outside the function; as expected the new element was empty. I then modified the function to include the & symbol which I belive passes a pointer to a variable, rather than the variable itself and allows the actual variable to be manipulated from within a function.

 

function printelement(&$array_to_print,$pointer) {

However the element added within the function remained empty when printed from outside of the function.

 

Have I got the idea of pointers & wrong or do they work differently with arrays? If I make the passed array global everything works as I'd expect, which brings me to my second beginner's question, which is what actually is the difference between passing a point to a variable and making a variable global?

 

I'm sure I'll get the hang of things eventually but it all seems a bit daunting at the moment.

Link to comment
https://forums.phpfreaks.com/topic/193699-passing-pointers-to-arrays-beginner/
Share on other sites

I feel somewhat embarassed posting my code on a PHP forum as I'm sure it's pretty shonky and really just an attempt to clarify things in my mind.

 

It's just a simple piece of code to explore arrays try and understand things rather than just go with the examples in the book. The program creates an array - $array1 - and populates it with some string values. Then it's just a case of zipping around a loop calling the printelement() function and passing it the name of the array and the element to print out. That worked fine. I then decided to explore pointers and addeded the line $array1[6] = "element7"; to the function, to add a new element to the end of the array. It's a fairly pointless piece of code as it write the same value to the array each time the function is call but bear with me. After finishing the loop the program simply echoes the last element of the array add in the function.

 

When I just passed the array name the added element of the array ($array1[6]) was blank, which made sense as I assume that a local copy of $array1 was made for the function and discarded when exiting the function. I then called the array using a pointer &$array1, assuming (based on what I understood about pointers) that this would point to the actual array and therefore $array1[6] would be intact to be echoed at the end of the program, however it remained a blank string. When I declared $array1 as global within the function $array1[6] was intact and could be echoed as expected.

 

Essentially I was just wondering whether I'd mis-understood the idea of pointers - I thought that they pointed to the actual variable which was then manipulated rather than a copy local to the function.

 

Thanks for the pointers to the PHP manual, I have explored there before but find it somewhat complex bewildering at this early stage.

 

My code is:-

 

<?php

function printelement(&$array_to_print,$pointer) {
echo "\$array1[$pointer]=<font color=red>$array_to_print[$pointer]</font><BR />";
$array1[6] = "element7";
return;
}

$array1 = Array("element1","element2","element3","element4","element5");
$array1[]="element6";
For ($iterations = 0;$iterations <sizeof($array1);$iterations++) {
printelement($array1,$iterations);
}
echo "\$array1[6] = $array1[6] <BR />";
?>

Ah yes, thank you. I see what I've done wrong, the pointer to $array1 was passed to the function, but is stored in $array_to_print, $array1 is still outside the scope of the function unless I declare it global!

 

I can see that I'm going to have to concentrate on variables. I'm sure that  PHP's way of simply treating non-existance variable or variables outside of the scope of a function as null, rather than raising an error is likely to trip me up again I suspect.

 

Thanks again.

Ah yes, thank you. I see what I've done wrong, the pointer to $array1 was passed to the function, but is stored in $array_to_print, $array1 is still outside the scope of the function unless I declare it global!

 

Don't use global.  It's a programming crutch that leads to easily avoidable errors.  Learn how to do things the right way.  PHP isn't all that different from other languages when it comes to variable scope and argument passing.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.