Solved

PHP if statement

Posted on 2001-08-07
10
200 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
Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

 

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
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.

623 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