0

Alright so i receive from $_POST and i need my code do something like this

if ($_POST contains EU-London) {
//do stuff here
}else{
//failed to find EU-London
}

Now i've been told several ways to find a certain phrase in code but the posted data contains:

 Array
 (
     [EU-London] => 
 )

How would i check if the EU-London is there? because pregmatch uses strings and im not sure how to grab this using in_array()

2 Answers2

4

You seem to be looking for a key in $_POST:

if (isset($_POST['EU-London'])) {
  // Key exists.
}

As correctly commented by Robert, the proper way to check for an existing key would be

if (array_key_exists('EU-London', $_POST)) {
  // Key exists.
}
Paul
  • 8,974
  • 3
  • 28
  • 48
  • actually the proper way to check if key exists is function` array_key_exists()` isset check if key exists and is set but you can have 'key' = null and isset will return false – Robert Apr 13 '16 at 12:50
  • Thank you so much! i can't believe it was that simple! I feel pretty stupid i get confused when it comes to using $_POST – tryzombie501 Apr 13 '16 at 12:52
  • @Robert True, but we talking about POST data, this corner case is very unlikely. Will update my answer though. – Paul Apr 13 '16 at 12:53
2

You can check if isset key

if (isset($_POST['EU-London'])) {
    //key isset
}
Shay Altman
  • 2,720
  • 1
  • 16
  • 20