Jaymoon Posted August 24, 2009 Share Posted August 24, 2009 I was wondering if it was possible to use an include when defining a variable. This is the code in the form of how I want it to behave... //Contents of color.php Green // Contents of index.php $color = include('color.php'); <p>My favorite color is:</p> <p><? $color ?></p> So how would I define the contents of color.php as the $color variable in index.php? Link to comment https://forums.phpfreaks.com/topic/171588-using-an-include-in-defining-a-variable/ Share on other sites More sharing options...
Cardale Posted August 24, 2009 Share Posted August 24, 2009 Probably want to do something like this Inside color.php <? $color = "blue"; ?> inside your index.php <? include "color.php"; echo "$color"; ?> This is really dirty but is the jist Link to comment https://forums.phpfreaks.com/topic/171588-using-an-include-in-defining-a-variable/#findComment-904832 Share on other sites More sharing options...
Jaymoon Posted August 24, 2009 Author Share Posted August 24, 2009 Probably want to do something like this Inside color.php $color = "blue"; inside your index.php include "color.php"; echo "$color"; This is really dirty but is the jist Well I thought of that, but I'm not sure if that will work. Without getting too complicated in explaining... color.php needs to only be a string of text because it will be inserted by a form drop down box. So on edit.php, there will be a drop down box with a list of colors. The user chooses Green, and when they hit save, this live-edit PHP code I have writes 'Green' to color.php. Then of course index.php will need to display the color.php contents. Link to comment https://forums.phpfreaks.com/topic/171588-using-an-include-in-defining-a-variable/#findComment-904837 Share on other sites More sharing options...
Cardale Posted August 24, 2009 Share Posted August 24, 2009 If I understand you correctly your trying something a little more complicated than you first described. This would require cookies, sessions, or a database of some sort. You could do this pretty easy with cookies if you don't want to complicated a system. If you want some real help for your problem I suggest you get as complicated as you can in your explanation of your situation. Link to comment https://forums.phpfreaks.com/topic/171588-using-an-include-in-defining-a-variable/#findComment-904838 Share on other sites More sharing options...
oni-kun Posted August 24, 2009 Share Posted August 24, 2009 I get your problem. You'll have to use something such as this. include() would basically be the same as shoving that color into the form and not have it parse as a variable. $color = get_file_contents('./color.php'); And you're set! Link to comment https://forums.phpfreaks.com/topic/171588-using-an-include-in-defining-a-variable/#findComment-904840 Share on other sites More sharing options...
Jaymoon Posted August 24, 2009 Author Share Posted August 24, 2009 $color = get_file_contents('./color.php'); That looks like exactly what I need, however it doesn't seem to be parsing. Fatal error: Call to undefined function get_file_contents() in D:\Web\college-picks\vote\week-01.php on line 9 Here is the actual code I am using: // contents of index.php 8 <?php 9 $awayfull01 = get_file_contents('./vote/week-01/01-away-full.php'); 10 ?> 11 12 <p><img src="images/team/<? $awayfull01 ?>.jpg" /></p> Right now the contents of '01-away-full.php' is just 'Stanford'. So I would like the page to render: <p><img src="images/team/Stanford.jpg" /></p> Hopefully that explains it a little better? I am using PHP 5.2.9 on Windows if that matters any. Link to comment https://forums.phpfreaks.com/topic/171588-using-an-include-in-defining-a-variable/#findComment-904850 Share on other sites More sharing options...
oni-kun Posted August 24, 2009 Share Posted August 24, 2009 Whoops, misworded the function. It's file_get_contents(). // contents of index.php <?php $awayfull01 = file_get_contents('./vote/week-01/01-away-full.php'); ?> <p><img src="images/team/<?php echo $awayfull01; ?>.jpg" /></p> Link to comment https://forums.phpfreaks.com/topic/171588-using-an-include-in-defining-a-variable/#findComment-904853 Share on other sites More sharing options...
Jaymoon Posted August 24, 2009 Author Share Posted August 24, 2009 Whoops, misworded the function. It's file_get_contents(). // contents of index.php <?php $awayfull01 = file_get_contents('./vote/week-01/01-away-full.php'); ?> <p><img src="images/team/<?php echo $awayfull01; ?>.jpg" /></p> Ah hah! Thank you very much oni-kun, that works perfect! Link to comment https://forums.phpfreaks.com/topic/171588-using-an-include-in-defining-a-variable/#findComment-905007 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.