...

View Full Version : Php Tags Basics



jamescool
04-23-2012, 05:24 AM
Hi friends I cannot understand why we need the close curly braces in opening and closing php tags.

kindly have the code below as the referrence:
im trying to implement this with a login script where user can be a guest,ordinary member and an admin...


<?php
include "dbc.php";
if (isset($_SESSION['user_id'])) {?>



<span class="style2"><a href="mysettings.php">Edit Profile</a> | <a href="logout.php">Logout </a></span>

<?php
if (checkAdmin()) { ?>
| <span class="style2"><a href="admin.php">Control Panel</a></span>


<?php
}
} else {
?>
<span class="style2"><a href="login.php">Login</a></span>
<?php
}
?>

so that if user is admin, Control Panel will be before edit Profile and Logout...
the visual thing is like the links below..

if admin:

Control Panel | Edit Profile | Logout

if not admin:

| Edit Profile | Logout

the implementation should be easy and the logic is very simple...im just having a problem with the php syntax...can u explain a little about the php tags...closed after curly braces etc..THanks Much!

litebearer
04-23-2012, 12:22 PM
Its NOT that the curly brace must follow the php tag, its that you 'dropped' out of php.

the following would need no curly braces
start php
do some coding
drop out of php
do some html
start php again

while this
start php
some condition { this is the opening brace for the condtion
do some php
} closing brace for the condtion

and finally
start php
some condition {
drop out of php
some html
start php
}

clear as mud?

abduraooft
04-23-2012, 12:35 PM
If the HTML blocks are very small, then it'd be better to use a single PHP tag and convert all html to strings, like


<?php
include "dbc.php";
if (isset($_SESSION['user_id'])) {

echo '<span class="style2"><a href="mysettings.php">Edit Profile</a> | <a href="logout.php">Logout </a></span>';


if (checkAdmin()) {

echo '| <span class="style2"><a href="admin.php">Control Panel</a></span>';

}
} else {

echo '<span class="style2"><a href="login.php">Login</a></span>';

}
?>

jamescool
04-23-2012, 01:47 PM
yup..thats what ive done after I was advised by a friend..


the code below:

<?php
include "dbc.php";


if (isset($_SESSION['user_id']))
{


if (checkAdmin())
{



echo '<span class="style2"><a href="admin.php">Control Panel | </a></span>';
}

echo '<span class="style2"><a href="mysettings.php">Edit Profile</a> | <a href="logout.php">Logout </a></span>';


}

else

{

echo '<span class="style2"><a href="login.php">Login</a></span>';

}


?>

thanks anyway



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum