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!

7
  • @L0j1k not sure I understand Commented Nov 10, 2012 at 4:01
  • If you're creating the div there, why not just put the width as the div's style? It seems like that would be easier. Commented Nov 10, 2012 at 4:02
  • @G-Nugget lol , you got me , I already have it in the function like that , and want to take it out of inline and put it where it belongs , <head> Commented Nov 10, 2012 at 4:05
  • @L0j1k check my posts first before complaining , see how many answers I got to accept Commented Nov 10, 2012 at 4:06
  • Please clarify I need to check if any of these functions has set the second argument to true, and if yes , I need first argument name. It sounds like you just want to knoe the first argument, which you can't not know. Commented Nov 10, 2012 at 4:07

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

8 Comments

well that is what I have but again , can someone help me if it is all possible to find out what the first and second arguments are outside the function ?
I did not post the complete function and yes I check for all arguments defined before prints , the complete function is much bigger than example above , but my question is not how my function is formulated
Are you looking for something like php.net/manual/en/function.func-get-args.php
no jared , the user has option to add or NOT add the width to the div within the function , if he does , I need to find out what div name is in question so I cann add it to css that will be placed in the head . right now I have widths defined as inline style and I want to remove the from there and placed them inside the head . hope this makes sense
lol I wish it did make sense. Next suggestion! You could just return an object and then outside the function check if the width property of the returned object is true?
|
1

What's this for?

function MyPrintFunction ($divName,$width=false,$html1='',$html2='', $echo = true ){

The logic goes like this

<?php
function blahblah($param1, $param2) {
//Put some weirdy stuff here

if($param2 == '1') {
 //Than it's true
  } else {
 //false
  }
}
blahblah("div1", "1"); //This is true
blahblah("div1", "0"); //This is false
?>

7 Comments

it is for all possible options, html before or after the main div name etc
well will do but kinda still does not gives me a clue to my question. how to get the function argument values outside of it
and if I use it the way you do I am forced to call all function arguments when printing , otherwise will have missing arguments 2 3 etc
no you cannot have missing arguments, check this out php.net/manual/en/function.func-get-args.php and this too php.net/manual/en/functions.arguments.php
the answer has nothing to do with the question , I did not ask how to formulate the function
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.