# PHP: Default Variable

Take the CGI variable ‘a’, or if it doesn’t exist, set it to 10.

My version:

``````if (\$_REQUEST['a']=='') \$a=10; else \$a=\$_REQUEST['a'];
``````

Who has a shorter version?

## Answer 1, authority 100%

Ternary expression:

``````\$a = ( \$_REQUEST['a'] == '' ) ? 10 : \$_REQUEST['a'];
//  :
\$a = ( ( \$a = \$_REQUEST['a'] ) == '' ) ? 10 : \$a;
// :
\$a = \$_REQUEST['a'];
if ( \$a == '' ) \$a = 10;
//   PHP,  JS :
\$a = ( \$_REQUEST['a'] == '' && 10 ) || \$_REQUEST['a'];
\$a = ( ( \$a = \$_REQUEST['a'] ) == '' && 10 ) || \$a;
\$a = \$_REQUEST['a'] || 10;
``````

Remove all optional spaces:

1. 42
2. 36
3. 34
4. 44
5. 37
6. 22

Options 4-6 are not suitable for PHP!!!

## Answer 2, authority 67%

``````//  :
//   ..
function v(&\$var, \$alt = false) { return !empty(\$var) ? \$var : \$alt; }
//  :
\$a = v(\$_REQUEST['a'], 10);
``````

Well, ok, try it, but be careful with this one 😉

``````\$a = @\$_REQUEST['a'] or \$a = 10;
``````

## Answer 3, authority 33%

``````\$a = @\$_REQUEST['a'] ?: 10;
``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.5277051,"math_prob":0.97802585,"size":880,"snap":"2022-40-2023-06","text_gpt3_token_len":326,"char_repetition_ratio":0.24200913,"word_repetition_ratio":0.13186814,"special_character_ratio":0.5477273,"punctuation_ratio":0.25396827,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97805023,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-10-07T15:37:10Z\",\"WARC-Record-ID\":\"<urn:uuid:1fa70553-bff5-4490-9d6e-116d564e4116>\",\"Content-Length\":\"61603\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:df98b659-94bc-4424-8032-e7008e653a7d>\",\"WARC-Concurrent-To\":\"<urn:uuid:72894bf9-ee88-4abe-bdf8-e421ce92c984>\",\"WARC-IP-Address\":\"104.21.21.49\",\"WARC-Target-URI\":\"https://phpzenfolio.com/php-default-variable.php\",\"WARC-Payload-Digest\":\"sha1:IDHIUFEHRKZ7JSYBZV5MJH2MZCDCCECA\",\"WARC-Block-Digest\":\"sha1:GAT7ZTUC6XJCTV55CEWP7XRXGFUJR7W5\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-40/CC-MAIN-2022-40_segments_1664030338213.55_warc_CC-MAIN-20221007143842-20221007173842-00661.warc.gz\"}"}