How to solve the problem of error reporting when nested functions are called in PHP? (Pictures + Videos)

藏色散人
Release: 2018-09-17 17:30:16
Original
4160 people have browsed it

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();
Copy after login

The results of this code we access and test through the browser are as follows:

How to solve the problem of error reporting when nested functions are called in PHP? (Pictures + Videos)

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();
Copy after login

We will then pass the browser test and the result will be as shown below:

How to solve the problem of error reporting when nested functions are called in PHP? (Pictures + Videos)

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();
Copy after login

The test results of accessing through the browser are as follows:

How to solve the problem of error reporting when nested functions are called in PHP? (Pictures + Videos)

As shown in the figure, an error message appears that the functionName2 function cannot be redefined. How to solve and avoid such errors?

Here we need to use the if judgment statement, then the complete code example is as follows:

<?php
// 当外部的函数没有被调用时,内部的函数相当于不存在
function functionName1()
{
    if (!function_exists(&#39;functionName2&#39;)){
        function functionName2(){
            echo "我是一个被嵌套的函数";
        }
    }
    echo "我是一个函数";
}
functionName1();
functionName1();
echo "<br>";
functionName2();
Copy after login
Finally, the test results we access through the browser are as follows:

How to solve the problem of error reporting when nested functions are called in PHP? (Pictures + Videos)

To sum up, we can draw some important conclusions:

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 website

PHP 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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template