Solved

PHP if statement

Posted on 2001-08-07
10
191 Views
Last Modified: 2008-02-01
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>

0
Comment
Question by:system
  • 6
  • 2
  • 2
10 Comments
 
LVL 1

Expert Comment

by:rumblefiz
ID: 6362137
what about:

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

echo $s;
?>
0
 
LVL 1

Expert Comment

by:rumblefiz
ID: 6362144
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
0
 
LVL 1

Expert Comment

by:rumblefiz
ID: 6362166
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
0
 

Author Comment

by:system
ID: 6365348
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?
0
 
LVL 1

Expert Comment

by:rumblefiz
ID: 6365393
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
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 1

Expert Comment

by:rumblefiz
ID: 6366154
system,

did this help you at all?

- rumblefiz
0
 
LVL 10

Accepted Solution

by:
Chad Smith earned 200 total points
ID: 6366727
PHP actually has a form of if and while statements for doing this very task.  Here is an example

<?
  $bolIsBol = true;
  if($bolIsBol):
?>
  <HTML>
  <BODY>
  This is the html if bolIsBol is set to true!
  </BODY>
  </HTML>
<?
  else:
?>
  <HTML>
  <BODY>
  This is the html if bolIsBol is set to false!
  </BODY>
  </HTML>
<?
  endif;
?>
0
 
LVL 1

Expert Comment

by:rumblefiz
ID: 6366804
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>

<? } ?>
0
 
LVL 10

Expert Comment

by:Chad Smith
ID: 6366887
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.

0
 

Author Comment

by:system
ID: 6370027
Thanks Knight,

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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now