1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Basic question about variable scope

Discussion in 'PHP' started by HenryCan, May 29, 2017.

  1. #1
    The website I'm revising will consist of a bunch of PHP files and some other odds and ends. I would like each of those PHP files to have access to certain variables whose values I want to set. For example, one of the variables indicates if the individual web pages I'm displaying via PHP should show the icons for HTML and CSS validators. I want to be able to set a variable named $show_validators equal to either "Y" or "N" somewhere and then all the PHP files somehow access the value of $show_validators.

    I'm having trouble making that happen. Can someone please reassure me that it *is* possible to do this? If it *is* possible, how should I do it?

    I created a simple file called validators.php and put a single line in it that said:

    $show_validators = "Y";
    PHP:
    Then, in the files that need to know the value of $show_validators, I added this line:

    <php? include 'variables.php'; ?>
    PHP:
    On the next line, I used a normal if like this:

    if ($show_validators == "Y") {
        include 'foo.shtml';
    }
    PHP:
    I truly expected that to work but it didn't. That code wouldn't even validate in the HTML validator.

    What is the right way to do this? I have only a working knowledge of HTML and haven't found any clear answer to my question in the PHP manual.
     
    HenryCan, May 29, 2017 IP
  2. hdewantara

    hdewantara Well-Known Member

    Messages:
    536
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #2
    Maybe it should be like:
    if(isset($show_validators) && $show_validators == "Y")
    {
        include 'foo.shtml';
    }
    PHP:
    Else, try to set error reporting to -1 and check?
     
    hdewantara, May 29, 2017 IP
  3. sarahk

    sarahk iTamer Staff

    Messages:
    28,494
    Likes Received:
    4,457
    Best Answers:
    123
    Trophy Points:
    665
    #3
    if this is a copy and paste of your code then
    <php? include 'variables.php'; ?>
    PHP:
    should be
    <?php include 'variables.php'; ?>
    PHP:
    that variable should be available. It gets trickier when you start having functions and classes but your code is quite straightforward.
     
    sarahk, May 29, 2017 IP
  4. Blank ™

    Blank ™ Well-Known Member

    Messages:
    223
    Likes Received:
    18
    Best Answers:
    6
    Trophy Points:
    110
    #4
    First things first, your PHP has nothing to do with HTML. Point. Otherwise, @sarahk seems to be on the right track. :)
     
    Blank ™, May 29, 2017 IP
  5. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #5
    Also, all these variables should be stored in a database (for easy changing and control), and you should have a method or way of fetching specific variables. Also, the variables should be either 1 for yes, and 0 for no.
     
    PoPSiCLe, May 29, 2017 IP
  6. HenryCan

    HenryCan Member

    Messages:
    39
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #6
    You nailed it, Sarahk! It was a typo. As soon as I fixed it, it worked fine. I usually find my own typos without too much trouble but this one slipped past me. Thanks for catching it!

    Yeah, sorry about that, I meant to say PHP, not HTML. My HTML is pretty fluent; it's my PHP that is still pretty rudimentary ;-)

    I'll have to give that some thought. I know how to use databases so that's not a problem but changing the values in a trivial PHP file is already pretty easy. If I put it in a database, I'd either have to edit the table manually with phpMyAdmin or something similar or write a program to insert/update/delete the variables; that's more work than just putting it in a trivial PHP program that I can edit in two seconds. I'm not sure the benefits justify the work in this case given that the variable is just doing something cosmetic on the web pages.
     
    Last edited by a moderator: May 29, 2017
    HenryCan, May 29, 2017 IP
  7. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #7
    You're thinking about it the wrong way. If you need to manually edit a specific PHP-file to change something stupid like this, you're doing it the wrong way. If you get more settings/variables, you get more files. And that will quickly become both bothersome and boring to do via FTP or whatever else direct editing you're doing. And oops, you have a typo, or you forget to change something, and so on and so forth.

    If you have a simple admin-interface, you can do these changes directly from that, all together at once, or just single ones. Yes, it's a bit more code / work when you make the interface, but it will (in my opinion) simplify things in the long run.

    Or, at least make a single config-file, and pull the variables from that file.
     
    PoPSiCLe, May 29, 2017 IP