简体   繁体   中英

only echo html if a $get condition is met

I am redirecting various messages from a header and echoing them onto a new page. How can I change this so that the html only appears if there is a message variable to echo. ie, if one of the $_GET conditions are met? Thanks

$message="";

if(isset($_GET['pw']))
{
  $message = "first message";
}
else if(isset($_GET['em']))
{
  $message = "second message";
}
else if(isset($_GET['fn']))
{
  $message = "third message";
}
else if(isset($_GET['ln']))
{
  $message = "forth message";
}
    ?>
<html>
     <body>
         <table class='tab2'>
             <td class='td2'><?php echo $message; ?></td>
         </table>
     </body>
<html>
<?php
   if($message)
   {
?>

   <html>
   ....

   </html>

<?php
   }
?>

You can use this:

<?php
$message="";

if(isset($_GET['pw']) && !empty($_GET['pw']))
{
  $message = "first message";
}
else if(isset($_GET['em']) && !empty($_GET['em']))
{
  $message = "second message";
}
else if(isset($_GET['fn']) && !empty($_GET['fn']))
{
  $message = "third message";
}
else if(isset($_GET['ln']) && !empty($_GET['ln']))
{
  $message = "forth message";
}
echo $message;
?>

Demo: http://shaquin.tk/experiments/get-conditions.php

It checks to see if a $_GET variable is defined, and if it is not empty, sets $message to the appropriate message.

See empty - PHP Manual and isset - PHP Manual .

EDIT:

If you want to display multiple messages, use this code:

$message=array();
if(isset($_GET['pw']) && !empty($_GET['pw']))
{
  $message[] = "first message";
}
if(isset($_GET['em']) && !empty($_GET['em']))
{
  $message[] = "second message";
}
if(isset($_GET['fn']) && !empty($_GET['fn']))
{
  $message[] = "third message";
}
if(isset($_GET['ln']) && !empty($_GET['ln']))
{
  $message[] = "forth message";
}
echo implode("\n", $message);

Demo: http://shaquin.tk/experiments/get-conditions2.php

It initializes $message as an array, then adds to the array if a condition is met.

Before printing the message, it implode s it, using a newline character as 'glue'.

See implode - PHP Manual .

For an HTML page, you would probably want to set the 'glue' as a <br /> element, like this:

echo implode('<br />', $message);

(or if you are using HTML4):

echo implode('<br>', $message);

<?php if (!empty($message)) { echo $message;}?>

or

<?php if (isset($message)) { echo $mssage;}?>

or

<?php if (($message !== "") || (isset($message)) { echo $message; } ?>

or anything to do if message exists then do this

You can do

if ($message != "") { ?> 
   All your HTML code (including echo $message) goes here 
<?php
} else { do nothing here }
?>

Everyone's answer has pretty much the same idea but, specifically, here is how you can do it:

$message="";

if(isset($_GET['pw']))
{
  $message = "first message";
}
else if(isset($_GET['em']))
{
  $message = "second message";
}
else if(isset($_GET['fn']))
{
  $message = "third message";
}
else if(isset($_GET['ln']))
{
  $message = "forth message";
}
?>
<?php if ($message) :?>
<html>
    <head>
    <style type='text/css'>
        .tab2
        {
            width:400px; height:40px;
            position: absolute; right: 300px; top: 70px;
        }

        .td2
        {
            background-color:pink;
            color:blue;
            text-align:center;
        }
     </style>
     </head>
     <body>

         <table class='tab2'>
             <td class='td2'><?php echo $message; ?></td>
         </table>
     </body>
<html>
<?php endif;?>

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