Welcome to WebmasterWorld Guest from 54.163.54.95

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

If.else in php

     
8:56 pm on Jul 14, 2013 (gmt 0)

New User

joined:July 4, 2013
posts: 15
votes: 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.
9:04 pm on July 14, 2013 (gmt 0)

Full Member

5+ Year Member

joined:Aug 16, 2010
posts:225
votes: 11


its looks like you are missing the echo statement.

echo '<div>........</div>'
9:27 pm on July 14, 2013 (gmt 0)

Administrator

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

joined:Aug 10, 2004
posts:10563
votes: 15


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

you are also missing a closing ?>
9:50 pm on July 14, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Dec 13, 2009
posts:945
votes: 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.
9:59 pm on July 14, 2013 (gmt 0)

New User

joined:July 4, 2013
posts: 15
votes: 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>" ;
6:23 am on July 16, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 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.
5:20 pm on July 20, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member Top Contributors Of The Month

joined:July 19, 2013
posts:1097
votes: 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";