syntax error, Unexpected 'global' (T_GLOBAL), expecting Function (T_FUNCTION) or const (T_CONST)

Asked

Viewed 259 times

0

I’m trying to call a variable into a function, but I’m making this mistake :

( ! ) Parse error: syntax error, Unexpected 'global' (T_GLOBAL), expecting Function (T_FUNCTION) or const (T_CONST)

<?php
/**
 *
 * Classe para obter dados do rank do PointBlank <pb.ongame.net>
 * e recursos avançados. Requer PHP 5 ou superior.
 *
 * @author     SkelletonX <[email protected]>
 * @version    1.0 $ 2018-10-21 01:09:51 $
 */
 class PB
 {  
    global $PBURLCLAN = "https://pb.ongame.net/ranking/clan/678998/GhostStars/?ref=ranking-season";
    
    public static function Clan_Info_Wrapper_Master(){
        libxml_use_internal_errors(true);
        $dom = new DOMDocument();
        $dom->loadHTMLFile($PBURLCLAN);
        $xpath = new DomXPath($dom);
        $elements = $xpath->query('//div[@id="clan_info_wrapper"]/div[1]/div/div/div[2]/div/div[2]');
            libxml_use_internal_errors(true);
            if (!is_null($elements)) {
                foreach($elements as $element){
                    $nodes = $element->childNodes;
                    foreach ($nodes as $node) {
                      echo $node->nodeValue. "\n";
                    }
                }
            }
    }

1 answer

0

You managed to solve?

For me it only worked when I first declared the variable as global and then assigned the value. In this way I was able to access the global variable within the scope of the function.

Using your example, it would look like this:

global $PBURLCLAN;
$PBURLCLAN = "https://pb.ongame.net/ranking/clan/678998/GhostStars/?ref=ranking-season";

After that you can access the global variable from within the function by calling as $GLOBALS['PBURLCLAN']:

$dom->loadHTMLFile($GLOBALS['PBURLCLAN']);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.