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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:21:01+00:00 2026-05-16T18:21:01+00:00

How do I pass a variable to create_function() ? I have something like this:

  • 0

How do I pass a variable to create_function() ?

I have something like this:

function my_hook_function(){
  $var = 10;
  apply_filters('hook', $var);
  return $var;
}


$variable = 5;
$function = create_function('', 'return $variable;');

add_filter('hook', $function); 

echo my_hook_function();

but it doesn’t work 🙁
theoretically the output should be 5

add_filter() is a wordpress function that allows you to change stuff around 🙂

  • 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-16T18:21:02+00:00Added an answer on May 16, 2026 at 6:21 pm

    There are two problems here. The first one is that you need to tell php what parameters get passed to the ‘created’ function, or reference them as a global inside of the body. The second is that you are expecting $var to be modified by the created function, but you are not passing it a reference. the created function simply returns the new variable and you do nothing with it.

    function my_hook_function(){
      $var = 10;
      $var = apply_filters('hook', $var);
      return $var;
    }
    
    /* This will return 5 by dividing the passed value by 2 and returning the result */
    $function = create_function('$variable', 'return $variable/2;');    
    add_filter('hook', $function);
    echo my_hook_function();
    
    /* This will return 5 by referencing the global $variable */
    $variable = 5;
    $function = create_function('', 'global $variable; return $variable;');
    add_filter('hook', $function);
    echo my_hook_function();
    

    Note that if you run this code exactly like this that both of the filters will be added to the ‘hook’ hook.

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

Sidebar

Ask A Question

Stats

  • Questions 537k
  • Answers 537k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Select aa, bb from a, b where a.aa like '%'… May 17, 2026 at 1:49 am
  • Editorial Team
    Editorial Team added an answer You can call FindFirstFile. Here is a sample I just… May 17, 2026 at 1:49 am
  • Editorial Team
    Editorial Team added an answer Yes, it is. You can use multiple case labels for… May 17, 2026 at 1:49 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Is there a method to pass a variable number of arguments to a function
I have a set of numbers that I need to pass from a function
Say I have a class which represents a person, a variable within that class
Suppose I have a C++ class like so: class A { public: A() {
I'm trying to create a function in C# which will allow me to, when
I'm doing a simple ajax query which retrieves a variable-length list of values as
So, I'm writing something and I've come into a roadblock on how to do
So, i have the following script to use the google maps API, its all
This is issue about LANGUAGE DESIGN. Please do not answer to the question until
I am trying to create a user login/creation script in PHP and would like

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.