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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:24:47+00:00 2026-06-17T14:24:47+00:00

Is it possible to use an array as a default parameter in a PHP

  • 0

Is it possible to use an array as a default parameter in a PHP function?

I would like to do the following:

$arr = array(1,2,3,4);

function sample($var1, $var2, $var3 = $arr){
    echo $var1.$var2;
    print_r($var3);
}

sample('a','b');
// Should still work without the third parameter and default to $arr
  • 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-17T14:24:49+00:00Added an answer on June 17, 2026 at 2:24 pm

    No, this is not possible, the right hand expression of the default value must be a constant or array literal, i.e.

    function sample($var1, $var2, $var3 = array(1, 2, 3, 4))
    {
    }
    

    If you want this behaviour, you could use a closure:

    $arr = array(1, 2, 3, 4);
    
    $sample = function ($var1, $var2, array $var3 = null) use ($arr) {
        if (is_null($var3)) {
            $var3 = $arr;
        }
    
        // your code
    }
    
    $sample('a', 'b');
    

    You could also express it with a class:

    class Foo
    {
        private static $arr = array(1, 2, 3, 4);
    
        public static function bar($var1, $var2, array $var3 = null)
        {
            if (is_null($var3)) {
                $var3 = self::$arr;
            }
    
            // your code here
        }
    }
    
    Foo::bar('a', 'b');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Cannot use concatenation when declaring default class properties in PHP? The following
Is it possible to use a regular expression with the php function array_key_exists() ?
is it not possible to define a static const array? i would like to
Possible Duplicate: I have an array of integers, how do I use each one
Is this possible to use Ajax.Beginform with update target inside of ajax form. like
Possible Duplicate: How do I use arrays in C++? I have the following C
Possible Duplicate: default method argument with class property? I'm writing a recursive function and
I would like to initialize an array of structs, with the same element repetitively,
Is possible to set a DateTime object as optional parameter using now as default?
As fast as possible, I would like to replace the first zeros in some

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.