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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:21:29+00:00 2026-06-09T06:21:29+00:00

I have lots of functions with optional arguments , which on an omitted value

  • 0

I have lots of functions with optional arguments, which on an omitted value gets its default value from a specified function, currently my code looks something like this:

function get_user($user_id = FALSE) {

   // If no ID is passed, get from session
   if(!$user_id) { 
      $user_id = get_id_from_session();      
   }

   // ... do something with the ID
}

It works fine, but it easily gets very clutty when having more then one optional argument. Instead, I’d prefer to do something like the following:

function get_user($user_id = get_id_from_session()) {

   // ... do something with the ID

}

I’m sure that you can see how that is more convenient. Is there any way to accomplish this, or do anyone have suggestions on another cleaner approach 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-09T06:21:30+00:00Added an answer on June 9, 2026 at 6:21 am

    The only way that you can “shorten” this is to use the ternary operator:

    $user_id = ( $user_id === false) ? get_id_from_session() : $user_id;
    

    Which is just a compact version of writing:

    if( $user_id === false) {
        $user_id = get_id_from_session();
    }
    

    If you want to be real fancy and less-readable, you can omit the middle part (PHP > 5.3):

    $user_id = ( $user_id) ?: get_id_from_session();
    

    Now, if ( $user_id) evaluates to true, you’d get the value of $user_id in $user_id, otherwise you’d get the return value from the function.

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

Sidebar

Related Questions

I have a function that takes optional arguments as name/value pairs. function example(varargin) %
We have lots of javascript functions, which are usually handled via the onclick function.
I have a code-base of legacy C/C++ code, which contains lots of functions that
i have a application which is handling lots of functions and also having lots
I received a demand to correct a ASP website that have lots of functions
I have lots of code like this in my constructors:- function __construct($params) { $this->property
The problem is the following. We have lots of ajax call via $.ajax function.
We have lots of stored procedures which all contain a lot of comments. Is
I have lots and lots of C preprocessor #define statements, which make my C
I have to use an extern library providing lots of free functions that do

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.