Help with syntax

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
User avatar
quadoc
Forum Contributor
Posts: 137
Joined: Fri Jul 01, 2005 5:33 pm
Location: Atlanta, GA

Help with syntax

Post by quadoc »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I've the following codes, all I wanted to do is to execute the HTML code when $an_mess not blank, but somehow I keep getting error messages.  Could someone tells me why? Thanks...

Code: Select all

<?php

  $mess = "this is a test";

  if ($mess != "")
   {  ?>
 
<html>   
   

<p >

 <? echo $an_mess; ?>  

</p>   
</body>   
</html>   

<? If ($mess != "") {echo "}"; }  ?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Wow. That's really horrible. First thing to do is to rewrite the code so it's properly formatted in a tidy and readable way, then you'll be able to see what's wrong with it. As it happens, the last line of your script is what's breaking it, plus, if you want to echo everything when $an_mess isn't empty you really need to check it rather than checking $mess.

Code: Select all

<?php

$mess = "this is a test"; //I imagine this is meant to be $an_mess, otherwise your code will never echo anything because $mess is unset and will always be empty

if (!empty($an_mess)) { //empty() checks to see if a variable contains a value, !empty() checks to see if it's not empty. Better than $var!="".

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

} //Close the if statement.

?>
User avatar
quadoc
Forum Contributor
Posts: 137
Joined: Fri Jul 01, 2005 5:33 pm
Location: Atlanta, GA

Post by quadoc »

Wow! Thanks... You made it look so easy.... 8O
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You are also referencing a variable that is never set in this snip of code.

Code: Select all

<?php
// You set $mess here...
$mess = "this is a test";

// You are checking $mess here (for empty)
if (empty($mess)): 
?>
<html>   
  <body>
    <!-- You are calling $an_mess here, but it is not set to anything as fas as I can see -->
    <p ><?php echo $an_mess; ?></p>
  </body>   
</html>   

<?php
// Close the if
endif;
?>
Post Reply