UserIsCorrupt
UserIsCorrupt

Reputation: 5025

Create variable in PHP regardless if value is set

If I were to create this variable:

$example = $_GET['test'];

I would get this error message if $_GET['test'] is not set:

Notice: Undefined variable...

How can I create variables like this (without having to use if statements and the like) without having to see this error message?

Upvotes: 0

Views: 70

Answers (2)

Explosion Pills
Explosion Pills

Reputation: 191789

Typically, I do something like the following:

function get($key, $default = null) {
   return isset($_GET[$key]) ? trim($_GET[$key]) : $default;
}

$example = get('test');

Upvotes: 1

Nemoden
Nemoden

Reputation: 9056

$example = isset($_GET['test']) ? $_GET['test'] : NULL;

Upvotes: 2

Related Questions