3

I have this script in php:

if($_SERVER['REQUEST_METHOD'] == 'POST'){

if(isset($_POST['data_id']) && $_POST['data_id'] != NULL){

  $data = $_POST['data_id'];
  DoConfig($data);

  function DoConfig($param_data){
    echo $param_data;
  }    
}

}else{
echo '0';
}

I don't understand why I'm getting an error Call to an undefined function, how can I fix it?

2
  • 3
    You are calling DoConfig function before it is created. Commented Jan 13, 2014 at 17:53
  • Its worth noting that had the function DoConfig() been declared outside the conditional block, it would have worked fine, even 'calling' it before declaring it. ideone.com/9ORbXv Commented Jan 13, 2014 at 17:58

6 Answers 6

7

PHP is executed sequentially - declare the function before using it and you'll be fine.

To elaborate - in PHP the entire file is loaded, and parsed based on scopes. If the function was at the end of the global scope this would work because at that point the global scope was evaluated before the subscope of the conditional was entered. Since you are entering a subscope with the if, the same evaluation order applies - the function needs to be evaluated before being used in its current scope.

Sign up to request clarification or add additional context in comments.

3 Comments

I might be wrong, but your answer might be half correct. If the function was declared outside and after the if, it would work... The file is initially parsed and if the function is outside the if it would be available at anytime.
No you're correct Jose - This answer is misleading. It doesn't work because the function is conditionally defined. If the function was declared in the global scope, it could be invoked anywhere, even at a position in the file before it is defined.
In this case, accidentally, yes, because the outer scope is completely evaluated (including declarations) before the inner scope is executed. Elaborated a bit to clarify this PHP peculiarity.
4

Your code is failing because the function is declared inside your if() loop and after it is called. You could move it outside of the if() and still leave it at the bottom of the script, but best practice dictates otherwise.

Declare your functions before you use them, and outside of any conditionals or loops; preferably in a separate file or in the very least at the very top of the script. For example:

function DoConfig($param_data) {
    echo $param_data;
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (isset($_POST['data_id']) && $_POST['data_id'] != NULL) {
        $data = $_POST['data_id'];
        DoConfig($data);
    }
} else {
    echo '0';
}

4 Comments

You can perfectly well declare a function inside a conditional.
I've just discovered this myself. You learn something new everyday.
This is exactly the right answer. My +1 to you ! Functions should be outside conditionals.
In the case of the function being outside the conditional, it can be declared after called. ideone.com/9ORbXv
0

please put DoConfig function outside if-else condition

if($_SERVER['REQUEST_METHOD'] == 'POST'){

    if(isset($_POST['data_id']) && $_POST['data_id'] != NULL){

     $data = $_POST['data_id'];
     DoConfig($data);

    }

 }else{
  echo '0';
 }
 function DoConfig($param_data){
   echo $param_data;
 }

Comments

0

You need to declare your function before you call it. Do:

function DoConfig($param_data){
echo $param_data;
}

if($_SERVER['REQUEST_METHOD'] == 'POST'){

if(isset($_POST['data_id']) && $_POST['data_id'] != NULL){

$data = $_POST['data_id'];
DoConfig($data);



}

}else{
echo '0';
}

Comments

0

Well, tell me more.. in my real project, inside the function DoConfig i have a insert into the DataBase in PDO, so if i put these function outside the ''if's'' , have problems into the security?

Comments

0

**Creating function inside if statement is not a best practice ** because that will be called if condition is true else will give undefined error in case you call that function later on. Also you called your function before it is even created thats why giving undefined error.

So better to create function outside if statement and run it anywhere.

//Creating function first and then calling it afterwards
function DoConfig($param_data)
{
            echo $param_data;
}

if($_SERVER['REQUEST_METHOD'] == 'POST')
{

        if(isset($_POST['data_id']) && $_POST['data_id'] != NULL){

          $data = $_POST['data_id'];
          //Function call
          DoConfig($data);
        }
}else
{
echo '0';
}

EDITED as per your answer: There is absolutely no problem with security if you create it outside if statement, use functions when needed.

Alternatively for future purpose if you are creating function inside if statement then use function_exist method later on so that you don't get undefined error https://www.php.net/function_exists

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.