English 中文(简体)
PHP 函数中将参考值用作默认参数
原标题:Using reference value as default param in PHP Function

我想这样做:

/* example filename: config_load.php */

$config_file = "c:path	ofile.php";

function read_config($file = &$config_file)
{
$settings = array();
$doc = new DOMDocument( 1.0 );
$doc->load($file); 
$xpath = new DOMXPath($doc); 
$all=$xpath->query( appSettings/add );
foreach ($all as $setting) {$settings[$setting->getAttribute( key )]=$setting->getAttribute( value );}

return $settings;
}

/* end config_load.php */

所以当我打电话给文件的时候, 它就像这样...

require_once "config_load.php";
// $config_file = "c:path	ofile2.php"; //could also do this
$config = read_config();

这样, 如果我不指定文件, 它就会读取默认配置文件 。 在调用函数之前, 我也可以在任何地方定义 $config_ file 。 无法调用配置_ load 文件的人不需要担心是否能够加载其它文件, 他们可以在调用读取_ config () 之前在任何地方定义它 。

最佳回答

这是不可能的:

默认值必须是不变表达式,而不是变量、类别成员或函数调用。

~http://www.php.net/manual/en/offices.arguments.php#formes.arguments.php#formations.arguments.default" rel=“nofollows”>http://www.php.net/manual/en/wices.arguments.php#forms.arguments.fault

然而,你可以像这样绕过它:

function read_config($file = false) {
    global $config_file;
    if ($file === false) $file = $config_file;

    $settings = array();
    $doc = new DOMDocument( 1.0 );
    $doc->load($file); 
    $xpath = new DOMXPath($doc); 
    $all=$xpath->query( appSettings/add );
    foreach ($all as $setting) {$settings[$setting->getAttribute( key )]=$setting->getAttribute( value );}

    return $settings;
}

或像这样 :

function read_config($file = false, $config_file = false) {
    if ($file === false && $config_file !== false) $file = $config_file;

    $settings = array();
    $doc = new DOMDocument( 1.0 );
    $doc->load($file); 
    $xpath = new DOMXPath($doc); 
    $all=$xpath->query( appSettings/add );
    foreach ($all as $setting) {$settings[$setting->getAttribute( key )]=$setting->getAttribute( value );}

    return $settings;
}
问题回答

您可以:

<?php

$greet = function()
{
   return "Hello";
};

$a = $greet();
echo $a;
?>

Read more here: http://php.net/manual/en/functions.anonymous.php





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签