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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:46:26+00:00 2026-05-29T11:46:26+00:00

I am confused about default values for PHP functions. Say I have a function

  • 0

I am confused about default values for PHP functions. Say I have a function like this:

function foo($blah, $x = "some value", $y = "some other value") {
    // code here!
}

What if I want to use the default argument for $x and set a different argument for $y?

I have been experimenting with different ways and I am just getting more confused. For example, I tried these two:

foo("blah", null, "test");
foo("blah", "", "test");

But both of those do not result in a proper default argument for $x. I have also tried to set it by variable name.

foo("blah", $x, $y = "test");   

I fully expected something like this to work. But it doesn’t work as I expected at all. It seems like no matter what I do, I am going to have to end up typing in the default arguments anyway, every time I invoke the function. And I must be missing something obvious.

  • 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-29T11:46:27+00:00Added an answer on May 29, 2026 at 11:46 am

    I would propose changing the function declaration as follows so you can do what you want:

    function foo($blah, $x = null, $y = null) {
        if (null === $x) {
            $x = "some value";
        }
    
        if (null === $y) {
            $y = "some other value";
        }
    
        code here!
    
    }
    

    This way, you can make a call like foo('blah', null, 'non-default y value'); and have it work as you want, where the second parameter $x still gets its default value.

    With this method, passing a null value means you want the default value for one parameter when you want to override the default value for a parameter that comes after it.

    As stated in other answers,

    default parameters only work as the last arguments to the function.
    If you want to declare the default values in the function definition,
    there is no way to omit one parameter and override one following it.

    If I have a method that can accept varying numbers of parameters, and parameters of varying types, I often declare the function similar to the answer shown by Ryan P.

    Here is another example (this doesn’t answer your question, but is hopefully informative:

    public function __construct($params = null)
    {
        if ($params instanceof SOMETHING) {
            // single parameter, of object type SOMETHING
        } elseif (is_string($params)) {
            // single argument given as string
        } elseif (is_array($params)) {
            // params could be an array of properties like array('x' => 'x1', 'y' => 'y1')
        } elseif (func_num_args() == 3) {
            $args = func_get_args();
    
            // 3 parameters passed
        } elseif (func_num_args() == 5) {
            $args = func_get_args();
            // 5 parameters passed
        } else {
            throw new \InvalidArgumentException("Could not figure out parameters!");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm confused about the Emacs indentation paradigm. I have this in my .emacs file:
I'm relatively confused about this... I've got a table like: +----------------+--------------------------------------------------+------+-----+-------------------+-----------------------------+ | Field |
I am bit confused about how Java handle conversion. I have char array consist
I'm a little confused about the default behaviour of Equals and GetHashCode in C#.
As an Objective-C beginner, I'm very confused about the init function, and how and
I'm a little confused about how EGit workes. I have an existing git repository
Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument I'm very confused about
I'm confused about how to use set_error_handler() properly, and the php documentation isn't really
I am going through this tutorial and I am a bit confused of about
Am a little confused about how subversion model works. So say me and my

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.