Page 1 of 1

strange php statment

Posted: Sun Apr 20, 2008 10:39 pm
by hush
I am using some code for a mailing list and whilst I understand most of it there is a statement in there that I just don't have a clue about:

Code: Select all

<?php
}
?>
I know it does something as when I took it out my web page no longer functioned.

Re: strange php statment

Posted: Sun Apr 20, 2008 10:57 pm
by flying_circus
Can you post more of the code? You can do something like this, which is what I am expecting your code to do:

Code: Select all

 
<?php
  $results = database results
 
  foreach($results as $result) {
?>
 
  <td>The result is: <?php print $result['value']; ?></td>
 
<?php
  }
?>
 
Essentially it allows you to inject HTML in place of having to do something like:

Code: Select all

 
  $output .= '<td>The result is:' . $result['value'] . '</td>';
 

Re: strange php statment

Posted: Sun Apr 20, 2008 11:11 pm
by hush

Code: Select all

 
<?php
if((DBHOST == "localhost") && (DBNAME == "yourdb") && (DBUSER == "youruser") && (DBPASS == "yourpass")) {
  echo("<p>Please edit <code>inc/dbConstants.php</code> to match your MySQL configuration.</p>");
}
else {
?>
    <form id="addressForm" action="index.php" method="get">
      <fieldset>
        <legend>Join our mailing list!</legend>
        <p>
          <input type="text" name="address" id="address" />
          <input type="submit" value="Sign Up" />
        </p>
        <p id="response">
 
<?php echo(storeAddress()); ?></p>
      </fieldset>
    </form>
   <p><a href="createTable.php">Need to create a database table?</a></p> 
<?php
}
?>
 
 

Re: strange php statment

Posted: Mon Apr 21, 2008 3:14 am
by aceconcepts
It's the closing bracket of else{ on line 6.

Re: strange php statment

Posted: Mon Apr 21, 2008 5:36 am
by Chris Corbyn
It's a terrible way to mix PHP and HTML. This is much more expressive:

Code: Select all

<?php if((DBHOST == "localhost") && (DBNAME == "yourdb") && (DBUSER == "youruser") && (DBPASS == "yourpass")): ?>
    <p>Please edit <code>inc/dbConstants.php</code> to match your MySQL configuration.</p>
<?php else: ?>
    <form id="addressForm" action="index.php" method="get">
      <fieldset>
        <legend>Join our mailing list!</legend>
        <p>
          <input type="text" name="address" id="address" />
          <input type="submit" value="Sign Up" />
        </p>
        <p id="response">
           <?php echo storeAddress(); ?>
        </p>
      </fieldset>
    </form>
   <p><a href="createTable.php">Need to create a database table?</a></p>
<?php endif; ?>

Re: strange php statment

Posted: Mon Apr 21, 2008 5:40 am
by Chris Corbyn
I also think I'd move the checking logic so it reads more clearly:

Code: Select all

<?php if((DBHOST == "localhost") && (DBNAME == "yourdb") && (DBUSER == "youruser") && (DBPASS == "yourpass")): ?>
Would be more clearly understood if the logic was moved into a function which describes the check:

Code: Select all

<?php if(configNeedsEditing()): ?>