php within an include

Posted on 2011-05-02
Last Modified: 2012-08-13
I want to move a switch statement from my php file into an include file. When I tried to do this, the switch didn't execute. I need to know if it is possible, and if so if there are some rules.
Question by:karimjohnson
    LVL 107

    Expert Comment

    by:Ray Paseur
    Sure, it's possible.  The included files have the same variable scope as the script that included them.  One reason this sometimes doesn't work is because the include file wraps the moved code into a code block inside a function.

    Author Comment

    my main.php has:

    <?php $TopNavElement = "Training"; ?>
    <?php $file - file_get_contents ('../inc/sidebarSwitch.php'); echo $file; ?>
    <?php $file - file_get_contents ('../inc/'$sidebarname.'.txt''); echo $file; ?>

    my sidebarSwitch.php has:

    switch($TopNavElement) {
        case "Training":
            $sidebarname = "sidebarTraining";
    } // end switch

    What am i doing wrong
    LVL 9

    Expert Comment

    Hi !

    Fiirst thing i see is

    <?php $file - file_get_contents ('../inc/'$sidebarname.'.txt''); echo $file; ?>  //( ' and . is in wrong place )

    line should be

    <?php $file - file_get_contents ('../inc/'.$sidebarname.'.txt'); echo $file; ?>

    And may i ask why do you use file_get_contents instead of include or require.
    i would better use


    And why do you place   $file - in          " <?php $file - "
    you don't need them ..

    cys bb

    Author Comment

    a few clarifications:

    my file is essentially an html file with embedded php when needed. So when I need to make a php call I have to start with <?php

    I use file_get_contents because include is not activated on the server for security reasons.

    LVL 9

    Expert Comment

    the clear problem is indicated above ... have you tried my suggestions about that line  ? change line below in your code first ....

    <?php $file - file_get_contents ('../inc/'$sidebarname.'.txt''); echo $file; ?>

    and btw i didnt mention <?php
    i was talking about         $file -  part of the lines.

    what are they for ? that seem like a subtraction like $a - $b but this is not the way if you do so .


    Author Comment

    those were typos. the line should be $file = file_get_contents....  and I did have the dot where you suggested.
    LVL 9

    Expert Comment

    oohh i ve just got the deal ... you are trying the imposibble :)

    file_get_contents just gets string data ... does not serve scope environment (such as variables) to the remote file.

    Just calls that file as you would do in any browser and take source code of page and paste it into $file manually :)

    Can not share variables because remote file is executed on remote server and serves output to our main php file ... (no matter they are in the same dir or another host/domain)

    This has to be so, because if it does as you wish this would be a HUGE securety hole. Think about someone changes remote file to delete your db using your connection link defined ... that would be an uncontrollable nightmare.

    Include or require is the only things does what you want. It's not sensible restricting use of them. Talk to your server admin or change your host better way.

    I've used hosts over 20 and haven't seen anyone restricted include ... none of php scripts work in that way.

    if you still insist on using that the only way of making that is passing variables over QueryString to the remote php. and recoding remote file according to this. and return appropriate string output

    Good Luck  (You'll need it without includes)

    LVL 9

    Accepted Solution

    BTW : look here

    file_get_contents reads remote file like file() ... does not execute

    Author Closing Comment

    Thanks for enlightening me. I was afraid that was the problem.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now