Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP if statement

Posted on 2001-08-07
10
Medium Priority
?
204 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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…
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 dynamically set the form action using jQuery.

704 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