Reputation: 65
I was wondering how can I convert the code below into a PHP function that works.
Here is my original code.
if(isset($_GET['cat'])) {
$cat_name = strip_tags(filter_var($_GET['cat'], FILTER_SANITIZE_URL));
}
So far I got this for the function.
function cat(){
if(isset($_GET['cat'])) {
$cat_name = strip_tags(filter_var($_GET['cat'], FILTER_SANITIZE_URL));
}
}
Upvotes: 0
Views: 109
Reputation: 18344
I would do:
function get_cat($GET)
$cat_name = null;
if(isset($GET['cat'])) {
$cat_name = strip_tags(filter_var($GET['cat'], FILTER_SANITIZE_URL));
}
return $cat_name;
}
And to call it, just do:
$cat = get_cat($_GET);
With this, the get_cat
function is encapsulated (doesn't depend on external vars).
Just my 2 cents. Hope this helps. Cheers
Upvotes: 0
Reputation: 28752
This will return the sanitized value or null
if $_GET['cat']
is not set. When you call this function, you would need to check if the return value is null
.
function get_cat() {
$cat_name = null;
if(isset($_GET['cat'])) {
$cat_name = strip_tags(filter_var($_GET['cat'], FILTER_SANITIZE_URL));
}
return $cat_name;
}
Upvotes: 2