1

I have a simple if-else statement--if true, I'd like it to echo this content:

<div id="login">
  <h2>Login</h2>
  <div class="box">
    <form method="POST" action="index.php/login">
      Username/Email:<br />
      <input type="text" name="username" value="<?php echo set_value("username"); ?>" size="50" class="form" />
      <?php echo form_error("username"); ?>
      Password:<br />
      <input type="password" name="password" value="<?php echo set_value("password"); ?>" size="50" class="form" />
      <?php echo form_error("password"); ?>
      <input type="submit" value="Login" name="login" />
    </form>
  </div>
</div>

and if false do something similar.

How should I approach this? I'm a PHP noob (and I've been annoying the community here!)--so I'm standing at the echo function right now--I think I can set variables, but that's about it.

Thanks so much!

7 Answers 7

8

Same thing as you have been doing:


<?php
if some_condition
{
?>
    <p> I am inside the true condition due : <?php echo "true on condition" ?> </p>
<?
}
else
{
?>
    <p> I am inside the false condition due : <?php echo "false on condition" ?> </p>
<?
}
?>

Sign up to request clarification or add additional context in comments.

Comments

4

You can simply break out of PHP in the if/else statement.

if(true)
{
    ?>
    Regular HTML Here
    <?php
}
else
{
    ?>
    Regular HTML Here
    <?php
}

You can break out of PHP at any time using the '?>' operator, and with any control structure, it should show the code just as if you echo'd it. Another option would be to use output buffering.

if(true)
    {
        ob_start();

        ?>
        Regular Code Here
        <?php

        $contents = ob_get_contents();
        ob_end_clean();
    }

This will leave the contents of what outputted between the start and end in $contents.

Finally, you could use an include() to only include your login form when you actually need it. (I prefer this method because I can put login forms almost anywhere on the site very easily)

if(true)
{
    include('loginform.php');
}

2 Comments

eh, my server doesn't allow "include".
Thats really weird lol. You could always call a function you put somewhere near the bottom of the code that outputs the login form. (You can break out of PHP in a function aswell, just like the first example)
2
<?php if ($condition) { ?>

    <div id="login">
      <h2>Login</h2>
      <div class="box">
        <form method="POST" action="index.php/login">
          Username/Email:<br />
          <input type="text" name="username" value="<?php echo set_value("username"); ?>" size="50" class="form" />
          <?php echo form_error("username"); ?>
          Password:<br />
          <input type="password" name="password" value="<?php echo set_value("password"); ?>" size="50" class="form" />
          <?php echo form_error("password"); ?>
          <input type="submit" value="Login" name="login" />
        </form>
      </div>
    </div>

<?php } else { ?>

    other html here

<?php } ?>

Comments

2

Just a suggestion; for readability and ease of flexibility, it is better to place what you want to display inside a variable first.

$html = "<form method='post' action='?'> ...... ";

if ($condition)
   $html .= "Error"
else
   $html .= "No problem"
....

<html>
  <body>
    <?php
      echo $html;
    ?>
   </body>
</html>

Or you can use a template. One which I would recommend is EasyTemplate.

Comments

1

there's a form of php conditional that's somewhat easier to read for this, uses colon notation so instead of

<?php if ($condition) { ?>
...html
<?php } else { ?>
...html
<?php } endif; ?>

you do

<?php if ($condition) : ?>
...html
<?php else : ?>
...html
<?php endif; ?>

also if your config allows it, you can shorthand

<?= $variable ?>

instead of

<?php echo($variable); ?>

Comments

1

Dropping out of php is the easiest way to do this. I find that using the alternate control structures makes this sort of code much more readable than stranded curly braces, especially if you have a lot of html.

<?php if (!$logged_in): ?>

    <div id="login">
      <h2>Login</h2>
      <div class="box">
        <form method="POST" action="index.php/login">
          Username/Email:<br />
          <input type="text" name="username" value="<?php echo set_value("username"); ?>" size="50" class="form" />
          <?php echo form_error("username"); ?>
          Password:<br />
          <input type="password" name="password" value="<?php echo set_value("password"); ?>" size="50" class="form" />
          <?php echo form_error("password"); ?>
          <input type="submit" value="Login" name="login" />
        </form>
      </div>
    </div>

<?php else: ?>

    // display html for a logged-in user

<?php endif; ?>

Comments

0

Use something like this

echo htmlentities($string);

htmlentities

Set the string to the html code you want to output.

1 Comment

sorry, my bad. I thought you wanted the output to be the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.