cinos11 Posted February 28, 2010 Share Posted February 28, 2010 I need to make a variable php code so i can include it in one of my files. I need it to be like: $testimony = ('include($_SERVER['DOCUMENT_ROOT'].'/page/testimony/customer_testomonies.php')') but that does not work and i get the error Parse error: syntax error, unexpected T_STRING in C:\Server\www\myserver.dev\vhost\REMOVED\public\page.php on line 2 The page that will be using the variable will include the file and have: <?php $testimony ?> How do i get it to work properly without the error? Quote Link to comment https://forums.phpfreaks.com/topic/193612-making-a-variable-php-code/ Share on other sites More sharing options...
smerny Posted February 28, 2010 Share Posted February 28, 2010 well the first thing i notice is your quotations... single quotes within single quotes 'include($_SERVER[' Quote Link to comment https://forums.phpfreaks.com/topic/193612-making-a-variable-php-code/#findComment-1019177 Share on other sites More sharing options...
cinos11 Posted February 28, 2010 Author Share Posted February 28, 2010 Well if i do double quotes for a change i get this error: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\Server\www\myserver.dev\vhost\REMOVED\public\page.php on line 2 using new quotes the code looks like: $testimony = ("include($_SERVER['DOCUMENT_ROOT'].'/page/testimony/customer_testomonies.php')") Quote Link to comment https://forums.phpfreaks.com/topic/193612-making-a-variable-php-code/#findComment-1019178 Share on other sites More sharing options...
smerny Posted February 28, 2010 Share Posted February 28, 2010 give this a try $testimony = ("include(".$_SERVER['DOCUMENT_ROOT']."/page/testimony/customer_testomonies.php)") you might have to use eval to get the string variable $testimony to be treated as code though... http://uk3.php.net/manual/en/function.eval.php Quote Link to comment https://forums.phpfreaks.com/topic/193612-making-a-variable-php-code/#findComment-1019180 Share on other sites More sharing options...
cinos11 Posted February 28, 2010 Author Share Posted February 28, 2010 At above when i tried your code i get the error: Parse error: syntax error, unexpected T_VARIABLE in C:\Server\www\myserver.dev\vhost\AquaDrilling\public\page.php on line 3 EDIT: Also because you added eval to your post, how would i add it to my code of $testimony = ("include(".$_SERVER['DOCUMENT_ROOT']."/page/testimony/customer_testomonies.php)") Quote Link to comment https://forums.phpfreaks.com/topic/193612-making-a-variable-php-code/#findComment-1019182 Share on other sites More sharing options...
smerny Posted February 28, 2010 Share Posted February 28, 2010 At above when i tried your code i get the error: Parse error: syntax error, unexpected T_VARIABLE in C:\Server\www\myserver.dev\vhost\AquaDrilling\public\page.php on line 3 error was on line 2 before.. did you forget the semi-colon or something? Quote Link to comment https://forums.phpfreaks.com/topic/193612-making-a-variable-php-code/#findComment-1019183 Share on other sites More sharing options...
cinos11 Posted February 28, 2010 Author Share Posted February 28, 2010 Okay i got $testimony = ("include(".$_SERVER['DOCUMENT_ROOT']."/page/testimony/customer_testomonies.php)"); not to give an error when the page loads BUT the included content using <?php $testimony ?> does not include it into the webpage Quote Link to comment https://forums.phpfreaks.com/topic/193612-making-a-variable-php-code/#findComment-1019186 Share on other sites More sharing options...
smerny Posted February 28, 2010 Share Posted February 28, 2010 EDIT: Also because you added eval to your post, how would i add it to my code of $testimony = ("include(".$_SERVER['DOCUMENT_ROOT']."/page/testimony/customer_testomonies.php)") the change would be done here <?php $testimony ?> use eval on $testimony... see if that works Quote Link to comment https://forums.phpfreaks.com/topic/193612-making-a-variable-php-code/#findComment-1019187 Share on other sites More sharing options...
PFMaBiSmAd Posted February 28, 2010 Share Posted February 28, 2010 What you are doing in not clear and what you have shown makes no sense (it also violates one of the prime rules of programming, the separation of code and data. Your data should not contain code.) You can use a variable as the argument that the include statement operates on and the code (the include) does not need to be part of the data. Quote Link to comment https://forums.phpfreaks.com/topic/193612-making-a-variable-php-code/#findComment-1019188 Share on other sites More sharing options...
cinos11 Posted February 28, 2010 Author Share Posted February 28, 2010 I did this wrong and i got an error <?php eval("$testimony";"); ?> how do i change the code up to work Quote Link to comment https://forums.phpfreaks.com/topic/193612-making-a-variable-php-code/#findComment-1019190 Share on other sites More sharing options...
cinos11 Posted February 28, 2010 Author Share Posted February 28, 2010 What you are doing in not clear and what you have shown makes no sense (it also violates one of the prime rules of programming, the separation of code and data. Your data should not contain code.) You can use a variable as the argument that the include statement operates on and the code (the include) does not need to be part of the data. This doesn't make very much sense. How am i going to include something if the include does not need to be part of the data. Quote Link to comment https://forums.phpfreaks.com/topic/193612-making-a-variable-php-code/#findComment-1019192 Share on other sites More sharing options...
smerny Posted February 28, 2010 Share Posted February 28, 2010 What you are doing in not clear and what you have shown makes no sense (it also violates one of the prime rules of programming, the separation of code and data. Your data should not contain code.) You can use a variable as the argument that the include statement operates on and the code (the include) does not need to be part of the data. he means changing: $testimony = ("include(".$_SERVER['DOCUMENT_ROOT']."/page/testimony/customer_testomonies.php)"); to: $testimony = ($_SERVER['DOCUMENT_ROOT']."/page/testimony/customer_testomonies.php"); and changing: <?php $testimony ?> to: <?php include($testimony); ?> Quote Link to comment https://forums.phpfreaks.com/topic/193612-making-a-variable-php-code/#findComment-1019193 Share on other sites More sharing options...
cinos11 Posted February 28, 2010 Author Share Posted February 28, 2010 What you are doing in not clear and what you have shown makes no sense (it also violates one of the prime rules of programming, the separation of code and data. Your data should not contain code.) You can use a variable as the argument that the include statement operates on and the code (the include) does not need to be part of the data. he means changing: $testimony = ("include(".$_SERVER['DOCUMENT_ROOT']."/page/testimony/customer_testomonies.php)"); to: $testimony = ($_SERVER['DOCUMENT_ROOT']."/page/testimony/customer_testomonies.php"); and changing: <?php $testimony ?> to: <?php include($testimony); ?> Doing that just gives me these errors: Warning: include() [function.include]: Filename cannot be empty in C:\Server\www\myserver.dev\vhost\AquaDrilling\public\inc\body\footer.php on line 16 Warning: include() [function.include]: Failed opening '' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\Server\www\myserver.dev\vhost\AquaDrilling\public\inc\body\footer.php on line 16 Quote Link to comment https://forums.phpfreaks.com/topic/193612-making-a-variable-php-code/#findComment-1019194 Share on other sites More sharing options...
cinos11 Posted February 28, 2010 Author Share Posted February 28, 2010 If this simplifies it here is what i need to do: I need the variable to be able to have the code used to include a file into a webpage Then the other page will now just put in the variable and have the content included. Quote Link to comment https://forums.phpfreaks.com/topic/193612-making-a-variable-php-code/#findComment-1019197 Share on other sites More sharing options...
cinos11 Posted February 28, 2010 Author Share Posted February 28, 2010 Okay nevermind. I found the answer using the suggestions provided Thanks for the all the help Quote Link to comment https://forums.phpfreaks.com/topic/193612-making-a-variable-php-code/#findComment-1019204 Share on other sites More sharing options...
PFMaBiSmAd Posted February 28, 2010 Share Posted February 28, 2010 Doing that just gives me these errors: Yes, did you read them, because they tell you what the problem is. The filename (in $testimony) is empty. Wherever you are defining $testimony at is not in the same program scope as the php code that is using $testimony. Quote Link to comment https://forums.phpfreaks.com/topic/193612-making-a-variable-php-code/#findComment-1019205 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.