WordPress.org

Ready to get started?Download WordPress

Forums

Variables vs Functions in template files (2 posts)

  1. vinhboy
    Member
    Posted 2 years ago #

    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!

  2. nsathees
    Member
    Posted 2 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic