Reputation: 121
I have URL below:
session.php?action=create_program
Then I have the following piece of code:
if( isset($_GET['action']) ){ $action= $_GET['action']; $action=''; }
It returns an empty string while it should return ''create_program'
Upvotes: 1
Views: 72
Reputation: 27119
I think you were trying to set it to empty if you didn't receive anything in your get request. So either do it like this
if( isset($_GET['action']) ){
$action= $_GET['action'];
} else {
$action='';
}
Or, even simpler, give a default value that remains set if the get parameter is absent.
$action = '';
if( isset($_GET['action']) ){
$action= $_GET['action'];
}
Finally, as suggested by Cashbee in the comment below, you can use the ternary operator to check whether $_GET['action']
was set and, in case it's not, give a default value to $action
.
$action = isset($_GET['action']) ? $_GET['action'] : '';
Upvotes: 5
Reputation: 514
//$action will be empty if not have $_GET['action'];, just for PHP7
$action = $_GET['action'] ?? '';
hope can help you!
Upvotes: 1
Reputation: 9090
Easy is the use of the ternary operator like this:
$action = isset($_GET['action']) ? $_GET['action'] : '';
You should always check if the variable is set or empty.
$action = empty($_GET['action']) ? '' : $_GET['action'];
Or even better, use the PHP filter functions.
$action = filter_input(INPUT_GET, 'action');
This has the advantage, you can even use rules, to give the appropriate result, like described in http://php.net/manual/de/function.filter-input.php
Upvotes: 2
Reputation: 2964
You are setting $action
to empty at last that's why it is returning an empty string
if( isset($_GET['action']) )
{
$action= $_GET['action'];
// $action=''; //Just remove this code
}
Upvotes: 1