【问题标题】:How to use an element in an array to print a textfile in PHP [duplicate]如何使用数组中的元素在PHP中打印文本文件[重复]
【发布时间】:2019-04-23 15:40:43
【问题描述】:

我是 PHP 代码的初学者,我正在尝试弄清楚如何在 file_get_contents 中使用变量。

我有一组与某个文件夹中的文本文件相关联的名称。我正在尝试使用数组中的名称来打印出文本文件。


echo '<p>';  
echo nl2br(file_get_contents( "Files/"+$names[1]+".txt"));
echo '</p>';

【问题讨论】:

  • 注意:如果 php-only,最好省略结束 php 标签 (?&gt;)
  • PHP 中的连接运算符是.,而不是+ - 请参阅string operators
  • 感谢您的帮助

标签: php html


【解决方案1】:

为了将 php 变量与字符串连接起来,您需要使用点运算符。在您的情况下,第二行代码应该是:

echo nl2br(file_get_contents( "Files/" . $names[1] . ".txt"));

【讨论】:

    猜你喜欢
    • 2014-09-16
    • 2021-05-03
    • 2012-06-09
    • 1970-01-01
    • 1970-01-01
    • 2021-03-11
    • 1970-01-01
    • 2012-10-24
    • 2016-08-25
    相关资源
    最近更新 更多