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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:46:22+00:00 2026-06-14T20:46:22+00:00

I have a function with 5 arguments and default site width variable $site_width =’1000px’;

  • 0

I have a function with 5 arguments and default site width variable

$site_width ='1000px';
function MyPrintFunction ($divName,$width=false,$html1='',$html2='', $echo = true ){
      ... plenty of code above, no need to post complete function

        $html = '<div id="'.$divName.'">';
        $html .= '</div>';
        if( $echo ){
          echo $html1.$html.$html2;
        }else{
          return $html1.$html.$html2;
        }


}

and than I print html like:

MyPrintFunction ('divname1',true);
MyPrintFunction ('divname2');
MyPrintFunction ('divname3');

I need to check if any of these functions has set the second argument to true, and if yes , I need first argument name.

This way I can add the argument name to css snippet ( will be injected in head later ) and set width for it

#ARrument1NameSHouldGoHereAsVariable{
   width:$site_width;
}

I hope I explained this right. Sorry if I did not. Any help is appreciated. Thank you!

  • 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-14T20:46:23+00:00Added an answer on June 14, 2026 at 8:46 pm

    Noo no no, you absolutely can have optional arguments. It’s a bit messy of a situation, but totally possible.

    echo MyPrintFunction('dafsd',435);
    function MyPrintFunction ($divName,$width=2342){
        return $divName . $width;
    }
    

    The reason it is discouraged is because…
    Let’s suppose you want to set a height, but not a width to 435

    echo MyPrintFunction('dafsd',435);
    function MyPrintFunction ($divName,$width=2222, $height=1111){
        return $divName . $width . $height;
    }
    

    Optional arguments should generally not be used, and if they are to place them at the end in an array

    Please oh please let this be what you are seeking…

    function MyPrintFunction ($divName,$width=false){
        $newObj = array();
        $newObj['divName'] = $divName;
        $newObj['width'] = $width;
        return $newObj;
    }
    function hasWidth($newObj) {
        if ($newObj['width']) {
            return $newObj['width'];
        }
        else {
            return 'no width!'; 
        }
    }
    $tempStore1 = MyPrintFunction('dafsd',123);
    $tempStore2 = MyPrintFunction('dafsd');
    echo hasWidth($tempStore1);
    echo hasWidth($tempStore2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP function that takes a variable number of arguments (using func_num_args()
I have a function that is supposed to take a variable number of arguments
I have a function in my company's site that uses the cgi.remote_host variable to
Is it possible to have default arguments in MATLAB? For instance, here: function wave(a,
Possible Duplicate: Default Arguments in Matlab I have a function with about 7 parameters
I have a function which takes two arguments, the ID of an item (wine)
I have a function which takes several boolean template arguments: template<bool par1, bool par2,
I have a function that takes variadic arguments, that I obtain from func_get_args() .
I have a function that can accept any number of arguments... const getSearchFields =
Say I have a function that takes an arbitrary number of arguments (the last

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.