For those new to PHP, it may be difficult to learn knowledge about PHP function nesting. For example, when some friends are practicing issues related to PHP function nesting, they will encounter errors when calling internal functions.
Then this article will give you a detailed analysis and introductionabout the processing of special functions and nested functions in PHP.
Below we will give you a detailed explanation through specific code examples.
First we create a PHP nested function codeThe example is as follows:
<?php function functionName1() { function functionName2(){ echo "我是一个被嵌套的函数"; } echo "我是一个函数"; } functionName2();
The results of this code we access and test through the browser are as follows:
As shown in the figure, an error message that the functionName2 function is not defined appears. How to deal with this situation?
Based on the above code, we will call the functionName1 function. The code is as follows:
<?php function functionName1() { function functionName2(){ echo "我是一个被嵌套的函数"; } echo "我是一个函数"; } functionName1(); functionName2();
We will then pass the browser test and the result will be as shown below:
At this time, the internal function functionName2 can be called successfully.
So what happens if we call external functions multiple times on a page?
Based on the above code, we will call the functionName1 function twice. The code is as follows:<?php function functionName1() { function functionName2(){ echo "我是一个被嵌套的函数"; } echo "我是一个函数"; } functionName1(); functionName1(); echo "<br>"; functionName2();
<?php // 当外部的函数没有被调用时,内部的函数相当于不存在 function functionName1() { if (!function_exists('functionName2')){ function functionName2(){ echo "我是一个被嵌套的函数"; } } echo "我是一个函数"; } functionName1(); functionName1(); echo "<br>"; functionName2();
1. When the external function is not called, the internal function is equivalent to not existing, otherwise If you call it directly, the above function is not defined error will occur.
2. When calling multiple external functions, you need to use a judgment statement to declare the nested function, otherwise there will be an error that cannot be redefined.
Note: function_exists() can determine whether the specified function is defined.
If you want to know more about PHP, you can follow the PHP Chinese websitePHP Video Tutorial. Welcome everyone to refer to and study!
The above is the detailed content of How to solve the problem of error reporting when nested functions are called in PHP? (Pictures + Videos). For more information, please follow other related articles on the PHP Chinese website!