简体   繁体   中英

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 ?

Thank you. I hope i get help this time.

First off, don't turn error_reporting() off. 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() :

<?= 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:

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

Ignoring the strange stuff in front of your PHP code before the equal sign change this

<?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:

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

If $_GET['q'] is not set, you can use the null coalesce operator:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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