任何人都知道以下代码段的简短if版本?

if(isset($_GET['tag'])) { 
  define("NJ_THISCATSUB","tag: ".$_GET['tag']);
}

There may be a couple of ways, none of which would really be recommended instead of what you have.

Except for omitting the {} you can't really shorten by packing it into a ternary operator (for example) if NJ_THISCATSUB is assumed to be not defined when $_GET['tag'] is unset. Stick with what you have -it is clear and readable, and correct.

// About as short as you're going to get:
// In other words, your method is fine and I don't recommend changing it.
if(isset($_GET['tag'])) define("NJ_THISCATSUB","tag: ".$_GET['tag']);

Ternary example:

However, NJ_THISCATSUB were defined as a null or empty value in absence of the $_GET['tag'], you could use a ternary:

// Define as NULL, absent $_GET['tag'] (not the same as what you have!)
define('NJ_THISCATSUB', isset($_GET['tag']) ? "tag: ".$_GET['tag'] : NULL);

This doesn't appear to be the behavior you are after though.


By short-circuit &&

You could take advantage of the logical && and to it this way, saving a couple of characters. If the left side of && is true, it proceeds to check the right side, and must evaluate the define() to do so. If the left side is FALSE, it will short circuit and not evaluate the right. Therefore this works:

isset($_GET['tag']) && define("NJ_THISCATSUB","tag: ".$_GET['tag']);

The above is kind of unconventional in PHP though (if there is a such thing as convention in PHP), and would be more at home in languages like JavaScript or Ruby. You're far more likely to encounter PHP code using the simpler if () you initially posted.

// Simple console demonstration
// Define X if $x is set:
php > $x = 123;
php > isset($x) && define('X', $x);
php > echo X;
// 123

// Define Y if $y is set (it isn't)...
php > isset($y) && define('Y', $y);
php > echo Y;
PHP Notice:  Use of undefined constant Y - assumed 'Y' in php shell code on line 1

Notice: Use of undefined constant Y - assumed 'Y' in php shell code on line 1
// Oops, Y is not defined as a constant...

If you can allow a half-filled constant:

define("NJ_THISCATSUB", @"tag: $_GET[tag]");

Note that this only makes sense if may want to debug the absent input parameter later on. With isset it's gone forever, with @() you can bring notices back.

Also note that absent array key quotes are only permissable in double quoted string context.