incompatible variable with file_get_contents

Seem like file_get_contents does not work with variable.

I created two files.
I want trying to use the variable from the VariableInsideFile.txt file but I don't want to use the include function.

//FIILE 1     VariableInsideFile.txt
   $Mokey

//File 2 Do_VariableInsideFile.php
<?php

$Money="Can't equal to happy";
$CallOtherVariable=file_get_contents("VariableInsideFile.txt");
echo $CallOtherVariable;
?>

The result should be
Can't equal to happy"

but this is what i got with my script.
$Money

if I write the whole script withing using file_get_contents by using the include and use the variable directly on the same page then I don't see any problem.

Like below

<?php
$Money="Can't equal to happy";
$CallOtherVariable="$Money";
echo $CallOtherVariable;
?>

So my question is how to use the variable inside the txt file.



jetleestyeAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
nizsmoConnect With a Mentor DeveloperCommented:
I think this is what you are looking for :)
<?php
 
$Money="Can't equal to happy";
$CallOtherVariable=file_get_contents("VariableInsideFile.txt");
eval("echo $CallOtherVariable;");
 
?>

Open in new window

0
 
Steve BinkCommented:
This is a bad idea, IMO, but you can use the eval() function to interpret the file input.  Make sure that file is protected against unauthorized edits!

http://us2.php.net/manual/en/function.eval.php
0
 
jetleestyeAuthor Commented:
How come the variable can't be assign in the eval function.
syntax error, unexpected

<?php
$Money="Can't equal to happy";
 
$CallOtherVariable=file_get_contents("VariableInsideFile.txt");
eval("$Money= $CallOtherVariable;");
echo $Money;
 
?>
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
glcumminsConnect With a Mentor Commented:
Well, consider what you are doing in that last bit of code. $CallOtherVariable will contain the string '$Money' after you call 'file_get_contents("VariableInsideFile.txt").

So if your method worked, this line:

eval("$Money= $CallOtherVariable;");

Would be evaluated to:

 $Money= $Money

That wouldn't make any sense.
0
 
glcumminsCommented:
If you explain more fully your intentions, perhaps we can help you devise a better method that doesn't require the use of a messy eval() call.
0
 
jetleestyeAuthor Commented:
I am trying to build a multilingual page.
The .txt file will help translator to translate more easily than reading the messy codes.
0
 
jetleestyeAuthor Commented:
If I write my script like this

<?php
$Money="Can't equal to happy";
 
$CallOtherVariable=file_get_contents("VariableInsideFile.txt");
eval("$Get_Money= $CallOtherVariable;");
echo $Get_Money;
?>
Notice: Undefined variable: Get_Money in on line 5
Parse error: syntax error, unexpected '=' in  : eval()'d code on line 1
Notice: Undefined variable: Get_Money
0
 
Steve BinkCommented:
See below.

I'm with glcummins.  This is a clumsy and dangerous method.  If anyone is able to edit that file, they could run just about any code they wanted.  Explain your need and strategy, and perhaps we can come up with a better way for this to get done.


<?
//inside money.php
$money="my money";
?>
 
<?
$t = file_get_contents('money.php');
/* at this point, $t="<?\n//inside money.php\n\$money=\"my money\";\n?>"
 
echo $t,'-',$money,'-';
/* output:
<?
//inside money.php
$money="my money";
?>--
*/
 
eval($t);
echo $t,'-',$money,'-';
/* output:
<?
//inside money.php
$money="my money";
?>-my money-
*/
?>

Open in new window

0
 
glcumminsCommented:
How about something like this:
<?php
 
$language = 'english';
 
include ('languages/$language.php');
 
echo $Money;
 
?>
 
The file 'languages/english.php' contains:
 
<?php
 
$Money = "Can't equal to happy.";
 
?>
 
Each additional language just has a place for the translators to edit the necessary variables. No code will be included in the language files other than simple variables and text.
 
?>

Open in new window

0
 
glcumminsCommented:
It may also be helpful to examine how larger projects (such as osCommerce) have already solved this problem. osCommerce uses a method very similar to what I described above, but the individual language files define constants instead of variables:

From 'languages/english/index.php':
  define('HEADING_TITLE', 'Let\'s See What We Have Here');
  define('TABLE_HEADING_IMAGE', '');
  define('TABLE_HEADING_MODEL', 'Model');
  define('TABLE_HEADING_PRODUCTS', 'Product Name');
  define('TABLE_HEADING_MANUFACTURER', 'Manufacturer');
  define('TABLE_HEADING_QUANTITY', 'Quantity');
  define('TABLE_HEADING_PRICE', 'Price');
  define('TABLE_HEADING_WEIGHT', 'Weight');
  define('TABLE_HEADING_BUY_NOW', 'Buy Now');

Open in new window

0
 
jetleestyeAuthor Commented:

I still like my idea better but as you mension about the risk of being attack on the code.
I am kinda scared.

So  as long as we can figure out on how to turn the text into variable then I think .

The script that you wrote problaly will not work on my script.

For example.
This is your $CurryTry tried  tried out of  $MaximumAllow.
The script should output like this.
This is your 1st tried out of  9.



0
 
glcumminsCommented:
Sure, that would still work. main file, you will have:

echo "This is your $CurryTry tried out of $MaximumAllow.";

Then in your language file:
<?php
 
$CurryTried = "1st";
$MaximumAllow = "9";
 
?>

Open in new window

0
 
glcumminsCommented:
Although in this example, $CurryTry and $MaximumAllow look like dynamic values that you wouldn't really define in a semi-static language file. Independent language files are more suited to values that will not change.
0
 
jetleestyeAuthor Commented:
seem like your script is not working, it still output the whole thing.

I rewrite the code  but I having a problem
<?
 $money="my money";

$t = file_get_contents('VariableInsideFile.txt');

echo "$t";

/* output:
echo "<br> Seem like nothing display below here"
eval($t);
echo $t,'-',$money,'-';
/* output:

*/
?>
0
 
jetleestyeAuthor Commented:
Ths is the clear looking , but

<?php
 
$Money="Can't equal to happy";
$CallOtherVariable=file_get_contents("VariableInsideFile.txt");
$Testme= eval("echo $CallOtherVariable;");
//    I TESTED THE $Testme, the $Testme doesn't holding any value in it.
if(empty($Testme)){echo "eval can't turn into Variable ";}
 
?>
0
 
jetleestyeAuthor Commented:
and even this too

<?php
 
$Money="Can't equal to happy";
$CallOtherVariable=file_get_contents("VariableInsideFile.txt");
$Testme= eval("$CallOtherVariable;");
//    I TESTED THE $Testme, the $Testme doesn't holding any value in it.
if(empty($Testme)){echo "eval can't turn into Variable ";}
 
?>
0
 
Steve BinkCommented:
One more try with full commenting.  Remember that:

1) money.php is a separate PHP file on your server.  The ONLY thing in the file for this example is:
    <?
    $money="my money";
    ?>

2) eval() takes the parameter you pass it, and attempts to parse it as PHP.  It will return TRUE on success, FALSE on failure, unless you have used the return() statement in the code.  All actions executed as a result of the parsed eval() code will impact the current scope of execution.

3) "Include ('money.php');" will effectively do the same thing as "$t=get_file_contents('money.php');eval($t);", but with less risk.

4) "DECLARE (my_money,'my money');" will effectively do the same thing as "$t=get_file_contents('money.php');eval($t);" but with less risk, and 'my_money' will be available as a constant, not a regular variable.


<?
// REMEMBER: money.php is a separate file!
/*
  At the start, the following are true:
    $t === undefined/null
    $money === undefined/null
    $z === undefined/null
*/
 
$t = get_file_contents('money.php');
/*
  At this point, the following are true:
     $t === (string) "<?\n\$money=\"my money\";\n?>"
     $money === undefined/null
     $z === undefined/null
*/
 
echo "$t-$money-";
/*
  The previous line will echo this:
 
   <?
   $money="my money";
   ?>--
 
  Note the two dashes are printed at the end because $money is still undefined.  You should also see
  an error in your PHP log regarding that fact.
*/
 
$z = eval($t);
/*
  At this point, the following should be true:  
     $t === (string) "<?\n\$money=\"my money\";\n?>"
     $money === (string) "my money"
     $z === (boolean) true
 
  If PHP could not parse the string in $t, or if another error occurred, $z will be false.
*/
 
echo "$t-$money-";
/*
  The previous line should output the following:
 
     <?
     $money="my money";
     ?>-my money-
*/
?>

Open in new window

0
 
jetleestyeAuthor Commented:
It looks complicated to follow, let me try.
I think it would be easier if you just write what is in first page and what is in the second page.
Le me try
0
 
Steve BinkCommented:
Just five lines of code, including the line in money.php.  The idea is that whatever you send through eval() acts exactly like PHP code, just as if you had typed it there.  If you were to remove the eval() line, and copy/paste the contents of money.php into its place, you would have the same execution.

The concept itself is not difficult, but I think your strategy for approaching this is making it unnecessarily so.  Why do you feel it is necessary to have eval() execute the contents of what could easily be an include file?
0
 
jetleestyeAuthor Commented:
again, I am trying to build  a multilingual page using text file.
I got your point, I can't get the eval work like copy and paste.
Let me do more research on the eval thing here

0
 
jetleestyeAuthor Commented:
this would be a better example on how to code my script.
<?php
$string = 'cup';
$name = 'coffee';
$str = 'This is a $string with my $name in it.';
echo "$str.<br>";
eval("\$str = \"$str\";");
echo $str. "\n";
?>
0
 
jetleestyeAuthor Commented:
I got it
0
 
Steve BinkCommented:
Yes!  Exactly!

Still, you're going about it the hard way.  There's no reason to push all this through eval().  Using glcummins' previous input, you can use a series of constant declarations.  The new code would look like this:


<?
// LANG-EN.PHP
declare('coffee_string', "This is a cup with my coffee in it.");
// however many other declarations you have
?>
 
<?
switch ($language) {
  case 'FR': require ('lang-fr.php'); break;
  case 'DE': require("lang-de.php'); break;
  case 'EN':
  default: require("lang-en.php"); break;
}
echo coffee_string;
?>

Open in new window

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.