Reputation: 2363
Can someone give me an example of how to use the PHP ternary operator which will check for a variable using $_GET (which can be defined in the URL), if it's not in the URL then check if the var was set in another PHP file. If it wasn't set in the URL or another PHP file, then I want it to equal "default".
Upvotes: 1
Views: 366
Reputation: 360592
$value = isset($_GET['somevar']) ? $_GET['var'] : $default_value;
On the most recent PHP versions, there's a shortcut version of this:
$value = isset($_GET['somevar']) ?: $default_value; (not the same as the first version)
You can use $GLOBALS['nameofvar']
to see if a particular PHP variable has been defined as well, though this'll be problematic if you're doing the check inside a function.
Upvotes: 3
Reputation: 26167
$myVar = isset($_GET["someVar"]) ? $_GET["someVar"] : (isset($someVar) ? $someVar : "default");
Upvotes: 3
Reputation: 1870
Are you looking for something like this:
if(isset($_GET["MyVar"]))
{
$newVar = $_GET["MyVar"];
}
else if(isset($myVar))
{
$newVar = $myVar;
}
else
{
$newVar = "default";
}
or
$newVar = isset($_GET["MyVar"]) ? $_GET["MyVar"] : (isset($myVar) ? $myVar : "default");
Upvotes: 2