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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:30:22+00:00 2026-06-02T00:30:22+00:00

I like creating my PHP functions using key=>value pairs (arrays) as arguments instead of

  • 0

I like creating my PHP functions using key=>value pairs (arrays) as arguments instead of individual parameters.

For example, I prefer:

function useless_func($params) {
    if (!isset($params['text'])) { $params['text'] = "default text"; }     
    if (!isset($params['text2'])) { $params['text2'] = "default text2"; }   
    if (!isset($params['text3'])) { $params['text3'] = "default text3"; }   
    echo $params['text'].$params['text2'].$params['text3'];
    return;
}

And I don’t like:

function useless_func($text = "default text", $text2 = "default text2", $text3 = "default text3") {
        echo $text.$text2.$text3;
    return;
}

I had first seen things done this way extensively in the WordPress codebase.

The reason I prefer arrays:

  • Function arguments can be provided in any order
  • Easier to read code / more self documenting (in my opinion)
  • Less prone to errors, because when calling a function I must investigate the proper array keys

I was discussing this with a co-worker and he says that it’s useless and just leads to extra code and it’s much harder to set the default values. Basically, he disagrees with me completely on all three points.

I am looking for some general advise and guidance from experts who might be able to provide insight: What’s the better or more proper way to do this?

  • 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-02T00:30:24+00:00Added an answer on June 2, 2026 at 12:30 am

    Well, it’s kinda usefully. But for some arguments which is passing always it’s better to use classic passing like function some($a1, $a2). I’m doing like this in my code:

    function getSome(SomeClass $object, array $options = array())
    {
        // $object is required to be an instance of SomeClass, and there's no need to get element by key, then check if it's an object and it's an instance of SomeClass
    
        // Set defaults for all passed options
        $options = array_merge(array(
            'property1' => 'default1',
            'property2' => 'default2',
            ... => ...
        ), $options); 
    }
    

    So, as you can see I like that code style too, but for core-arguments I prefer classic style, because that way PHP controls more things which should I, if I used the you code style.

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

Sidebar

Related Questions

I'm creating a JSON object like tags = {jon:[beef,pork],jane:[chicken,lamb]}; which was generated using php
I'm creating xml-like mark-up language using System.Xml.XmTextWriter that will be read by a third
i am creating a threaded comment using php.I using this code to display the
I started using PHP classes and looked into OOP. I played around by creating
I am creating a registration form using PHP and jquery with ajax. Each field
I'm creating a simple application using the Kohana PHP framework, just FYI. This is
I am creating a multipage form in PHP, using a session. The $stage variable
I'm creating JSON encoded data from PHP arrays that can be two or three
I am having trouble tracking down information regarding extending Intellisense. It looks like creating
Im creating forum like application and I have textbox where users can enter tags

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.