【问题标题】:PHP include function followed by echoPHP包含函数后跟回声
【发布时间】:2016-04-07 06:02:24
【问题描述】:

在这个stackoverflow 讨论中,一位用户告诉我,我可以从外部访问一个 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>

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

【问题讨论】:

    标签: php php-include


    【解决方案1】:

    试试这个:

    文件2.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.');       
    ?>  
    

    文件1.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 } ?>
    

    【讨论】:

    • 成功了!非常感谢您提供的解决方案以及您花费的时间
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-16
    • 2012-02-20
    • 2013-06-12
    • 2022-11-25
    • 1970-01-01
    • 1970-01-01
    • 2016-03-12
    相关资源
    最近更新 更多