• Can someone explain what is the difference in scope here

    If in my functions.php I had this

    $blue = 'blue';
    function red() {
      echo 'red';
    }

    then in sidebar.php

    <?php echo $blue; ?>
      <?php red(); ?>

    If I included the sidebar with get_sidebar(), which involves a “require”, the function red() is available and works, but the variable blue does not exist.

    My question is, why is the function red() available, but the variable $blue, not available? (unless I make $blue a global).

    Thanks in advance!

Viewing 1 replies (of 1 total)
  • you have declared the value for $blue in function file. The wordpress reads only the functions from there. Thus the variable $blue value not declared.

Viewing 1 replies (of 1 total)
  • The topic ‘Variables vs Functions in template files’ is closed to new replies.