SenTisso
SenTisso

Reputation: 641

Using PHP function for shortening repeating code

My code in PHP is pretty long and I want to make it shorter with creating one function with different values and than I would just write one line with function name instead of many lines of code, but it doesn't seem to work.

This is that repeating code:

if (!isset($_POST['ID_user']) || empty($_POST['ID_user'])) {
 $_SESSION['ID_user_missing'] = "error";
 header("location: index.php");
} else {
   $ID_user = $_POST['ID_user'];
}


if (!isset($_POST['meta_name']) || empty($_POST['meta_name'])) {
 $_SESSION['meta_name_missing'] = "error";
 header("location: index.php");
} else {
   $meta_name = $_POST['ID_user'];
}


if (!isset($_POST['meta_value']) || empty($_POST['meta_value'])) {
 $_SESSION['meta_value_missing'] = "error";
 header("location: index.php");
} else {
   $meta_value = $_POST['meta_value'];
}

And this was the plan, instead of that code up ther, I would just have this down below:

function ifIssetPost($value) {
 if (!isset($_POST[$value]) || empty($_POST[$value])) {
 $_SESSION[$value.'_chybi'] = "error";
 header("location: index.php");
 } else {
   $$value = $_POST[$value];
 }
}

ifIssetPost('ID_user');
ifIssetPost('meta_name');
ifIssetPost('meta_value');

But it just doesn't work, when you try to echo for example variable $meta_name it shows that it's empty. Can you help me ? Thank you very much.

NOTE: when I doesn't that function and do it the long way, everything works just fine, but the problem comes when I use that function.

Upvotes: 1

Views: 231

Answers (3)

Syscall
Syscall

Reputation: 19779

The variable is in the scope of function. That's why you cannot access to it outside the function. You could return the value:

function ifIssetPost($value) {
  if (empty($_POST[$value])) { // Only empty is needed (as pointed out by @AbraCadaver)
    $_SESSION[$value.'_chybi'] = "error";
    header("location: index.php");
    exit; // add exit to stop the execution of the script.
  }
  return $_POST[$value]; // return value
}

$ID_user = ifIssetPost('ID_user');
$meta_name = ifIssetPost('meta_name');
$meta_value = ifIssetPost('meta_value');

Upvotes: 6

Hackerman
Hackerman

Reputation: 12305

You can also follow your specification, using $$value:

function ifIssetPost($value) {
 if (!isset($_POST[$value]) || empty($_POST[$value])) {
 $_SESSION[$value.'_chybi'] = "error";
 header("location: index.php");
 } else {
   return $_POST[$value];
 }
}

$value = 'ID_user';
$$value = ifIssetPost($value);  
echo $ID_user;

$value = 'meta_name';
$$value = ifIssetPost($value);
echo $meta_name;

Upvotes: 1

You can use an array to iterate over the $_POST vars. If you want to declare a variable using a string or another variable containing an string, you need to use {}. like ${$value}

$postValues = ["ID_user", "meta_name", "meta_value"];

foreach ($postValues as $value) {
    if (!isset($_POST[$value]) || empty($_POST[$value])) {
     $_SESSION[$value."_missing"] = "error";
     header("location: index.php");
    } else {
       ${$value} = $_POST[$value];
    }
}

Upvotes: 0

Related Questions