0

Another micro-optim question. What is the php best way to set variable like in javascript existing or new value

var a = a || false

using

$a = $a || false;

we get error notice abour undefined variable $a. This variable may or may not be set in previous programm part. (Due plug-ins.) It can be done with extra condition before. Stg like if isset(...

The question is does elegant 1 row solution exists?

Jirka Kopřiva
  • 2,939
  • 25
  • 28

2 Answers2

1

Solution exists:

In general:

$a = isset($a) ? $a : false;

Some example with handling $_GET array.

$a = isset($_GET['someParam']) ? $_GET['someParam'] : false;

You can use everything instead of $a or $_GET['someParam'].

Sharikov Vladislav
  • 7,049
  • 9
  • 50
  • 87
1

$a = isset ($a)? $a:false

This is the best way for doing so without warnings

Yaron U.
  • 7,681
  • 3
  • 31
  • 45