ERROR syntax error, Unexpected T_VARIABLE

Asked

Viewed 1,351 times

1

I have a problem and am beginner in PHP, I do not know if it is easy to fix this error.

(Parse error: syntax error, Unexpected T_VARIABLE in /home/u611580299/public_html/wp-content/themes/simplicityantigo/includes/google-fonts.php on line 1)

<?php
global $fontArrays;

$fontArrays = array(
    0 =>
    array(
        'kind' => 'webfonts#webfont',
        'family' => 'ABeeZee',
        'variants' =>
        array(
            0 => 'regular',
            1 => 'italic',
        ),
        'subsets' =>
        array(
            0 => 'latin',
        ),
    ),
    // ...
);
?>

Complete file

  • 1

    No global variables declared outside the scope of functions, simple.

  • Also, honestly, with a bit of array within an array as you have there, the chance of having forgotten a semicolon..... is huge. rsrs

  • 1

    @Zebradomal dot and comma inside array? Traveled huh

  • 2

    For those who voted to close: How is not reproducible ?

  • It’s a great question, a lot of people will have that same problem.

  • 1

    By the size of this Array I do not advise, but if you want to use a GLOBAL outside the scope of a function you should use the superglobal $GLOBALS[].

  • @Danielribeiro but the way it is the variable is already in the global scope

  • @gmsantos Yes, really, if he just despises the first statement, which should be used within functions, the variable is global in the archive, but if he wants to make it global for the whole project, he can use what I mentioned above, although it is not advisable for performance reasons, because the array eh gigante. If I’m talking straight, please correct me.

  • But actually I think the problem itself was just a syntax error, I took it wrong thinking that he wanted to load the variable out of the file. My mistake.

  • Not exactly @Danielribeiro. Assuming we have the file i.php setting something in $_GLOBAL. If you don’t have at any time a include of i.php in an archive m.php, the variable that I have defined in i.php will also not be available. Sign in using global $a or $_GLOBALS['a'] does not change anything in the performance issue.

  • Got it @gmsantos. Thanks for clearing that up. Basically, the variable does not need to be explicitly declared as $GLOBALS to belong to the scope, only the fact that it exists in the file, out of functions, already includes it in the superglobal. The globals serves to clarify that it is using an existing variable outside the scope of the function. Blz, now it is all understood xD. Right?

  • That’s right @Danielribeiro

  • @gmsantos I was speaking of the semicolon after the array. array (); But from what I’ve looked at now here actually has one array only (I thought they had several when I looked quick). Uahua

Show 8 more comments

1 answer

5


The problem here is in the use of the keyword global.

global serves to reference within the scope of a function a variable in the global scope:

<?php

$a = 5;
$b = 3;

function soma(){
    $a = 1;
    $b = 2;

    return $a + $b;
}

function somaGlobal(){
    // A partir desse ponto, $a = 5 e $b = 3
    global $a, $b;

    return $a + $b;
}

echo soma();        // escreve 3
echo somaGlobal();  // escreve 8

Browser other questions tagged

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