Cannot redeclare php_1() previously declared in Error




642 Views | Rishi Kashyap | 1 FOLLOW
    X

    Cannot redeclare php_1() previously declared in Error


    0

    I got "Cannot redeclare php_1() previously declared in" error in few PHP Pages. The php_1() is a PHP function declared in a page named "ext_funct.php" . I was including this Page in other PHP pages where required. So if I wanted to declare the function in a PHP page, I would simply place it as require_once "ext_funct.php";
    The error "Cannot redeclare php_1() previously declared in" was shown simply because I was including "ext_funct.php" twice in a PHP page resulting in declaring the same PHP function php_1() twice. Of course I could have removed one include but the Page was including two separate pages each including the "ext_funct.php" which was essential for the script.

    The solution to avoid this error is PHP inbuilt function "function_exists(string)". PHP function_exists will return TRUE or FALSE depending on whether function is already declared or not. So rather than including the "ext_funct.php" directly I changed it to if(!function_exists(php_1)){require_once "ext_funct.php";} Thats it! Now the PHP would check if the php_1 function was declared or not ? If not declared then the PHP will include "ext_funct.php" else it will ignore this required file. Hope it helps someone ;-)

    Rishi Kashyap | 7 years ago | EDIT | REPLY



      tags  PHP TUTORIAL