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 6543017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:17:45+00:00 2026-05-25T11:17:45+00:00

In php 5.3, when you create an anonymous function, can you set the default

  • 0

In php 5.3, when you create an anonymous function, can you set the default values?

Like in a normal functon you do

function tim($a=123){

}

where 123 is the default value for $a. What abut in anonymous functions?

UPDATE

I’m having trouble with it in this context:

//$data is an object;
$data->title = 'test';
add_filter('title',function($current, $new = $data->title ){ return $new; });

produces “unexpected T_VARIABLE”

works fine without the $data->title bit, but I really want to pass this in…

add_filter('title',function($current, $new = 'some-title' ){ return $new; });

I’m adding a filter in WordPress. Works fine if I explicitly set it, but I want to pull it from another variable. Is that possible?

  • 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-25T11:17:45+00:00Added an answer on May 25, 2026 at 11:17 am
    $ php -r '$foo = function($a = 123){echo $a, PHP_EOL;};$foo(1);$foo();'
    1
    123
    

    So that’s a yes

    Update

    You can only assign simple values to argument defaults. From the manual

    The default value must be a constant expression, not (for example) a variable, a class member or a function call.

    Try passing the external variable via the use keyword

    add_filter('title', function($current, $new = null) use ($data) {
        if (null === $new) {
            $new = $data->title;
        }
        return $new;
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In PHP you can create form elements with names like: category[1] category[2] or even
A php closure or anonymous function is used to create function without specifying its
Is there any thing in PHP to create basic scaffold, like in Rails? EDIT:
We use the ExpressionEngine CMS (php) to create websites. For each site, we set
Possible Duplicate: PHP : Create array for JSON I have an array like this
Heres a simple php script that generates some XML: <?php // create doctype $dom
Our PHP scripts create dynamic folders and files and we are having a permission
I am using PDO with PHP to create a new database and then a
I have a script that parse the database and create php classes to work
I found the following code to create a tinyurl.com url: http://tinyurl.com/api-create.php?url=http://myurl.com This will automatically

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.