I have a php echo code inside a variable I want to echo from another page. I can't seem to echo the "$list_of_teachers" without having it disappear or having it read the code itself.

Here's what I have:

====================

picture-gallery.php

//A picture gallery with variables

$teacher = "Henderson";
$title = "Third Grade Class From Mrs Henderson";
$html = "
<img src='student1.jpg'>
<img src='student2.jpg'>
<br />
Right before lunch and recess
<br />
Email <?php echo \$list_of_teachers[$teacher];?>
";

====================

template.php

//universal template for all galleries

echo "$title <br /> $html";
echo "$link"; //pull data from an array

====================

My Arrays

$list_of_teachers[Henderson] = "henderson@henderson.com";
$list_of_teachers[Smith] = "smith@smith.com";

    I think what you want is:

    $html = "
    <img src='student1.jpg'>
    <img src='student2.jpg'>
    <br />
    Right before lunch and recess
    <br />
    Email {$list_of_teachers[$teacher]}
    ";
    

      Thanks Nogdog for the reply. I have tried it and the variable disappeared.

        curb wrote:

        Thanks Nogdog for the reply. I have tried it and the variable disappeared.

        Where do you set it?

          Hm that was weird. It started working when I replaced the {} with . Thanks NogDog.

            What I found when saving a bunch of code to a $var and then writing that $var to a file (file.php) that the PHP code would not execute when called. Turns out the question mark written by php has a different ascii value then the one that triggers the PHP interpreter. So I had to use sprintf("%c", 63) to write the correct question mark so the PHP code will work.

            <?php
            	// Hex Value of ? so new page will execute php.
            	$WritesToFile = '<' . sprintf("%c", 63) . 'php 
            	echo "Hello World"; ' 
            	. sprintf("%c", 63) . '>';
            ?>

            So using the above code and pretending before its done $WritesToFile is written to a file called (file.php) If you load file.php it will echo "Hello World".

            Hope this helps.

            NM i see you got it working. Maybe this info will help someone else.

              13 days later

              I'm running into this problem again but I don't know why it's not working. I tried \$var and {$var} but it either prints the variable or doesn't echo.

              If you view the little code below, the variable $link prints out "<?php echo $school_links;?>" and not the school URL

              VARS.PHP

              //school vars
                $school_links = "http://www.williamgreen.com/";
              

              MAIN.PHP

              <?php
                include('vars.php');
              
                //link to the specific school website
                $link = '<' . sprintf("%c", 63) . 'php echo $school_links; ' . sprintf("%c", 63) . '>';
              
                //link sentence
                $letter_template = "If you would like to visit our school website, please visit $link";
              
                include('website_template.php');
              ?>

              WEBSITE_TEMPLATE.PHP

              <?php
                echo "Title of the school";
                echo "$letter_template";
                echo "Header of the school";
              ?>
              

              Can a mod remove the '[RESOLVED]' on the thread title? Is there a way I can do it myself?

                let's have a look at your MAIN code:

                <?php
                // OK, but make sure this is the correct path from the page which is loaded
                  include('vars.php');
                
                  //link to the specific school website
                // There really is not need whatsoever for all this sprintf
                // The echo is absolutely out of place, as you do NOT wish to echo
                // You are in the middle of a string build statement, you cannot use the ;
                //  $link = '<' . sprintf("%c", 63) . 'php echo $school_links; ' . sprintf("%c", 63) . '>';
                
                $link = "<a href=\"".$school_links."\">".$school_links."</a>";  
                
                  //link sentence
                  $letter_template = "If you would like to visit our school website, please visit $link";
                
                  include('website_template.php');
                ?> 
                
                
                  Write a Reply...