Link to home
Start Free TrialLog in
Avatar of system
system

asked on

PHP if statement

Hello,

I would like to be able to make a PHP file that reads in a value from itself (ie define a var inside the header of the PHP file) and then display HTML content based on that

for example (psuedo code):
a new var (boolean) = true

if the new var is true
then display:
<html>
Some text ...
</html>

else
<html>
The site is down...
</html>

Avatar of rumblefiz
rumblefiz

what about:

<?
$isDown = 1;
if($isDown) {
   $s  = Down";
} else {
   $s  = "Not down";
}

echo $s;
?>
a clearer ex:

<?
   $bIsDown = false;
   if($bIsDown) {
      echo "<html>";
      echo "<head><title>Site Is Down</title></head>";
      echo "<body>Come back later...</body>";
      echo "</html>";
   } else {
      echo "<html>";
      echo "<head><title>Welcome</title></head>";
      echo "<body>The site is up...</body>";
      echo "</html>";
   }
?>

hope that helps

- rumblefiz
it may also be easier (depending on how much text) to do the following:

<?
   $bIsDown = false;
   if($bIsDown) {
      $s = "The site is down";
   } else {
      $s = "The site is up";
   }
?>

<html>
<head><title>My Page</title></head>
<body>
   <?= $s; ?>
</body>
</html>

- rumblefiz
Avatar of system

ASKER

rumblefiz:

is there a way to have $s be a multi line
string, i.e. a chunk of code? without having to type everything on one long line?
i am not really sure what you are asking. take a look at the following samples and see if any answer your question:

<?
     $bIsDown = false;
     if($bIsDown) {
        echo "
                    <html>
                    <head><title>Site is down</title></head>
                    <body>
                       The site is down. Please come back another time.
                       You can span multiple lines, of course!!!
                    </body>
                    </html>
                  ";
      } else {
        echo "
                    <html>
                    <head><title>Site is up</title></head>
                    <body>
                       The site is up. Welcome!!!!
                    </body>
                    </html>
                  ";
       }
?>


you could also do like:

<?
      $bIsDown = false;
      if($bIsDown) {
?>
      <html>
      <head><title>The site is down</title></head>
      <body>
         The site is currently down. Please come back!!!!
      </body>
      </html>
<? } else { ?>
      <html>
      <head><title>The site is up</title></head>
      <body>
         The site is up. Welcome!!!!
      </body>
      </html>
<? } ?>


or like this:

<?
      $bIsDown = false;
      if($bIsDown) {
         $s = "The site is currently down. Please come back" .
                  "This is another line." .
                  "And another....";
      } else {
         $s = "The site is up!" .
                  "Welcome. This is another line";
      }
?>

<html>
<head><title>Site Status</title></head>
<body>
   <?= $s; ?>
</body>
</html>

i hope these help. if not, give me an ex of what you are trying to do and i will be glad to assist further.

- rumblefiz
system,

did this help you at all?

- rumblefiz
ASKER CERTIFIED SOLUTION
Avatar of Chad Smith
Chad Smith
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
knight00,

out of curiosity, what is the difference between your post and my post above  (besides the braces) that is:


<?
         $bIsDown = false;
         if($bIsDown) {
?>
         <html>
         <head><title>The site is down</title></head>
         <body>
             The site is currently down. Please come back!!!!
         </body>
         </html>

<? } else { ?>

         <html>
         <head><title>The site is up</title></head>
         <body>
             The site is up. Welcome!!!!
         </body>
         </html>

<? } ?>
They both work, and they both have to same outcome.

However, the style I used was designed for spanning your control structures if/while/for/etc across multiple code blocks.

Avatar of system

ASKER

Thanks Knight,

your post is the most adaptible.
Thanks to everyone who posted.