Question

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!

Was it helpful?

Solution

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

OTHER TIPS

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');
}
<?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 } ?>

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.

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); ?>

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

Use something like this

echo htmlentities($string);

htmlentities

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top