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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:03:58+00:00 2026-06-04T19:03:58+00:00

I am looking for the exact type of a Required Parameter in a PHP

  • 0

I am looking for the exact type of a Required Parameter in a PHP Function signature

Does initializing a parameter only with NULL makes it an optional? i.e

function foo($optional=NULL, $OptionalOrRequired="Optional Or Required", $required){}

I am confused about the 2nd argument, does it comes in Required or Optional parameter?

UPDATE

I am using reflection to get all and required parameters of a function

public function getPlayer($params=3, $c){}
// results
$reflection->getNumberOfParameters()            ->   2
$reflection->getNumberOfRequiredParameters()    ->   2 // it should be one

public function getPlayer($params=3, $c, $x=NULL)
// results
$reflection->getNumberOfParameters()            ->   3
$reflection->getNumberOfRequiredParameters()    ->   2

As i get in one answer that defaults comes before required, is this the reason the reflection function is returning wrong count for required parameters?

Thanks

  • 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-04T19:03:59+00:00Added an answer on June 4, 2026 at 7:03 pm

    ‘Optional’ arguments are just arguments with a default value, whether that value is null, false, a string (etc.) does not matter – if a function argument has a default value it is optional.

    However, it wouldn’t make sense to have an optional parameter come before a required parameter, as you must give the prior arguments some value (even if it’s null) in order to ‘get to’ the required argument – so all arguments before the last ‘required’ argument are effectively required.

    Some examples:

    // Bad
    function bad($optional = 'literally anything', $required) {}
    
    bad('required arg');              // This breaks - 'missing argument 2'
    bad('something', 'required arg'); // This works - both parameters are needed
    
    // Good
    function($required, $optional = 'literally anything') {}
    
    good('required arg');              // This works just fine, last argument has a default
    good('required arg', 'something'); // Also works fine, overrides 'literally anything'
    

    Update RE: Reflection

    As noted above, putting an ‘optional’ parameter before a required parameter effectively makes that ‘optional’ parameter required, as a value must be given for it in order to ever satisfy the method signature.

    For example, if the first argument has a default and second argument does not (like in the bad function above), we NEED to pass a second argument, and so we NEED to pass a first argument also, so both are ‘required’. If there are subsequent arguments with default values, still only the first 2 arguments are required.

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

Sidebar

Related Questions

Is there any way to find the exact file type in php without looking
Looking at DateTime.TryParseExact(dateString, M/d/yyyy hh:mm:ss tt, CultureInfo.InvariantCulture, 0, out dateValue) I supplied the exact
Looking at the following code, does theme[sprite].img get nested inside result[definition].data (as theme[sprite].img is
Looking for a simple scroll right, nothing special. My code: <script type=text/javascript> // Initialize
I want to check the type of an object. I only want to return
I'm looking at some codes which makes heavy uses of templates. It compiles fine
Looking at similar questions, I actually want the exact opposite of this: SQL query
Looking for more detailed info on how to read the NAL header and more
Looking at the output of this particular method is confusing to me. I'm trying
Looking at the example here - http://jsfiddle.net/uqYeQ/3/ The first row behaves as expected, returning

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.