php 读写 外部 文件_在PHP中包含外部文件

php 读写 外部 文件

包含和要求 ( Include and Require )

PHP is capable of utilizing SSI to include an external file in the file being executed. Two commands that do this are INCLUDE () and REQUIRE (). The difference between them is that when placed within a false conditional statement, the INCLUDE is not pulled but the REQUIRE is pulled and ignored. This means that in a conditional statement, it is faster to use INCLUDE. These commands are phrased as follows:

PHP能够利用SSI在正在执行的文件中包含一个外部文件。 执行此操作的两个命令是INCLUDE()和REQUIRE()。 它们之间的区别在于,当放置在错误的条件语句中时 ,不会拉入INCLUDE,但是会拉出REQUIRE并忽略它。 这意味着在条件语句中,使用INCLUDE更快。 这些命令的措词如下:

INCLUDE 'http://www.yoursite.com/path/to/file.php' ;
//or
REQUIRE 'http://www.yoursite.com/path/to/file.php' ;

Some of the most common uses for these commands include holding variables that are used across multiple files or holding headers and footers. If an entire site's layout is housed in external files called with SSI, any changes to site design need only be made to these files and the entire site changes accordingly.

这些命令最常见的用法包括保存在多个文件中使用的变量或保存页眉和页脚。 如果整个站点的布局都位于用SSI调用的外部文件中,则只需对这些文件进行站点设计的任何更改,并且整个站点也会相应地更改。

拉文件 ( Pulling the File )

First, create a file that will hold the variables. For this example, it is called "variables.php."

首先,创建一个包含变量的文件。 在此示例中,它称为“ variables.php”。

//variables.php 
$name = 'Loretta';
$age = '27';
?>

Use this code to include the "variables.php" file in the second file called "report.php."

使用此代码在名为“ report.php”的第二个文件中包含“ variables.php”文件。

//report.php 
include 'variables.php';​// or you can use the full path; include 'http://www.yoursite.com/folder/folder2/variables.php';
print $name . " is my name and I am " . $age . " years old.";
?>

As you can see, the print command easily uses these variables. You can also call the include within a function, but the variables must be declared as GLOBAL  to use them outside the function.

如您所见,print命令可以轻松使用这些变量。 您还可以在函数内调用include,但是必须将变量声明为GLOBAL才能在函数外使用它们。

";​//The line below will work because $name is GLOBAL 
print "I like my name, " . $name;
print "
";​//The next line will NOT work because $age is NOT defined as global
print "I like being " . $age . " years old.";
?>

更多SSI ( More SSI )

The same commands can be used to include non-PHP files such as .html files or .txt files. First, change the variables.php file name to variables.txt and see what happens when it is called.

相同的命令可用于包括非PHP文件,例如.html文件或.txt文件。 首先,将variables.php文件名更改为variables.txt,然后查看调用它时会发生什么。

//variables.txt 
 $name = 'Loretta'; 
 $age = '27'; 
 ?> 
 //report.php 
 include 'variables.txt'; 
 // or you can use the full path; include 'http://www.yoursite.com/folder/folder2/variables.txt';
 print $name . " is my name and I am " . $age . " years old."; 
 ?> 

This works just fine. Basically, the server replaces the include ''; line with the code from the file, so it actually processes this:

这样很好。 基本上,服务器取代了include ''; 与文件中的代码保持一致,因此它实际上可以处理以下内容:

 //report.php
//variables.txt $name = 'Loretta'; $age = '27'; 
// or you can use the full path; include 'http://www.yoursite.com/folder/folder2/variables.txt 

print $name . " is my name and I am " . $age . " years old."; ?&gt
打印$ name。 “是我的名字,我是”。 $ age。 “岁。” ?&gt

It is important to note that even if you include a non.php file if your file contains PHP code you must have the tags, or it will not be processed as PHP. For example, our variables.txt file above included PHP tags. Try saving the file again without them and then run report.php:

重要的是要注意,即使您包含non.php文件(如果您的文件包含PHP代码)也必须具有标签,否则它将不会作为PHP处理。 例如,我们上面的variables.txt文件包含PHP标记。 尝试再次保存没有它们的文件,然后运行report.php:

 //variables.txt 

$name = 'Loretta';
$ name ='洛雷塔';
$age = '27';
$ age ='27';

This does not work. Since you need the tags anyway, and any code in a .txt file can be viewed from a browser (.php code cannot) just name your files with the .php extension to begin with.

这是行不通的。 由于仍然需要标签,因此可以从浏览器查看.txt文件中的任何代码(不能使用.php代码),因此只需以.php扩展名命名文件即可。

翻译自: https://www.thoughtco.com/including-external-files-in-php-2693792

php 读写 外部 文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值