Josee Posted May 16, 2011 Share Posted May 16, 2011 Hello, I'm so little advanced that I got stuck right at the beginning of chapter 1. On page 3, Larry says: "By printing out the values of $x[‘key1’] and $y[‘key1’], one can see how the user-defined sorting function is invoked." So that's what I'm trying to do. I got this far: function mysort1 ($x, $y) { echo "<p>Iteration: ".$x['key1']." vs ".$y['key1']."</p>"; return ($x['key1'] > $y['key1']); } and the result is as follows Iteration: 23 vs 940 Iteration: 894 vs 23 Iteration: 940 vs 23 Iteration: 894 vs 940 but how do you number the iterations so as to obtain the same result as in the book? Iteration 1: 23 vs 940 Iteration 2: 894 vs 23 Iteration 3: 940 vs 23 Iteration 4: 894 vs 940 I tried this: $n = 0; echo "<p>Iteration ".$n++.": ".$x['key1']." vs ".$y['key1']."</p>"; but all iterations get numbered "0". Obviously, there's something fundamental I don't understand about functions and would be very grateful if someone can enlighten me! Also: I went to read the "usort" page in the PHP manual and saw this: function array_sort_by_subval(&$array, $key) { foreach($array as &$v) { $v['__________'] = $v[$key]; } usort($array, 'sort_by_underscores'); foreach($array as &$v) { unset($v['__________']); } } What does "&" in front of a variable name mean? Or what does it do? With thanks for your help, Link to comment Share on other sites More sharing options...
Jonathon Posted May 16, 2011 Share Posted May 16, 2011 Hi Josee, I don't think I can help you with all of your questions, but I think the count number can be achieved by using a static variable. function() { static $n = 1; echo $n; } I think , please let me know how you get on. 1 Link to comment Share on other sites More sharing options...
Josee Posted May 16, 2011 Author Share Posted May 16, 2011 Thank you very much, Jonathon: "static" did it! One more subject to add to the list of all the things I still have to learn… Link to comment Share on other sites More sharing options...
Jonathon Posted May 16, 2011 Share Posted May 16, 2011 That's ok Josee. I'm going to get the book out in a bit and have a look over that section. See if I can figure the rest of your question out. Link to comment Share on other sites More sharing options...
Josee Posted May 16, 2011 Author Share Posted May 16, 2011 Thank you, Jonathon. But I just found the explanation for the & sign in the comments to the PHP manual (http://us2.php.net/manual/en/function.reset.php). One user said: "I had a problem with PHP 5.0.5 somehow resetting a sub-array of an array with no apparent reason. The problem was in doing a foreach() on the parent array PHP was making a copy of the subarrays and in doing so it was resetting the internal pointers of the original array." And another answered: <? // ... foreach($a as $k => &$d){} // notice the "&" // ... ?> It`s a new feature in PHP5 to use references in foreach loop. This way PHP isn`t making a copy of the array, so the internal pointer won`t be reset. 1 Link to comment Share on other sites More sharing options...
Antonio Conte Posted May 16, 2011 Share Posted May 16, 2011 $var = 0; function name( &$var ) { $var = 15; } echo $var; // this is now 15 if you use the function [i]name[/i] This means the variable is passed by reference. - You play with the ACUTALL variable - Normally you get A COPY / NEW variable with the same name The difference is that any thing you do with the variable passed by refference, is kept with the original variable. Edit: Too late... 1 Link to comment Share on other sites More sharing options...
Josee Posted May 17, 2011 Author Share Posted May 17, 2011 Thank you, Antonio Conte. Link to comment Share on other sites More sharing options...
Josee Posted May 17, 2011 Author Share Posted May 17, 2011 I was still a bit confused about variables passed by reference (using & before a variable name) and found this page from the PHP manual clarified things for me: http://us2.php.net/m...s.arguments.php Making arguments be passed by referenceBy default, function arguments are passed by value (so that if the value of the argument within the function is changed, it does not get changed outside of the function). To allow a function to modify its arguments, they must be passed by reference. To have an argument to a function always passed by reference, prepend an ampersand (&) to the argument name in the function definition: Example #2 Passing function parameters by reference <?php function add_some_extra(&$string) { $string .= 'and something extra.'; } $str = 'This is a string, '; add_some_extra($str); echo $str; // outputs 'This is a string, and something extra.' ?> I hope this helps someone else! 1 Link to comment Share on other sites More sharing options...
Antonio Conte Posted May 17, 2011 Share Posted May 17, 2011 I got this explained really really good by watching a Java online course from Stanford University. Passed by value: When you work with a variables, you get the value from the memory location of the variable inside the computer. The original variable is not known or important, just it's value. Passed by reference: Because objects and arrays are much larger amounts of data, you get a pointer to the memory locations (plural) of the actuall array/object. Think of it like this: Little Jimmy goes to Louvre to watch Mona Lisa. He wants to use his Chainsaw to cut Mona Lisa in half. When you pass by value, Little Jimmy goes to the gift shop instead, and gets A COPY to play with. It looks the same, but the original is safely stored in Louvre (And your computer) When you pass by reference, Little Jimmy plays with the REAL MONA LISA. Any changes he does to it, will affect the original! (And also the original on your computer) 1 Link to comment Share on other sites More sharing options...
Jonathon Posted May 17, 2011 Share Posted May 17, 2011 Thanks to both of you for going the extra mile to try and explain it, because I didn't know of the top of my head. Admittedly I havent re-checked the book or any sources. I must have just blazed past it as I read the book as it was early on and I just wanted to read too much. I understand (I think now). Sometimes though I find with some examples that although I understand the logic. I don't really understand a real world example of where I'd use it? Would anyone know of any? Link to comment Share on other sites More sharing options...
Larry Posted May 18, 2011 Share Posted May 18, 2011 In my daily programming, I don't pass variables by reference often. I normally think of it as being more useful in OOP (where sometimes you need to work with a complex object) and sometimes I've used it when I'm being lazy. But as a concrete example, if you have an array that needs to be manipulated by a function, it's easier to pass that array by reference so that you don't have to return the array. The array_walk() function does this: http://us2.php.net/manual/en/function.array-walk.php If you look at the first argument in the function definition, you'll see the array to be walked over is received by array_walk() by reference. Passing by reference can also have a performance benefit for very large sets of data, as passing by reference doesn't require the creation of a second data set (i.e., a doubling of the memory required). Link to comment Share on other sites More sharing options...
Antonio Conte Posted May 18, 2011 Share Posted May 18, 2011 (...) If you have an array that needs to be manipulated by a function, it's easier to pass that array by reference so that you don't have to return the array. The array_walk() function does this: http://us2.php.net/m....array-walk.php If you look at the first argument in the function definition, you'll see the array to be walked over is received by array_walk() by reference. What would be preferable: - Using array_walk to pass the array as reference to a function? - Passing the array straight to the function as reference? array_walk() does not seem to difficult to use, but it does add a layer of complexity. Are both these methods listed possible solutions, or do you NEED to use the array_walk()? Example: <?php $fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple"); function test_print($item2, $key) { echo "$key. $item2<br />\n"; } array_walk($fruits, 'test_print'); /*----------- Straight to function -----------*/ self::my_function($fruits) private function my_function(%$array) { foreach ($array as $key => $item) { echo "$key. $item<br />\n"; } } ?> Link to comment Share on other sites More sharing options...
Larry Posted May 19, 2011 Share Posted May 19, 2011 My inclination is to say that if you need the functionality that array_walk() provides, you should use it. If you don't need that functionality, you should not use it. 1 Link to comment Share on other sites More sharing options...
Recommended Posts