Welcome to WebmasterWorld Guest from 54.167.153.63

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

If.else in php

   
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.
9:04 pm on Jul 14, 2013 (gmt 0)



its looks like you are missing the echo statement.

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

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



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

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

WebmasterWorld Senior Member 5+ Year Member



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 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>" ;
6:23 am on Jul 16, 2013 (gmt 0)

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



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 Jul 20, 2013 (gmt 0)

WebmasterWorld Senior Member Top Contributors Of The Month



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";
 

Featured Threads

Hot Threads This Week

Hot Threads This Month