在PHP中使用函数vs include作为“子例程” - php

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center获取指导。

7年前关闭。

推荐哪种方法?

使用包括:

// subroutine.php

echo 'hello '.$a;

// usage.php

$a = 'foo';
include 'subroutine.php';

使用功能:

// subroutine.php

function subroutine ($a)
{
    echo 'hello '.$a
}

// usage.php

include 'subroutine.php';
$a = 'foo';
subroutine($a);

因为从技术上来说都可以使用,并且因为PHP中不像ASP,所以没有“子例程”。模拟子例程的最佳方法是什么?

参考方案

功能更适合于此目的。 Includes更广泛地用于模板化或控制器调用视图时。

但是,您的函数不应回显串联的字符串。它应该返回它。

$a = "foo";
echo subroutine($a);

function subroutine($a) { return "hello " . $a; }

这使您的代码可测试。

PHP strtotime困境 - php

有人可以解释为什么这在我的服务器上输出为true吗?date_default_timezone_set('Europe/Bucharest'); var_dump( strtotime('29.03.2015 03:00', time()) === strtotime('29.03.2015 04:00�…

PHP-全局变量的性能和内存问题 - php

假设情况:我在php中运行一个复杂的站点,并且我使用了很多全局变量。我可以将变量存储在现有的全局范围内,例如$_REQUEST['userInfo'],$_REQUEST['foo']和$_REQUEST['bar']等,然后将许多不同的内容放入请求范围内(这将是适当的用法,因为这些数据指的是要求自…

php-casperjs获取内部文本 - php

我正在为casperjs使用php包装器-https://github.com/alwex/php-casperjs我正在网上自动化一些重复的工作,我需要访问一个项目的innerText,但是我尚不清楚如何从casperjs浏览器访问dom。我认为在js中我会var arr = document.querySelector('label.input…

PHP-将日期插入日期时间字段 - php

我已在数据库中使用datetime字段存储日期,使用PHP将“今天的日期”插入该字段的正确方法是什么?干杯, 参考方案 我认为您可以使用php date()函数

PHP-如何获取类的成员函数列表? - php

如果我知道班级的名字。有没有办法知道类的成员函数列表? 参考方案 get_class_methods()是你的朋友