【发布时间】:2013-01-05 04:53:31
【问题描述】:
我必须在我的主页上打印这些新闻。 在我设置的 index.php 文件中,在下面包含此文件,但没有返回任何内容...错误在哪里? 该脚本从文件中获取消息。 然后它将每个新闻设置为一个字符串。 然后该函数为每个新闻创建一个“标准”回声 然后将新闻打印出来。
但是什么都没有打印出来……
谢谢大家!
<?php
if (file_exists("./public/ita/news/news.txt")) {
$getnews1 = "./public/ita/news/news.txt";
$news1 = file($getnews1); //file in to an array
}
if (file_exists("./public/ita/news/news2.txt")) {
$getnews2 = "./public/ita/news/news2.txt";
$news2 = file($getnews2); //file in to an array
}
if (file_exists("./public/ita/news/news3.txt")) {
$getnews3 = "./public/ita/news/news3.txt";
$news3 = file($getnews3); //file in to an array
}
if (file_exists("./public/ita/news/news4.txt")) {
$getnews4 = "./public/ita/news/news4.txt";
$news4 = file($getnews4); //file in to an array
}
if (file_exists("./public/ita/news/news5.txt")) {
$getnews5 = "./public/ita/news/news5.txt";
$news5 = file($getnews5); //file in to an array
}
function post_news()
{
echo '<div align="left" class="newstitle">';
echo $news[0];
echo '</div>';
echo '<div align="left" class="news">';
echo '<p></p>';
echo $news[1];
echo $news[2];
echo $news[3];
echo $news[4];
echo $news[5];
echo $news[6];
echo $news[7];
echo $news[8];
echo $news[9];
echo $news[10];
echo $news[11];
echo '</div>';
}
if($news1 != NULL) {
$news = $news1;
post_news();
}
if($news2 != NULL) {
$news = $news2;
post_news();
}
if($news3 != NULL) {
$news = $news3;
post_news();
}
?>
【问题讨论】:
-
$news1 与 $news[1] 不同;)
-
$news1 是文件新闻。 $news[1] 是该文件的第一行...不幸的是,这不是我的问题
-
您应该了解 PHP 中的变量范围.. 以及变量名.. 和类型!你还有一段路要走..
-
我在暗示一个解决方案,确实名称不同。 $news 在你的函数外部和内部是不同的。
-
@Perocat - 如果
$news1是文件行的数组,那么您使用$news1[0]访问第一行...您从未创建过名为$news的数组
标签: php file function include indexing