alin19 Posted April 22, 2008 Share Posted April 22, 2008 how can i declare a var in a php file and to be used in a second file that is included in first? i know that i haven't been very clear with this, so i have an example here: <?php $x="test"; include ("/usingtest.php"); ?> //usingtest.php <?php $y=$x ?> Link to comment https://forums.phpfreaks.com/topic/102301-include/ Share on other sites More sharing options...
wrathican Posted April 22, 2008 Share Posted April 22, 2008 as far as i know you need to declare the variable in the included file: <?php //setx.php $x = "test"; ?> <?php //displayx.php include ('setx.php'); $y = $x; echo $y; ?> woud output: test Link to comment https://forums.phpfreaks.com/topic/102301-include/#findComment-523838 Share on other sites More sharing options...
micah1701 Posted April 22, 2008 Share Posted April 22, 2008 when you include a file, its just like copying and pasting the code from that other file into the parent file so, what you're proposing should work fine. <?php //page2.php $y = $x + 3; ?> <?php //parent page $x = 2; include('page2.php'); echo $x * $y; // result: 10 ?> Link to comment https://forums.phpfreaks.com/topic/102301-include/#findComment-523849 Share on other sites More sharing options...
alin19 Posted April 22, 2008 Author Share Posted April 22, 2008 but i need the other way around, what can i do than? i need a var declared in the first file to could be included in the second one; //first <?php <form> <input type=text name="form"> </form> $x=$_POST['form']; include(second.php); ?> //second <?php echo "test"; //this isn't working $y=$x; echo $y; //and this also isn't working ?> Link to comment https://forums.phpfreaks.com/topic/102301-include/#findComment-523851 Share on other sites More sharing options...
micah1701 Posted April 22, 2008 Share Posted April 22, 2008 are you sure you have the path to the include file right? echo "test"; should at least print the word test ??? as a test, try using require() instead of include() if the path is wrong, the parent page will throw an error (or not load at all) Link to comment https://forums.phpfreaks.com/topic/102301-include/#findComment-523902 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.