Is there any short hands for doing this logic?
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 = htmlspecialchar开发者_如何学编程s($_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.
$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.
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
Here's another possibility for the extraction part:
foreach (array('email','sessionKey','page','ipp') as $v) {
$$v = htmlspecialchars($_POST[$v]);
}
To expand upon the extract
answer, you can slice out only the $_POST entries that you need with:
$vars = array_intersect_key($_POST,
array_flip(array("email", "sessionKey", "page", "ipp")));
// and then this replaces the isset() check and the extraction
if (count($vars) == 4) {
extract(array_map("htmlspecialchars", $vars));
}
精彩评论