strange php statment

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hush
Forum Newbie
Posts: 9
Joined: Sun Mar 23, 2008 8:17 pm

strange php statment

Post 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.
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: strange php statment

Post 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>';
 
hush
Forum Newbie
Posts: 9
Joined: Sun Mar 23, 2008 8:17 pm

Re: strange php statment

Post 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
}
?>
 
 
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: strange php statment

Post by aceconcepts »

It's the closing bracket of else{ on line 6.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: strange php statment

Post 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; ?>
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: strange php statment

Post 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()): ?>
Post Reply