How to check whether $_GET['id'] is set and it's not empty using php
Get the solution ↓↓↓Solution:
You should check both the set-status and the content:
if ( isset( $_GET['id'] ) && !empty( $_GET['id'] ) )
// ....
Note that you should not use the value simply as it is and work with it. You should make sure that it only contains values you expect. To check for an integer, and even better to work with an integer from that on, do something like that:
$id = ( isset( $_GET['id'] ) && is_numeric( $_GET['id'] ) ) ? intval( $_GET['id'] ) : 0;
if ( $id != 0 )
// id is an int != 0
else
redirect('index.php');
Answer
Solution:
What does the error tell you?
But this would check if it's set, and if it's an integer
if(isset($_GET['id']) && ctype_digit($_GET['id']) && intval($_GET['id']) > 0)
Or check out ctype_digit
Answer
Solution:
if (!empty($_GET['id']) && filter_var($_GET['id'], FILTER_VALIDATE_INT))
or
if (!empty($_GET['id']) && ctype_digit($_GET['id']))
Answer
Solution:
Well... If you're setting it up as an integer type variable, then you could do also this:
if($_GET['id'] && gettype($_GET['id']) == 'integer'){
#do something
}else{
#do something else
}
Answer
Solution:
How to determine id is an integer and it's not empty and then perform the specific action....
if (!empty($_GET['id']) && (intval($_GET['id']) == $_GET['id'])) {
//do something
} else {
redirect('index.php'); //redirect is a function
}
The above may not be the best solution, but it should work for what you need. It will not allow the id to be 0, and will require it to be an integer.
Answer
Solution:
if(isset($_GET['id']) && ctype_digit($_GET['id']) && is_numeric($_GET['id']) && $_GET['id']>0) {
$user= User::find_by_id($_GET['id']);
}
else {
redirect('index.php'); //redirect function
}
Try this and this code full pill your requirement.
Answer
Solution:
It seems like you're just having an issue with the ?id throwing a true when you want to check if its set. Thats because it is set, but not set to a value that you would like to use.
I'm assuming the userids have to be > 0 so just do this
if (isset($_GET['id']) && $_GET['id'] > 0) {
$user= User::find_by_id($_GET['id']);
} else {
redirect('index.php');
}
Also you're prolly best cleaning the id before processing it so
$userID = isset($_GET['id']) && $_GET['id'] > 0 ? (int) $_GET['id'] : 0;
if ($userID) {
$user= User::find_by_id($userID);
} else {
redirect('index.php');
}
Theres a lot of ways to do it, personally I prefer this. It makes your code look a bit cleaner.
Answer
Solution:
You'll have to check multiple times for every case possible: what if id is not set or empty? what if id is set but it is not a digit? what if 'id' key does not exist at all in GET request?
I had similar situation. I was sending two vars with GET, one digit: 'id' and another one was string which was a name: 'state' ... I solved it by checking all possible conditions.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: php undefined array key
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.