I have an array that goes like this
$array = array('key1' => $_GET['value1'], 'key2' => $_GET['value2']);
I want to check if the key’s value is not empty. Say a user goes to the webpage and requests this
http://mysite.com/page.php?value1=somevalue&value2= which means that value2 is empty or say that it is missing from the query string i want to be able to check if it’s there and is not empty.
I tried this
foreach($array as $key => $value)
{
if(empty($value))
{
//spout some error
}
}
but even though i enter this http://mysite.com/page.php?value1=somevalue meaning that value2 is missing, i don’t get the error.
How do i achieve what i want?
When I do this:
With:
http://jfcoder.com/test/arrayempty.php?value1=somevalue
EDIT: And this – http://jfcoder.com/test/arrayempty.php?value1=somevalue&value2=
I get:
I’m thinking you need to give more context to your question about actual use, since there is probably some other detail throwing it off.
EDIT
This doesn’t make any sense, but this was provided in a comment:
This actually doesn’t parse due to the single quotes being mixed in with the single quoted string, and the fact that this, even if it did parse, would set the array piece equal to the literal string, not the function result.
I gather that maybe it was:
Which would indicate that a string would always be returned that was at least
'', which would never be empty, due to the single quotes.Consider PDO.