This question already has answers here:
Check if $_POST exists
(15 answers)
Closed 6 years ago.
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()
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.
}
You can check if isset key
if (isset($_POST['EU-London'])) {
//key isset
}
Related
This question already has answers here:
How to verify if $_GET exists?
(7 answers)
Closed 5 years ago.
Like the title says, how do I determine which GET value has been passed on within the url?
Take http://www.example.com/view.php?id=20as an example. The current GET value is 'id', with a value of 20. This works great, but if someone plays around and changes the GET value to something other than 'id', I get a lot of PHP errors on the page.
So my goal is to check wether the passed GET value is incorrect, so I can redirect them to another page. How would I go about doing that?
You should validate the URL parameters. In your case, it has to be id and that has to be numeric:
if (isset($_GET['id']) && is_numeric($_GET['id']))
As an advance check, you can validate whether id is integer or not:
if((int)$_GET['id'] == $_GET['id']){
return TRUE;
} else {
return FALSE; // It's a number, but not an integer
}
This question already has answers here:
Check if multiple strings are empty [duplicate]
(7 answers)
Closed 5 years ago.
I have more than 30 fields in a form, and can't believe that I must write an array to check if any of them is empty (likehere)
I need something like js
$_POST[].each function{
if this is empty...
}
and hope that 'php' as so glorified language has something like this or similar.
Try this:
foreach($_POST as $post)
{
if( !empty($post) )
{
// your code here
}
}
This question already has answers here:
recursive array_diff()?
(5 answers)
Closed 6 years ago.
I am checking whether two arrays are identical and even though I know they are, my conditional returns false.
Here are the two arrays: http://pastebin.com/knekiW67
Here is the code:
$stored_items = (Array1 in Pastebin link)
$new_items = (Array2 in Pastebin link)
if($stored_items === $new_items) {
return true;
} else {
return false;
}
I've even checked the two arrays using https://www.diffchecker.com/ and it responds that they are identical.
Does anyone know why it's returning false?
=== if not working for array(only string and int)
for checking array need use http://php.net/manual/ru/function.array-diff-ukey.php with special callback
This question already has answers here:
Checking if array is multidimensional or not?
(16 answers)
Closed 9 years ago.
How come when I run this code, I get an output of I am a multidimensional array! (the first block). I thought it would go into the second block, but it doesn't. What am I missing here?
$values = array('1','2');
if(isset($values[0][0])){
echo "I am a multidimensional array!";
}else{
echo "I am not a multidimensional array.";
}
$values = array(1,array(1,2));
$multi = false;
if(is_array($values)){
foreach($values as $k=>$v){
if(is_array($v)){
$multi = true;
break;
}
}
}
echo $multi ? "multi" : "not multi";
Try this:
if(is_array($values[0]))
Edit: This will check the first element of the array only. You should loop through each element to check if its truly multidimensional.
This code checks to see if the first element of the array is also an array. isset just checks whether or not a variable is NULL.
isset in your example is not working as expected. Perhaps there is a slight difference in functionality between PHP versions or setups. I didn't see anything in the manual but maybe you can:
http://php.net/manual/en/function.isset.php
Using is_array is more semantic, so in my opinion is a much better choice.
This code only goes into the if-branch for me, if the first value in the array is explicitly declared as a string,
$values = array('1',2);
– and with that the behavior is nothing but logical, because $values[0] is that text literal '1', and that has a first character that can be access using a zero based index.
So I guess either your real data is of a string type – or it maybe depends in the PHP version (I tested under 5.3.16).
Anyway, using is_array as the other answers already suggested is the right way to go here.
This question already has answers here:
PHP $_GET and $_POST undefined problem
(5 answers)
Closed 2 years ago.
I continue to get undefined printed out when I use print($_GET['user_username']); from the previous page. The URL of the page is page.php?user_username=Pete. Why is this happening?
$_GET manual says
An associative array of variables
passed to the current script via the
URL parameters.
First be sure that element exists
<?php
echo !isset($_GET["user_username"]) ? "undefined" : $_GET["user_username"];
?>
Or try var_dump against $_GET array to see if element with user_username key exists.
var_dump($_GET);
Is your request like this one?
http://www.mydomain.com/something.php?user_username=something
Try this code:
print_r($_GET);
You will get all the elements passed using get in array format. Then you can check it.. It also helps better in debugging many times.