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

  • Home
  • SEARCH
  • 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 1008589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:47:42+00:00 2026-05-16T08:47:42+00:00

I’ve tested the following and it works on both PHP 5.2 and 5.3, however,

  • 0

I’ve tested the following and it works on both PHP 5.2 and 5.3, however, it’s not documented anywhere as far as I can see so I’m evaluating its use.

I have a function in a class called isValid, this checks a hash to see if the given value is in the set of allowed values. There are some values that are valid, but deprecated; I’d like my isValid function to update the passed in value to the current one and return true.

That’s fine for when I call it myself, however, I’d like to use this method when used as a callback for array_filter too.

Here’s a test case, which as expected results in an array with the values 2,3,4,5,6.

<?php
$test = array(1, 2, 3, 4, 5);
echo print_r(array_filter($test, 'maptest'), true);

function maptest(&$value)
{
$value ++;
return true;
}

So StackOverflow: is this allowed, or is it undocumented functionality that may disappear/stop working/cause errors in the future?

  • 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-05-16T08:47:43+00:00Added an answer on May 16, 2026 at 8:47 am

    Yes, it’s allowed.

    In this respect, there’s nothing special about calling functions through callbacks.

    However, your specific example does not illustrate one difficulty. Consider:

    function inc(&$i) { $i++; }
    $n = 0;
    // Warning:  Parameter 1 to inc() expected to be a reference, value given:
    call_user_func('inc', $n);
    

    The problem is that you’re passing $n to call_user_func and call_user_func doesn’t accept values by reference. So by the time inc is called, it won’t receive a reference. This isn’t a problem with array_filter because it traverses the array directly and can directly pass the variables in the array to the callback function.

    You could use call-time pass-by-reference, but this is deprecated and removed from trunk:

    function inc(&$i) { $i++; }
    $n = 0;
    // Deprecated: Call-time pass-by-reference has been deprecated
    call_user_func('inc', &$n);
    

    So the best option is to use call_user_func_array instead:

    function inc(&$i) { $i++; }
    $n = 0;
    call_user_func_array('inc', array(&$n));
    

    This function will pass-by-reference the elements that have the is_ref flag set and will pass-by-value the other ones.

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

Sidebar

Related Questions

No related questions found

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.