我需要一个函数将以下php代码从一个php "变量页"(或其他简单文件)拉到我想要使用它的php文件。
我尝试了echo include,但是不能使它工作。
<?php
$option1 = 1;
$option2 = 2;
$option3 = 3;
$option4 = 4;
$option5 = 5;
?>
您可以使用require
或include
。或者,如果您想确保变量文件不会被包含多次,您可以使用require_once
或include_once
。
require "variables_page.php";
require
和include
的区别在于,如果找不到文件,require
会使脚本失败,而include
只会发出警告。通常你应该选择require
。
还要注意,require
和include
是语言结构,而不是函数,因此不需要用圆括号括住参数。
我建议你使用require_once("variables_page.php");
include函数的区别:
(http://www.w3schools.com/php/php_includes.asp)这两个函数在各方面都是相同的,除了它们的处理方式错误:
- include()生成一个警告,但是脚本将继续执行
- require()生成致命错误,脚本将停止
使用require_once或include_once如果你想确保文件只被包含一次。
use
require_once()
或
include_once()
功能。
参数-是文件名。如果文件夹相同,则使用
require_once( "file.php" )