PHP 包含函数后跟回显

PHP include function followed by echo

在此 Whosebug 讨论中,一位用户告诉我,我可以从外部访问 php,其中包含我目前在同一个 .php 文件中拥有的代码部分.
这是我的 file1.php:

     <?php
    $commentsLabels = array('First Signal','Second Signal');
    $commentsTexts = array('First comment for #signal1 id - it will open in a fancybox.','Second comment for #signal2 id - it will open in a fancybox.');       

    $counter = 0; 

     ?>

<!--GENERATING LABELS-->
<div  class="signals"> 
    <ul>

    <?php
    foreach ($commentsLabels as $commentLabel) {
        $counter++;
    ?>
            <li><a href="#signal<?php echo $counter; ?>" class="fancybox"><?php echo $commentLabel; ?></a></li> 
    <?php
        } //loop ends       
    ?>  

    </ul>
</div>

<!--GENERATING POPUPS-->
<?php
    $counter = 0; //reset counter

    foreach ($commentsTexts as $commentText) { //loop starts
        $counter++; 
?>
        <div id="signal<?php echo $counter; ?>" style="display:none;"><p style="color:#fff"><?php echo $commentText; ?></p></div>
<?php
    } //loop ends
?>

我试图进入 "file2.php" 创建我的标签和文本的代码部分:

<?php
    $commentsLabels = array('First Signal','Second Signal');
        $commentsTexts = array('First comment for #signal1 id - it will open in a fancybox.','Second comment for #signal2 id - it will open in a fancybox.');       
?>   

然后我创建了一个包含函数,然后将 echo $variable 放入 file1.php:

<li><a href="#signal<?php echo $counter; ?>" class="fancybox"><?php include 'file2.php'; echo $commentLabel; ?></a></li> 

<div id="signal<?php echo $counter; ?>" style="display:none;"><p style="color:#fff"><?php include 'file2.php'; echo $commentText; ?></p></div>

当我尝试时,它什么也没显示:你能告诉我为什么 include 函数不起作用吗?

试试这个:

File2.php

<?php
     $commentsLabels = array('First Signal','Second Signal');
     $commentsTexts = array('First comment for #signal1 id - it will   open in a fancybox.',
                     'Second comment for #signal2 id - it will open in a fancybox.');       
?>  

File1.php

<?php
     include 'file2.php';
     $counter = 0;
?>
<!--GENERATING LABELS-->
<div  class="signals"> 
  <ul>

  <?php
       foreach ($commentsLabels as $commentLabel) {
       $counter++;
  ?>
     <li><a href="#signal<?php echo $counter; ?>" class="fancybox"><?php    echo $commentLabel; ?></a></li> 
  <?php } ?>  
  </ul>
</div>

<!--GENERATING POPUPS-->
<?php
    $counter = 0; //reset counter

    foreach ($commentsTexts as $commentText) { //loop starts
       $counter++; 
?>
    <div id="signal<?php echo $counter; ?>" style="display:none;"><p style="color:#fff"><?php echo $commentText; ?></p></div>
<?php } ?>