By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,519 Members | 1,235 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,519 IT Pros & Developers. It's quick & easy.

PHP functions and arrays

P: 4
Just a quick question:
Are arrays in php pass by reference?

So if I had two functions:

Expand|Select|Wrap|Line Numbers
  1. function A($arr) { $arr[5]++; }
  2.  
  3. function B() { 
  4. $arr = array();
  5. $arr[5] = 0;
  6. A($arr);
  7. echo "The new value is: ".$arr[5];
  8. }
Should print:
The new value is: 1;

??
Jun 4 '10 #1
Share this Question
Share on Google+
2 Replies


dlite922
Expert 100+
P: 1,584
No they are not pass by reference,

The above code will out put: The new value is: 0

Thanks for playing,

Dan
Jun 4 '10 #2

paulrajj
P: 47
If u want to print the result as 1, then u should use pass by reference.

Expand|Select|Wrap|Line Numbers
  1. function A($arr) { $arr[5]++; }
  2.  
  3. function B() { 
  4. $arr = array();
  5. $arr[5] = 0;
  6. A(&$arr);  // pass by reference
  7. echo "The new value is: ".$arr[5];
  8. }
  9. B();
  10.  
  11.  
Hence the output will be 1.

PHP References
Jun 5 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.