Call to a undefined function, why? - php

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?

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.

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';
}

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;
}

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';
}

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?

*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
http://in1.php.net/function_exists

Related

PHP user-defined function issue with if clause

I have created this emptyFields function in my functions.php file and I am using require_once to require functions.php in my validation.php file.
function emptyFields($n, $e, $p, $cp) {
$r;
if ( empty($n) || empty($e) || empty($p) || empty($cp) ) {
$r = true;
} else {
$r = false;
}
return $r;
}
Now, I am using this if clause in the validation.php file to check for any empty fields.
if (emptyFields($name, $email, $pwd, $cpwd) !== false) {
header("location: signup.php?errorid=emptyfields");
exit();
}
Note: Before forwarding input to the emptyFields function, I also use the inputSanitize function which is as follows.
function inputSanitize($n) {
$n = addslashes($n);
$n = htmlspecialchars($n);
$n = trim($n);
}
Apart from the above if clause, I also have other if clauses for email validation, password match etc.
THE PROBLEM: No matter what, It always executes this emptyFields function even when the input is properly provided. The next if clause in line, checks email format using another function, but it never gets executed. From my signup.php file, I input data and I get this query-string so I am assuming that only the emptyFields function gets executed.
location: signup.php?errorid=emptyfields
I have tried using multiple elseif but nothing changes. I have checked the form inputs' names to see whether there is any error in taking input from form but everything seems fine there too. I am using XAMPP.
Any help on this will be greatly appreciated. Thank you.
try below instead of you own, remove the "else" condition which create the conditional tree and slow down when you execute alot of code.
function emptyFields($n, $e, $p, $cp) {
$r = false;
if ( empty($n) || empty($e) || empty($p) || empty($cp) ){
$r = true;
}
return $r;
}

PHP isset $_session equal to string

So guys I'm new to PHP OOP
I make Login with Roles, and make function(checkrole) for knowing what role it is.
This is how my function looks like
public static function hasadmin()
{
if(session_id() == '') {
session_start();
}
if(isset($_SESSION['role']) == 'A') {
return true;
}
}
and call it into navbar partial :
<?php if (helper::login() == true && helper::hasadmin() == true) { ?>
<li style="float:\right">Logout</li>
<li>Petugas</li>
<li>Laporan</li>
Function helper::login works perfectly.
Every time I login with another role the partial (petugas, laporan)
still comes out.
isset returns a boolean. Run the isset and check the actual value.
if(isset($_SESSION['role']) && $_SESSION['role'] == 'A') {

Calling functions from within functions (with arguments) in PHP

I’m building a system that captures info from a POST method and adds them into a PHP $_SESSION. The basic logic I want to follow is:
Check the method and call the relevant function
Check if $_SESSION data already exists via a function
Check if the $post_id variable is already in the $_SESSION's array via a function
Based on the outcomes on these functions, add to the array, create a new array, or do nothing
Here is the code I have written to handle this logic so far. I am looking to get just the add_to_lightbox() function working first, and will move onto the other two after.
session_start();
// set variables for the two things collected from the form
$post_id = $_POST['id'];
$method = $_POST['method'];
// set variable for our session data array: 'ids'
$session = $_SESSION['ids'];
if ($method == 'add') {
// add method
add_to_lightbox($post_id, $session);
} elseif ($method == 'remove') {
// remove method
remove_from_lightbox($post_id);
} else ($method == 'clear') {
// clear method
clear_lightbox();
}
function session_exists($session) {
if (array_key_exists('ids',$_SESSION) && !empty($session)) {
return true;
// the session exists
} else {
return false;
// the session does not exist
}
}
function variable_exists($post_id, $session) {
if (in_array($post_id, $session)) {
// we have the id in the array
return true;
} else {
// we don't have the id in the arary
return false;
}
}
function add_to_lightbox($post_id, $session) {
if (!session_exists($session) == true && variable_exists($post_id, $session) == false) {
// add the id to the array
array_push($session, $post_id);
var_dump($session);
} else {
// create a new array with our id in it
$session = [$post_id];
var_dump($session);
}
}
It's stuck in a state where it's always getting to add_to_lightbox() and following the array_push($session, $post_id); each time. I’m unsure whether this code I’ve written is possible because of the nested functions, and how I can refactor it to get the functionality working.
Correction from before, seems like $session is an array of ids..
The problem you are having is that you're modifying the local copy of that array within add_to_lightbox function. You don't need to specifically instantiate the variable as an array, you can just use the following.
$_SESSION['ids'][] = $post_id;

PHP What is wrong with this function call?

I have an existing method, which is part of Wordpress:
function process_checkout() {
global $wpdb, $wpdeals;
$validation = $wpdeals->validation();
...
Now I want to extend this a bit, should really be childs play, not sure what I am doing wrong?
function process_checkout() {
if (address_captured() == false)
{
echo '<script>alert("expected");</script>';
exit;
}
global $wpdb, $wpdeals;
$validation = $wpdeals->validation();
...
And (Simplified for testing)....
function address_captured()
{
return false;
}
Code stops executing around the if (address_captured() == false) line.
Can you spot anything out of the ordinary? Seems really 101 basics....
EDIT
It's not the exit statement either. Checked that too.

PHP function not working

<?php
$name = $_POST['name'];
$namecheck = namecheck($name);
function namecheck($name){
if(strlen($name)>0)
{
return TRUE;
}
else if(strlen($name)==0)
{
return FALSE;
}
};
function control($namecheck1)
{
if ($namecheck == TRUE)
{
echo "It is TRUE";
}
else if ($namecheck == FALSE)
{
echo "It is FALSE";
}
};
?>
I wrote that there is no problem in HTML part, there is a problem in my php functions because I am new in PHP. Can you make it proper.
I think you will understand what I want to do in the functions its simple Im trying to do if it is true I want to see "it is TRUE" in the screen. Else .....
Take a look at the variables. They don't match:
function control($namecheck1)
{
if ($namecheck == TRUE)
You also never actually invoke that function.
You're not calling your 'control' function. try starting with
$name = $_POST['name'];
$namecheck = namecheck($name);
control($namecheck);
Also, your definition of you function is wrong (or the variable you use is). You can change the function to this
function control($namecheck)
Of the if's to
if ($namecheck1 == TRUE)
in the end the name after control( is the one you should check for in the if's
In your control function, the parameter is called $namecheck1 at first, but you only call it $namecheck when you try to use it inside the function.
It looks as if you are not calling control function.
your are referencing $namecheck in the function "control" but the parameter passed is named $namecheck1. $namecheck in the scope of function "control" is undefined.
Some tips:
instead namecheck() you can use empty()
before using $_POST['name'] you should check if it exists isset() should help
This works fine
<?php
$name = $_REQUEST['name'];
function namecheck($name1)
{
if(!empty($name1))
{
return TRUE;
}
else
{
return FALSE;
}
}
if (namecheck($name) == TRUE)
{
echo "It is TRUE";
}
else if (namecheck($name) == FALSE)
{
echo "It is FALSE";
}
?>

Categories