0

I have a function with an array that has a lot of variables inside it that are declared outside of the function.

Here is a stripped down version of the function:

function get_badges(){
    $badge_array = array(

        array(
            "Comment Freak",
            ($user_revision >= $revision_master_req) && ($tixx1 >= $tixx2)
        ),

        array(
            "Revision Freak",
            ($user_revisionx55 >= $revision_master_reqx134) && ($tixx11233 >= $tixx1342)
        )

    );

    return $badge_array;
}

My question is, what would be the best way to access variables outside the function when considering performance? Upon research, I read I need to use globals but apparently that is not a good approach, particularly if I have many variables...

9
  • passing them as a parameter? Commented Oct 28, 2014 at 15:39
  • 1
    Pass the variables as function arguments. e.g. function get_badges($user_revision, $revision_master_req....) You may also consider grouping data into class objects or arrays, and passing just a few arguments instead. Commented Oct 28, 2014 at 15:40
  • @lolka_bolka How does this work? Commented Oct 28, 2014 at 15:40
  • @Populus Can you please write up an answer detailing exactly how this works? Please consider I am learning PHP so a lot of terms are completely new to me. Commented Oct 28, 2014 at 15:41
  • Function parameters in PHP work just like in all other programming languages. Commented Oct 28, 2014 at 15:41

3 Answers 3

1

You need to pass your variables as parameters, example:

function get_badges($user_revision, $revision_master_req, etc... ){
    $badge_array = array(

        array(
            "Comment Freak",
            ($user_revision >= $revision_master_req) && ($tixx1 >= $tixx2)
        ),

        array(
            "Revision Freak",
            ($user_revisionx55 >= $revision_master_reqx134) && ($tixx11233 >= $tixx1342)
        )

    );

    return $badge_array;
}
Sign up to request clarification or add additional context in comments.

Comments

1

you can pass params as an array. And also, you can create a little helper function what is build this array for you, so you do not need to build it every time. Global is not so good, but this time it will help you.

$args = getBagesArgs();
$bagets = get_badges($args);

function get_badges($args) {
    $badge_array = array(
        array(
            "Comment Freak",
            ($args['user_revision'] >= $args['revision_master_req']) && ($args['tixx1'] >= $$args['tixx2'])
        ),
        array(
            "Revision Freak",
            ($args['user_revisionx55'] >= $args['revision_master_reqx134']) && ($args['tixx11233'] >= $args['tixx1342'])
        )
    );
    return $badge_array;
}

function getBagesArgs() {
    global $user_revision, $revision_master_req, $tixx1, $tixx2, $user_revisionx55, $revision_master_reqx134, $tixx11233, $tixx1342;
    $args = array(
        'user_revision' => $user_revision,
        'revision_master_req' => $revision_master_req,
        'tixx1' => $tixx1,
        'tixx2' => $tixx2,
        'user_revisionx55' => $user_revisionx55,
        'revision_master_reqx134' => $revision_master_reqx134,
        'tixx11233' => $tixx11233,
        'tixx1342' => $tixx1342
    );
    return $args;
}

Comments

1

The calling

get_badges( array( 'user_revision ' => 4211
    , 'revision_master_req' => 9845
    , 'tixx1 ' => 778, 
) );

The body :

function get_badges($all_vars=array()){
$badge_array = array(

    array(
        "Comment Freak",
        ($all_vars['user_revision'] >= $all_vars['revision_master_req']) && ($all_vars['tixx1 ']>= $all_vars['tixx2'])
    ),

etc...
);
return $badge_array;
}

Comments

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.