Php code within php script

I hesitate to call it a Content Management System, but I have written a quick-and-dirty php app that I run on my local (Apache) server that serves my purposes except for one thing – I cannot generate the code for a variable name.

Here is what I am trying to generate:

<?php
$source={$sourceid};
include_once "../volunteer.php"
?>

If I leave the dollar sign of $source as is, my php CMS complains.
If I change the dollar sign to ascii equivalent (ampersand hash 36 semicolon) the generated html/imbedded php complains.

I even tried imbedding it in Heredoc. I also tried defining a php variable as $dollarsign = anscii value and concatenating onto the literal ‘source’.

What to do?

BTW My CMS has no problem with generating

<?php
include_once "..\\menu.php"
?>

grNadpa

Hi grNadpa, how are you doing?

I’m a little confused about what it is that you’re trying to do. Am I right in thinking that, rather than setting the value of $source directly, you want to set it to the value of another variable, the name of which is contained in $sourceid? Or am I barking up the wrong tree? How does $source actually get used in your included script?

Is $$sourceid or \$sourceid what you are after?


Yes. Here are some excerpts from my generator

<?php
/* generates html folder/files inserting "games" mySql database "substitution" table contents into template. */

function Build_Page($dbRow, $includepath){
    $sourceid = $dbRow['sourceid'];                            ************ here is the source id I want to use .... see below

    $h2 = $dbRow['h2'];
    $col1 = $dbRow['col1'];
<snip> removed code for brevity
    $title = $dbRow['title'];
    $description = $dbRow['description'];
    $keywords = $dbRow['keywords'];
<snip> removed code for brevity
$indexhtml = <<<HEREDOC
<!DOCTYPE html>
<html>
<head>
    <title>{$title}</title>
    <meta charset="utf-8" />
    <meta name="description" content="{$description}" />
    <meta name="keywords" content="{$keywords}" />
    <meta name="robots" content="index, follow" />
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" type="text/css" href="/threecol.css" media="screen" />
<snip> removed code for brevity
                <!-- Column 2 start -->
<?php
include_once "{$includepath}"  
?>
                <!-- Column 2 end -->
            </div>
            <div class="col3">
                <!-- Column 3 start -->
<?php
{$sourceVar};     *********************************** what I want to see is $source= whatever is in $sourceid (Which derived from $dbRow['sourceid'] )
include_once "../volunteer.php"
?>
</body>
</html>
HEREDOC;
return $indexhtml;
 } // end function Build_Page
	$currentHost = pathinfo($_SERVER['REMOTE_ADDR'],PATHINFO_BASENAME);
    if($currentHost == "127.0.0.1" OR $currentHost == "::1"){   // no place like home
<snip> removed code for brevity
    }

    try {
        $conn = new PDO('mysql:host=localhost;dbname=games', $theUsername, $thePassword);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $data = $conn->query('SELECT * FROM substitution where generate');
 
        foreach($data as $row) {
            $gamepath = dirname(__FILE__) . DIRECTORY_SEPARATOR;
            $includepath = "menu.php";
            if(strlen($row['folder']) > 0) {
                $gamepath .= $row['folder'] . DIRECTORY_SEPARATOR;
                $includepath = '..' . DIRECTORY_SEPARATOR . $includepath;
            } // end if folder
            $gamepath .=  $row['filename'];
            $gamePage = Build_Page($row, $includepath);
            echo("<br />generated: {$gamepath}");
            if (strlen($row['postgen']) > 0) {
                echo (" ==> " . $row['postgen']);
            } // end if postegen
            file_put_contents($gamepath, $gamePage);
        } // end foreach
    } // end try
    catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    } // end catch   
?>
 

If the $dbRow[‘sourceid’] is “makeasandwich”, I want the generated code to be

<?php
$source="makesandwich";
include_once "../volunteer.php"
?>

Here is the contents of “…/volunteer.php”. Note the the source is an argument on the link

<?php
$vList = <<<HEREDOC
                <h3>Want to Help?</h3>
                <fieldset id="volunteerset">
                <legend>Volunteer</legend>
                <ul id="volunteerul">                
                    <li><a href="/volunteer/translate.php?source={$source}">Translate</a> one or more of these games into another language</li>
                    <li><a href="/volunteer/newgame.php?source={$source}">Suggest</a> a game on horseback</li>
                    <li><a href="/volunteer/wordsmith.php?source={$source}">Wordsmithing</a></li>
                    <li>Site <a href="/volunteer/appearance.php?source={$source}">Graphics, Layout, Design</a></li>
                    <li><a href="/volunteer/technical.php?source={$source}">Technical</a>Programming, SEO, Mobile</li>
                    </ul>
                </fieldset> <!-- volunteer -->
HEREDOC;
echo($vList);
?>

So you’re trying to print-literal $source. That would be echo ‘$source’ or echo “\$source” or you could escape the php temporarily to push a literal HTML string
?>
$source = <?php echo $source;

(bit sloppier imo, but whatever floats your boat.

Is either:

highlight_file()

or

highlight_string()

what you’re looking for?

“\$source” Floated my boat. Onto the next capsize.

Thanks