Reputation: 83
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?
Upvotes: 3
Views: 134
Reputation: 5728
**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
Upvotes: 0
Reputation: 83
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?
Upvotes: 0
Reputation: 8227
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';
}
Upvotes: 4
Reputation: 41958
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.
Upvotes: 7
Reputation: 23483
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';
}
Upvotes: 0
Reputation: 12117
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;
}
Upvotes: 0