Jump to content

Variable Before Include


steveangelis

Recommended Posts

As per all of my botcheries of coding, I am attempting to do something that does not want to work.

 

$report_info = include("srs/".$_POST['report_types'].".php");

 

Now because of how the pages are setup, the variable must be before the include in this manner.  What I want is for the include to be, well, included when the variable is called up.  For instance, the $_POST['report_types'] comes from another page, and when that variable is loaded onto the next page, that line of code is used.  Once the variable is called up later in the page, I need the page to be included to be displayed.  If it makes sense to anyone, does anyone know why it is not working?  The page is being included at the top of the page where this line of code is.

Link to comment
https://forums.phpfreaks.com/topic/191837-variable-before-include/
Share on other sites

include is not a function is a statement so it doesnt return anything.

include works like this

 

lets say this is your index.php

<?php
$GLOBAL_VAR = 13;

include("another.php");

echo $GLOBAL_VAR;
?>

 

now let's say another.php looks like this

echo "Hello!!!<br />";
$GLOBAL_VAR = $GLOBAL_VAR + 2;

 

ouput of index.php

will be

Hello!!!
15

 

u can imagine include as u would copy and paste peace of code

bu remember it doesn return content of that file(use file_get_contents in those cases)

 

 

Sader is correct, however, if you do want the "output" from an included file returned in a variable you can use output buffering:

 

ob_start();
include("srs/".$_POST['report_types'].".php");
$report_info = ob_end_clean();

 

Would do it, however, I highly suggest against calling files with include like you have. It is a security risk.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.