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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:42:31+00:00 2026-06-13T20:42:31+00:00

Probably this is a limitation of PHP, but is it possible somehow, to call

  • 0

Probably this is a limitation of PHP, but is it possible somehow, to call a function – and enforcing the ‘default’ value of an optional parameter – even if in the function call, the optional parameter is given (and == null)?

Maybe it’s easier to show what I mean:

<?php
    function funcWithOptParam($param1, $param2 = 'optional') {
        print_r('param2: ' . $param2);
    }

    funcWithOptParam('something'); // this is the behaviour I want to reproduce

    // this will result in 'param2: optional'

    funcWithOptParam('something', null); // i want $param2 to be 'optional', not null

    // this will result in 'param2: ' instead of 'param2: optional'
?>

Now, the easiest answer to this is “dont write null then” – but in my special case, I get a parameter array for a function call, and only can do this:

<?php
    funcWithOptParam($param[0], $param[1]);
?>

So even if $param[1] is null, the null will overwrite the default value of the optional parameter

I only see one solution to this – to skip the optional parameters, and do it like this:

<?php
    function funcWithOptParam($something, $notOptional) {
         if($notOptional === null) {
              $notOptional = 'defaultvalue';
         }

         ...
    }
?>

But I would like to know if there’s another solution to this. Is there a ‘real’ null value in PHP? That really translates into ‘nothing’? Something like undefined in js?

  • 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-13T20:42:32+00:00Added an answer on June 13, 2026 at 8:42 pm

    Why not pass the whole array as the function argument?
    You simply call it this way:

    funcWithOptParams($params);
    

    And handle it like so:

    function funcWithOptParams($params) {
         if(!isset($params[1])) {
              $params[1] = 'defaultvalue';
         }
    
         ...
    }
    

    This approach gives you the possibility to pass only the arguments you want to specify and have default values in the ones you don’t want to specify. As a bonus you don’t have to worry about the order of the arguments.

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

Sidebar

Related Questions

Probably this isn't the right place... but ill give it a try. I want
This probably sounds like a terrible idea at first glance, but here is my
This probably is a dummy question but I cannot find a clear indication. I
I know this probably really simple but Im not sure what im doing wrong...
I know this probably has been asked before but I am having issues with
Im sorry for this probably dumm question, but I want to simply open modals
I am using ASP.Net MVC but this probably applies to all MVC patterns in
Probably this will not result in any noticable performance difference, but I was just
Okay, this will probably get closed or get some negative replies, but I've been
I don't know much about web development, so probably this question will sound exceptionally

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.