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

太过爱你忘了你带给我的痛 2022-12-07 12:08 241阅读 0赞

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更快。 这些命令的措词如下:

  1. INCLUDE 'http://www.yoursite.com/path/to/file.php' ;
  2. //or
  3. 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”。

  1. //variables.php
  2. $name = 'Loretta';
  3. $age = '27';
  4. ?>

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

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

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

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才能在函数外使用它们。

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

更多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,然后查看调用它时会发生什么。

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

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

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

  1. //report.php
  2. //variables.txt $name = 'Loretta'; $age = '27';
  3. // 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:

  1. //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 条评论,241人围观)

还没有评论,来说两句吧...

相关阅读

    相关 PHP多线程文件操作

    因为项目中需要做一个在线视频抢看的功能,多人抢看,肯定需要阻塞处理,不然数据会出错,就像电商的秒杀一样,需要多数据进行加锁控制,PHP里面对文件有一个加锁的控制,防止多

    相关 PHP文件包含

    1.网站网页的分层设计,将不同的数据(一些公共的显示数据),整理出来,形成一个单独的文件。只需要在其他html代码界面中,需要用到的时候,直接包含(加载)。 ![这里写图片