You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP主文件底部引入函数文件后调用函数失败问题咨询

问题原因与解决办法

你的代码示例

main.php

$x = 75;
$y = 25;

addition();
echo $z;

//include file in bottom of main page
include 'include_call.php';

include_call.php

function addition() {
    $GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}

报错原因

PHP是顺序执行的代码,从上到下逐行解析运行。你现在的写法里,调用addition()的代码在include语句之前——这时候包含函数的文件还没被引入,addition()函数根本不存在,自然会触发“未定义函数”的错误。

解决办法

不是不能在主文件底部引入包含函数的文件,而是必须在引入文件之后,再调用其中的函数。两种调整方式:

  1. 把include移到函数调用之前(也就是你说的放顶部的写法):
$x = 75;
$y = 25;

include 'include_call.php';

addition();
echo $z;
  1. 保持include在底部,但把函数调用移到include之后:
$x = 75;
$y = 25;

include 'include_call.php';

addition();
echo $z;

只要保证函数定义(通过include引入)的动作发生在函数调用之前,代码就能正常运行。

内容的提问来源于stack exchange,提问作者AMit G.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 12:52:11