php within an include

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.
karimjohnsonAsked:
Who is Participating?
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
BTW : look here http://www.php.net/manual/en/function.file-get-contents.php

file_get_contents reads remote file like file() ... does not execute
0
 
Ray PaseurCommented:
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.
http://php.net/manual/en/language.variables.php
0
 
karimjohnsonAuthor Commented:
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:

<?php
switch($TopNavElement) {
    case "Training":
        $sidebarname = "sidebarTraining";
        break;
} // end switch
?>

What am i doing wrong
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
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

include_once('../inc/'$sidebarname.'.txt'')


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

cys bb
0
 
karimjohnsonAuthor Commented:
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.

0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
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 .

0
 
karimjohnsonAuthor Commented:
those were typos. the line should be $file = file_get_contents....  and I did have the dot where you suggested.
0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
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)

0
 
karimjohnsonAuthor Commented:
Thanks for enlightening me. I was afraid that was the problem.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.