1

I know title seems obscure but trust me I have researched this already but it fails for my code.

I have a code for licensing which pretty much looks like this

<?php if ( true ) : ?>
<?php print( 'Your License is Active' ); $type = 1; ?>

It pretty much sets the $type variable.

Now my problem:

function file_pull() {
if ( $type == 1 ) {
    $import_path = '/main/';

    $files_array = array(
        'start'               => array(),
        'home'                => array(
            'description' => __( 'Importing: Main Templates', 'kss' ),
            'files'       => array(
                'home.xml',
            ),
        ),

    );
}
if ( $type == 2 ) {
    $import_path = '/main/';

    $files_array = array(
        'start'               => array(),
        'home'                => array(
            'description' => __( 'Importing: Main Templates', 'kss' ),
            'files'       => array(
                'page1.xml',
            ),
        ),

    );
}

}

How can I use the $type variable inside this function so that it runs these file pull requests?

I have tried global to no avail. I have also tried setting it as a parameter.

function file_pull($type) {

Thanks.

4
  • 1. Are you setting the $type and then calling the function in the same request? 2. Can you put var_dump($type); inside file_pull function (just above the first if) and post the result? Commented Mar 20, 2018 at 9:43
  • you need to set $type before you call the function. Commented Mar 20, 2018 at 9:44
  • 3
    function file_pull($type) { is the way to go, but be aware that $type in this case does not automatically use your $type from outside the function. You'd need to call the function like file_pull(1) or file_pull(2) Commented Mar 20, 2018 at 9:48
  • 1
    Just to add on to @kerbholz's comment (which I also believe is the best solution to this question) $type is out of scope from your function. Have a read of the documentation on function scopes to get a better grasp on the issue. Commented Mar 20, 2018 at 9:56

1 Answer 1

1

Your function file_pull($type) { is a good start. Change your function to:

function file_pull($typeParam) {
    if ( $typeParam == 1 ) {
        $import_path = '/main/';

        $files_array = array(
            'start'               => array(),
            'home'                => array(
                'description' => __( 'Importing: Main Templates', 'kss' ),
                'files'       => array(
                    'home.xml',
                ),
            ),

        );
    }
    if ( $typeParam == 2 ) {
        $import_path = '/main/';

        $files_array = array(
            'start'               => array(),
            'home'                => array(
                'description' => __( 'Importing: Main Templates', 'kss' ),
                'files'       => array(
                    'page1.xml',
                ),
            ),

        );
    }
}

This way you can call it like:

$type=1;
file_pull($type);

I used $typeParam as function parameter to not get confused with your $type outside your function.

Sign up to request clarification or add additional context in comments.

2 Comments

Not sure who you're talking about when saying about confusion but PHP won't get confused if file_pull requires $type. The OP's code likely didn't work before because he probably didn't call file_pull($type);. Anyway, this wins my upvote as it's the solution I was hoping for.
I meant OP maybe getting confused about two $types there

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.