Are there any short hands to follow 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 = htmlspecialchars($_POST["ipp"]); } 

ok, the idea is that I MUST assign a parameter with the same variables. For example, if I send the parameter "test", I have to assign this to a variable ... test ...... Is there any short segment for me to do something like this? Thanks.

+6
source share
4 answers
 $data=array_map('htmlspecialchars',$_POST); extract($data); 

Be careful with extract , this may override existing variables with the same name, but you can change this behavior by passing an additional parameter to the extraction function.

+2
source

Take a look:

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

In addition, isset accepts more than one parameter, so you can use it as follows:

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

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

+2
source

Here is another option for the extraction part:

 foreach (array('email','sessionKey','page','ipp') as $v) { $$v = htmlspecialchars($_POST[$v]); } 
+1
source

To extend the answer of extract , you can cut off only the $ _POST entries you need:

 $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)); } 
+1
source

Source: https://habr.com/ru/post/888100/


All Articles