【发布时间】: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