简体   繁体   English

PHP-如果echo $ _GET ['q']; 空添加此TEXT

[英]PHP - if echo $_GET['q']; empty add this TEXT

this is my code 这是我的代码

amzn_assoc_default_search_phrase = "<?php error_reporting(0); echo $_GET['q'];?>";

and if $_GET['q'] is empty, add my text example: perfume how i can do this ? 如果$_GET['q']为空,请添加我的文本示例: perfume我该怎么做?

Thank you. 谢谢。 I hope i get help this time. 希望这次我能得到帮助。

First off, don't turn error_reporting() off. 首先,请不要关闭error_reporting() That's a bad idea since it will be pretty hard for you to debug your code later on if there are other errors/issues. 这是一个坏主意,因为如果有其他错误/问题,以后将很难调试代码。

If you're using a PHP version older than PHP 7, you can use isset() : 如果您使用的PHP版本早于PHP 7,则可以使用isset()

<?= isset($_GET['q']) ? $_GET['q'] : 'perfume' ?>

If you're using PHP 7+, you can use the new shorter null coalescing operator ( ?? ) syntax that does the same: 如果您使用的是PHP 7+,则可以使用新的较短的null合并运算符( ?? )语法,其语法相同:

<?= $_GET['q'] ?? 'perfume' ?>

Ignoring the strange stuff in front of your PHP code before the equal sign change this 等号更改之前,请忽略PHP代码前面的奇怪内容

<?php error_reporting(0); echo $_GET['q'];?>

to

<?php 
error_reporting(0); 
echo (empty($_GET['q']) ? 'perfume' : $_GET['q']);
?>

If $_GET['q'] is set but empty, you can use the empty ternary operator: 如果$_GET['q']设置为空,则可以使用空的三元运算符:

<?php echo $_GET['q'] ?: 'perfume'; ?>

If $_GET['q'] is not set, you can use the null coalesce operator: 如果未设置$_GET['q']则可以使用null合并运算符:

<?php echo $_GET['q'] ?? 'perfume'; ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM