Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any short hands for doing this logic?

Tags:

php

shortcut

Here is the code....

if(!(isset($_POST["email"]) && isset($_POST["sessionKey"]) && isset($_POST["page"]) && isset($_POST["ipp"]))){
        return;
}else{
   $email       = htmlspecialchars($_POST["email"]);           
   $sessionKey  = htmlspecialchars($_POST["sessionKey"]);           
   $page        = htmlspecialchars($_POST["page"]);           
   $ipp         = htmlspecialchars($_POST["ipp"]);           
}  

ok, the idea is I MUST assign the parameter with the same variables. For example, if I post a parameter "test" , I must assign this to a variable .... test... ...Is there any short cut for me to doing something like this? Thank you.

like image 546
Tattat Avatar asked May 14 '11 11:05

Tattat


3 Answers

$data=array_map('htmlspecialchars',$_POST); 
extract($data);

Be careful with extract this can override your existing variables with same name but you can change this behaviour by passing additional parameter to extract function.

like image 98
Shakti Singh Avatar answered Oct 25 '22 14:10

Shakti Singh


Have a look at:

http://php.net/manual/en//function.extract.php

Also, isset accepts more than one parameter, so you could use it like this:

if (isset($_POST["sessionKey"], $_POST["page"], $_POST["ipp"])) ...

See: http://www.php.net/manual/en/function.isset.php

like image 42
Yoshi Avatar answered Oct 25 '22 15:10

Yoshi


Here's another possibility for the extraction part:

foreach (array('email','sessionKey','page','ipp') as $v) {
  $$v = htmlspecialchars($_POST[$v]);
}
like image 1
awm Avatar answered Oct 25 '22 13:10

awm