FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Placement of Brackets in php code





Possum
Hi

I have some code that works. but I don't understand why the brackets are place here



and here



Code:
<?php if ($user->data['user_id'] == ANONYMOUS) { ?>
<form action="<?php echo $forumurl; ?>ucp.php?mode=login" method="post">
<input type="text" name="username" value="" /><br />
<input type="password" name="password" value="" /><br />
<input type="checkbox" name="autologin" value="1" /> Remember Me<br />
<input type="hidden" name="redirect" value="<?php echo $newurl; ?>" />
<input type="submit" name="login" value="Login" /> </form>
Not a member? <a href="<?php echo $forumurl; ?>ucp.php?mode=register">Register now</a>!
<?php } else { ?>
<a href="<?php echo $forumurl; ?>ucp.php?mode=logout&sid=<?php echo $user->data['session_id']; ?>&redirect=<?php echo $newurl; ?>">Log Out</a>
<?php } ?>


They seem to dissect the code.. I would of course appreciates a comment on how this works. But where would I find the rules for writing functions like this.. Thank you
adri
Well the code for an if else-statement is :

Code:
<?php if (something = something else) {
then do this
}
else {
do that
}
?>


In your case this is:

Code:
<?php if ($user->data['user_id'] == ANONYMOUS) { ?>  //OPENING THE TAG
A FORM
<?php } //CLOSING THE TAG OF IF
else { ?> //STARTING ELSE STATEMENT
Log out link
<?php } ?> //CLOSING ELSE STATEMENT



adri
sonam
This is combination between HTML tags and PHP - inline scripting. In that case you don't need to worry about quotes, etc.
For example mixed:
Code:
<img class="<?php echo $class; ?> " src="images/some.jpg" />


is same like pure php:
Code:
<?php
echo '<img class="' . $class; .'" src="images/some.jpg" />
?>


In most situations I build all variables on load and then just echo in html where I need.

Sonam
Possum
Thank you everyone..

I hope my question wasn't too silly..
sonam
Possum wrote:
Thank you everyone..

I hope my question wasn't too silly..


No, it is not silly.

Sonam
kacsababa
It was beginner question, but no silly, everything has to be learned somehow.

I add a plus note on mixing PHP and HTML and using loops/conditional statements.

You can use another syntax with for/if/else/while, instead of using brackets you can use the colon syntax.

Example:
Code:
// with brackets
for ($i=0; $i<10; $i++) {
  // do something
}

// with colon
for ($i=0; $i<10; $i++):
  // do something
endfor;

This is no advantage usually, but if you break your php code with HTML for example then it becames a lot more readable.

Example:
Code:
// with brackets
<ul>
<?php for ($i=0; $i<10; $i++) { ?>
  <li><?php echo $i; ?></li>
<?php } ?>
</ul>

// with colon
<ul>
<?php for ($i=0; $i<10; $i++): ?>
  <li><?php echo $i; ?></li>
<?php endfor; ?>
</ul>


With a simple, short example as this it is not so obvious, but the end of the structured block is more readable this way.
This is ofcourse is in the php manual too everywhere where it's supported.
Fire Boar
The only silly question is the question not asked. I'd suggest going with what kacsababa suggested, because it does make code more readable. In this case:

Code:
<?php if ($user->data['user_id'] == ANONYMOUS): ?>
  <form action="<?php echo $forumurl; ?>ucp.php?mode=login" method="post">
    <input type="text" name="username" value="" /><br />
    <input type="password" name="password" value="" /><br />
    <input type="checkbox" name="autologin" value="1" /> Remember Me<br />
    <input type="hidden" name="redirect" value="<?php echo $newurl; ?>" />
    <input type="submit" name="login" value="Login" />
  </form>
  Not a member? <a href="<?php echo $forumurl; ?>ucp.php?mode=register">Register now</a>!
<?php else: ?>
  <a href="<?php echo $forumurl; ?>ucp.php?mode=logout&sid=<?php echo $user->data['session_id']; ?>&redirect=<?php echo $newurl; ?>">Log Out</a>
<?php endif; ?>


It looks like this is a modification of a phpBB3 template. Well, their code is hardly the shining example of solid programming: extensions and external tools are very tricky to write because of their lack of API for example. So I wouldn't worry too much about following their coding style for your own templates.
D'Artagnan
the main confusion i think is how to treat the "HTML" code in php. well i always say the following (starting from now) , anything which is not inside <?php ?> is output is like you saying print or echo when the stuff is not inside the php tags. so you are coding PHP and outputing "HTML" (or xml json etc)

it helps because, output will always be processed after the programming itself so

Code:
<?php if(1==1){ ?> <<real programing will be processed first
   <div>Hola!</div> << it's output and  will depend on the real programing
<?php }else{ ?>
  <div>No Hola!</div>
<?php } ?>


would be the same as

Code:
<?php if(1==1){ 
echo "<div>Hola!</div>";
 }else{
echo "<div>No Hola!</div>";
} ?>



i also like the : for outputing more then the conventional way
Related topics
How To : Improve Your PHP Programming
Show PHP Code
Php Code Trouble Set-up
PHP CODE INCLUSION
mySQL Query/PHP code - get the highest value...
my PHP code benchmark script that got some problems
PHP code snippit?
CSS for making images go through a php code.
Gradient Fill Cell
What is a good or bad php code?
I want PHP Code to ......
Help With Simple PHP Code Snipit
removing DirectAdmin part of URL makes php code work
Highlight PHP-code in a HTML-document
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.