homepage Welcome to WebmasterWorld Guest from 54.146.190.193
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
If.else in php
ksklos



 
Msg#: 4593083 posted 8:56 pm on Jul 14, 2013 (gmt 0)

I have a home page and I want to put links on the page. Depending on who is logged in I want some links hidden and others shown. I have gotten this far (see code) but I keep getting the error: PHP Parse error: syntax error, unexpected '<' on line 33.


Welcome back <?= $fgmembersite->UserID(); ?>!

<p><a href='change-pwd.php'>Change password</a></p>
<?php
$ID = $fgmembersite->UserID();

if ($ID>1)
{
echo "View only links here";
}
else
{
<div style="display: none;"><p><a href='access-controlled.php'>A sample 'members-only' page</a></p></div>;
}


It works fine without the <div> statement and I know that is my problem because besides the error the color coding of my text editor is all wrong. I couldn't figure out from googling how to code the <div> so that it will work. Thanks for any help.

 

bhukkel



 
Msg#: 4593083 posted 9:04 pm on Jul 14, 2013 (gmt 0)

its looks like you are missing the echo statement.

echo '<div>........</div>'

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4593083 posted 9:27 pm on Jul 14, 2013 (gmt 0)

you should be using double quotes for your html attribute values.

you are also missing a closing ?>

Readie

WebmasterWorld Senior Member



 
Msg#: 4593083 posted 9:50 pm on Jul 14, 2013 (gmt 0)

Single quotes validate just as well as double quotes in HTML. Infact, I think even no quotes will validate (Although that does somewhat limit what you can have in your attributes, and will affect readability). It is simply more common to find HTML using double quotes.

A final ?> is also not required at the (absolute) end of a script, it's just neater.

ksklos



 
Msg#: 4593083 posted 9:59 pm on Jul 14, 2013 (gmt 0)

Thanks for the help. I took the <div> out since I realized that I could control what links to show with the If...Else statement. This worked: echo "<p><a href='access-controlled.php'>A sample 'members-only' page</a></p>" ;

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4593083 posted 6:23 am on Jul 16, 2013 (gmt 0)

If you stick to double quotes in HTML output and single quotes in PHP syntax, you'll make less coding errors in the long run.

JD_Toims

WebmasterWorld Senior Member Top Contributors Of The Month



 
Msg#: 4593083 posted 5:20 pm on Jul 20, 2013 (gmt 0)

If you stick to double quotes in HTML output and single quotes in PHP syntax, you'll make less coding errors in the long run.

^^^ This

Also, PHP parses double quoted strings for variables/special characters, but not single quoted strings, so this:

echo '<p><a href="access-controlled.php">A sample "members-only" page</a></p>';

is faster/more efficient code than this:

echo "<p><a href='access-controlled.php'>A sample 'members-only' page</a></p>" ;

ADDED NOTE: '\n'; will not produce a new line, because it's not parsed, so to add a new line in a single quoted string the string needs to contain a new line or a double quoted \n (or \r) needs to be concatenated on the string.

THESE BOTH WORK:
'stuff
';
'stuff'."\n";

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved