note 92459 deleted from function.include by danbrown

From: Date: Sat, 25 Jul 2009 15:40:04 +0000
Subject: note 92459 deleted from function.include by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-158722@lists.php.net to get a copy of this message
Note Submitter: jsb17 at cornell dot edu ---- get_include_contents() scope: Assuming you need to process a nested file right now and display html from that nested file later, get_include_contents('myfile.php') works great if your nested file myfile.php is self-sufficient (needs no outside variables). However, if myfile.php relies on outside variables, using get_include_contents() is risky business, because you can NOT pass local variables to a script using get_include_contents(). Unfortunately, I see no absolute best solution for all occasions (please post if you discover otherwise). Assuming file2.php needs to access $address: Does NOT work: file1.php: <?php $address = "123 Main Street"; // file2.php can not access $address $html_content = get_include_contents("file2.php"); ... // Erratic output echo($html_content); ?> Also does NOT work: file1.php: <?php $address = "123 Main Street"; // Like include(), looks for literal file name, instead of script with parameters $html_content = get_include_contents("file2.php?address=" . urlencode($address)); ... // Empty output and no error message echo($html_content); ?> Also does NOT work: file1.php: <?php $address = "123 Main Street"; // Save evaluation for later $html_content = "include(\"file2.php\");"; ... $address="666 Wall Street"; ... // Incorrect output, assuming file2.php expected the Main Street address echo eval($html_content) ?> This DOES work (file1.php and file2.php displayed), but is highly discouraged, and usually impractical with a slew of html form variables or arrays file1.php: <?php $address = "123 Main Street"; // Save evaluation for later $html_content = get_include_contents("file2.php"); ... $address="666 Wall Street"; ... // Correct output, displays "123 Main Street" echo eval($html_content) ?> file2.php <?php // Works, but impractical // Adding a comment such as "// defined in file1.php" is a bad idea, // because a future programmer may call file2.php from file3.php, etc... // rendering your comment false global $address; echo $address; ?> This DOES work, and although it appears to be the best option, it is still high risk. file1.php: <?php $address = "123 Main Street"; // Save evaluation for later $html_content = "include(\"file2.php\");"; ... // Output is correct, assuming a future programmmmer or your future self // does not alter $address within the "...", as in the example immediately above echo eval($html_content) ?> This example DOES work, but requires world access to your directory and a redundant database connection in file2.php, and is also impractical for a slew of html form variables or arrays file1.php: <?php $address = "123 Main Street"; // Save evaluation for later $html_content = "include(\"http ://www.mywebsite.com/my_directory/file2.php?submit=" . urlencode($submit) . "\");"; ... $address = "666 Wall Street"; ... // Correct output, displays "123 Main Street" echo eval($html_content) ?>

« previous php.notes (#158722) next »