在定义之前调用函数PHP

时间:2021-02-14 23:59:31

Is there any possible way when in one file - please note, just one file. To call a function when it isn't defined yet, e.g.

在一个文件中是否有任何可能的方法 - 请注意,只有一个文件。要在尚未定义的情况下调用函数,例如

<?php

echo global_title();

function global_title()
{
    $title = $_GET['name'];

    return $title;
}

?>

I don't know how to explain this, but it's not quite possible isn't it? What about variable from another file (without including it) can be called in a different file, e.g.

我不知道如何解释这个,但不是很可能不是吗?来自另一个文件(不包括它)的变量可以在不同的文件中调用,例如,

config.php

<?php

$db = "localhost";

?> 

index.php

<?php

// I do not want it to be accessed by including it or using sessions

echo $db;

?>

Know what I mean? :)

明白我的意思了吗? :)

3 个解决方案

#1


26  

You can call a function which is defined after calling it. That's because PHP first parses the file and then executes it.

您可以调用调用后定义的函数。那是因为PHP首先解析文件然后执行它。

As for the variable - this is not possible, you have to include the file.

至于变量 - 这是不可能的,你必须包含该文件。

#2


3  

I just discovered that you can call a function if it's defined later in the same file.
But if it's defined in an other file, you must include the file before calling the function.

我刚刚发现,如果稍后在同一个文件中定义了函数,则可以调用它。但如果它在另一个文件中定义,则必须在调用该函数之前包含该文件。

my_func();
function my_func() {...}
--->   No problem

but

my_func();
include_once 'define_my_func.php';
--->   PHP Fatal error

It's like a conditional function as in the example 2 on the doc on user-defined functions

它类似于条件函数,如关于用户定义函数的文档中的示例2

#3


1  

You cannot call undefined function, it will raise a fatal error. although in procedural code it can be called and afterwards defined. As the script is first parsed then executed. includes don't matter, they behave as if they were written in the exact file.

你不能调用未定义的函数,它会引发一个致命的错误。虽然在程序代码中它可以被调用,然后被定义。首先解析脚本然后执行。包括无关紧要,它们的行为就好像它们写在确切的文件中一样。

there's no such thing as a variable "from a file". if the code defines the variable is not run, it can't be there.

没有变量“来自文件”。如果代码定义变量未运行,则不能存在。

#1


26  

You can call a function which is defined after calling it. That's because PHP first parses the file and then executes it.

您可以调用调用后定义的函数。那是因为PHP首先解析文件然后执行它。

As for the variable - this is not possible, you have to include the file.

至于变量 - 这是不可能的,你必须包含该文件。

#2


3  

I just discovered that you can call a function if it's defined later in the same file.
But if it's defined in an other file, you must include the file before calling the function.

我刚刚发现,如果稍后在同一个文件中定义了函数,则可以调用它。但如果它在另一个文件中定义,则必须在调用该函数之前包含该文件。

my_func();
function my_func() {...}
--->   No problem

but

my_func();
include_once 'define_my_func.php';
--->   PHP Fatal error

It's like a conditional function as in the example 2 on the doc on user-defined functions

它类似于条件函数,如关于用户定义函数的文档中的示例2

#3


1  

You cannot call undefined function, it will raise a fatal error. although in procedural code it can be called and afterwards defined. As the script is first parsed then executed. includes don't matter, they behave as if they were written in the exact file.

你不能调用未定义的函数,它会引发一个致命的错误。虽然在程序代码中它可以被调用,然后被定义。首先解析脚本然后执行。包括无关紧要,它们的行为就好像它们写在确切的文件中一样。

there's no such thing as a variable "from a file". if the code defines the variable is not run, it can't be there.

没有变量“来自文件”。如果代码定义变量未运行,则不能存在。