首页 > PHP文件直接返回一个数组是几个意思?

PHP文件直接返回一个数组是几个意思?

比如:

<?php
return array(
    'app' => __DIR__.'/../app',
    'public' => __DIR__.'/../public',
    'base' => __DIR__.'/..'
);

数组没有变量名,在require这个文件的地方怎么使用这个数组里的数据呢?


直接赋值

test.inc

<?php
return array(
    'appname' => 'app',
    'version' => '1.0'
);

test.php

<?php
$rtn_value = require('test.inc');
var_dump($rtn_value);

运行 test.php

array(2) {
  ["appname"]=>
  string(3) "app"
  ["version"]=>
  string(3) "1.0"
}

中文PHP手册中的解释

如果在全局范围中调用,则当前脚本文件中止运行。如果当前脚本文件是被 include 的或者 require 的,则控制交回调用文件。此外,如果当前脚本是被 include 的,则 return 的值会被当作 include 调用的返回值。如果在主脚本文件中调用 return,则脚本中止运行。

【热门文章】
【热门文章】