Skip to content
Advertisement

how do I declare a variable outside a public static function in php?

I am trying to declare a variable outside a public class in PHP, but for some reason, it does not work.

Here is my code

$catID = '33234532';

    public static function saveFile($name, $catID){
    $name = 'catid_' . $catID . time() . '_' . '.' . pathinfo(saveFiles:sanitizeFileName($name), PATHINFO_EXTENSION);
    ... rest of my code
    }

now, when I save the file I get catid__1487393557.png, it completely ignores the $catID.

Now, when I do this below it outputs the right thing.

    public static function saveFile($name, $catID){
    $catID = '33234532';

    $name = 'catid_' . $catID . time() . '_' . '.' . pathinfo(saveFiles:sanitizeFileName($name), PATHINFO_EXTENSION);
    ... rest of my code
    }

This outputs this: catid_33234532_1487393557.png

Advertisement

Answer

You can try global variable to declare variable outside function

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement