简体   繁体   English

在声明之前调用函数

[英]Calling a function before it is declared

The following code runs in PHP以下代码在 PHP 中运行

<?php

$foo = "Chocolate milkshake";

go($foo);

function go($param) {
    echo $param;
}

?>

// Output: chocolate milkshake

See this Demo http://codepad.viper-7.com/ToApZa看到这个演示http://codepad.viper-7.com/ToApZa

This code runs without errors and prints specified output, why?此代码运行没有错误并打印指定的输出,为什么? I thought this "function hoisting" only occurred in JavaScript我以为这种“函数提升”只发生在 JavaScript 中

It doesn't matter where you declare your functions in PHP in most cases, as you've just proved :)在大多数情况下,您在 PHP 中声明函数的位置并不重要,正如您刚刚证明的那样:)

Take a look at this page for more details.查看此页面了解更多详情。 The key point:关键点:

Functions need not be defined before they are referenced, except when a function is conditionally defined as shown in the two examples below.函数在被引用之前不需要定义,除非有条件地定义函数,如下两个示例所示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM