The MU forums have moved to WordPress.org

Completely basic PHP question. Don't laugh at me. (4 posts)

  1. nolageek
    Member
    Posted 15 years ago #

    I know this is like, PHP 101... I used to so this a million times but I can't get it to work for some reason.

    I have text that I only want to show on my index page.

    In my index I have

    <?php
    
    $bnindex = 1;
    
    ?>

    In my footer I have

    <?php
    
    if ($bnindex == 1;) {
    
    echo 'test';
    
    }?>

    Super, super, super basic. But it doesn't work. I've tried using the value of "yes" (with quotes) and still nothing.

    I know I'm no PHP hacker, but this is like.. I mean. I dunno. Is there something obvious that I'm not seeing?

  2. drmike
    Member
    Posted 15 years ago #

    The issue is footer.php is being called as a function. You may have to declair $bnindex as a global. Something like:

    <?php
    
    echotest();
    
    function echotest() {
         global $bnindex;
         if ($bnindex == 1;) {
              echo 'test';
         }
    }
    ?>

    Only thing I can think of. I'm just an beginner on this as well.

  3. digiC
    Member
    Posted 15 years ago #

    You would have to have your variable '$bnindex' declared before the conditional statement (if statement) in the footer.

    Also I would probably declare it in a function so in index.php you would have:

    <?php
    function echotest($output_text) {
    return $output_text;
    }
    ?>

    then in footer.php (where you want to output text) you would have this:
    <?php
    if (function_exists('echotest')) {
    echo echotest('test');
    }
    ?>

    I've made a few changes to make it more versatile aswell. Another EASIER and better way to do it is to actually check what page your on and then show what you want.

  4. lunabyte
    Member
    Posted 15 years ago #

    You're in 2 different files, that don't talk to each other.

    Using your original example, without trying to confuse you, you would need to do something like this:

    index.php:

    <?php
    global $var;
    ?>

    footer.php

    
    <?
    global $var;
    
    if ( $var == '1' ) {
    echo 'var == 1';
    }
    ?>
    

    First you have to make "var" available to the global scope, and then you have to call it from the global scope. If that makes sense. I'm trying to keep it simple. lol

    Now if they were all in index.php, there would be no need for a global variable since it's within the same file.

About this Topic

  • Started 15 years ago by nolageek
  • Latest reply from lunabyte