Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8917867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:35:58+00:00 2026-06-15T05:35:58+00:00

If a function my_func is defined as: function my_func(&$arr) { array_push($arr, 0); array_push($arr, 1);

  • 0

If a function my_func is defined as:

function my_func(&$arr) {
    array_push($arr, 0);
    array_push($arr, 1);
    array_push($arr, 2);
    array_push($arr, 3);
    array_push($arr, 4);
}

If I call my_func as follows, then the results are as expected:

$test_array = array();
my_func($test_array);
print_r($test_array);

Results in:

Array
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 4
)

I was thinking that I could shorten this code somewhat by calling my_func as follows:

my_func($test_array = array());
print_r($test_array);

However, the result changes:

Array
(
)

Why does this shorter code snippet lead to a different result?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:35:59+00:00Added an answer on June 15, 2026 at 5:35 am

    You’re not passing the variable by reference, but the value of an expression.

    If you were to enable all errors, you would see the following message:

    Strict Standards: Only variables should be passed by reference in 
    

    The value of an assignment is the value that is being assigned itself. It is that value—a copy of the array—that gets passed to my_func.

    The PHP documentation on the matter puts this more strongly:

    No other expressions should be passed by reference, as the result is undefined.

    Conclusion: Only ever pass variables ($x, $x[0], $x->a), new objects or references returned from functions to a function expecting a reference.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do you declare a function defined as such: void func(cv::Mat &img) { ...
function my_func(a, b){ if(typeof(a) !== 'undefined'){ // do stuff } return b; } How
Unless I'm mistaken, creating a function in Python works like this: def my_func(param1, param2):
The C function myfunc operates on a larger chunk of data. The results are
I try to write a functor to call a boost function with bind and
Suppose I have this function: void my_test() { A a1 = A_factory_func(); A a2(A_factory_func());
var func = function() { $(this).hide(); $parent = $(this).parent(); $parent.removeClass('my_signature_mouseover'); var text = $(this).val();
Via Function: myfunc Parameters: a - First parameter b - First parameter I can
I have a function - myFunc() in class A. There are multiple other classes
For example the following code prints { key: 'b' } function myFunc(key, value) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.