Solved

PHP if statement

Posted on 2001-08-07
10
199 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

740 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