views:

79

answers:

6

Hi ! I'm having a bit of an issue by using Conditional Statements in PHP separated by HTML code. This is the type of code I'm trying to write. This is a profile page and it should only be seen by the user whose profile it is (i'm using session variables for checking that) :

<?php if(check if user is logged in) ?>
<display users profile in html>
<?php else ?>
<display an error>

But this doesn't work. I also tried using the shorthand notation by putting a : at the end of the if and using the endif statement, but it didn't work. ( On an earlier project , the : method worked for foreach and endforeach so I thought I would try it out )

Any Ideas ?

A: 

Try putting in brackets to separate the conditions.

<?php if(check if user is logged in) { ?>
<display users profile in html>
<?php } else { ?>
<display an error>
<?php } ?>
Aaron W.
A: 

use brackets because to the php interpreter this spans multiple lines

<?php if(check if user is logged in)  { ?>
<display users profile in html>
<?php } else { ?>
<display an error>
<?php } // else ?>
Joe Hopfgartner
+7  A: 

You probably forgot the endif of the alternative control structure syntax:

<?php if(check if user is logged in): ?>
<display users profile in html>
<?php else: ?>
<display an error>
<?php endif; ?>

Omitting the braces as you wrote is not possible. It is only possible if it is followed by a regular statement.

Gumbo
+1: I prefer this syntax for readability.
RedFilter
+1 for using the `end*` / `:` methods for doing conditionals in HTML.
RobertPitt
eurk. I hate the "endif" syntax. Give me nice friendly curly braces any day. ;)
Spudley
+1. This is the only situation where the alternative syntax is more readable than using braces.
You
`<display users profile in html>` <- I think he meant some html code with many lines, so does a multi line html code read as a regular line in php?
Ruel
Stupid Me ! I mixed up the else and endif statements ! Thanks :)
Hardik Ruparel
@Ruel, yes I could manage to display a multi-line code in html..
Hardik Ruparel
+3  A: 

use braces { and }.

<?php if(check if user is logged in) { ?>
<display users profile in html>
<?php } else { ?>
<display an error>
<?php } ?>
Ruel
i preffer braces, especially when you use an IDE
Hannes
Thanks, that worked too :)
Hardik Ruparel
A: 

Your initial thought was correct. There are two ways of doing this, as per the PHP documentation:

<?php if($loggedin): ?> 
<p>User is logged in.</p>
<?php else: ?>
<p>User is not logged in.</p>
<?php endif; ?>

Or:

<?php if($loggedin){ ?> 
<p>User is logged in.</p>
<?php }else{ ?>
<p>User is not logged in.</p>
<?php } ?>
You
Thanks for making it loud and clear :) Cheers :)
Hardik Ruparel
+1  A: 

PHP has two styles of notation for if() blocks (and blocks in general).

Firstly, you have the wordy notation, which involves explicitly stating endif; at the end of the if() block. It looks like this:

if(whatever):
   do something
else:
   do something else
endif;

The colons at the end of the if and else lines are important, because otherwise PHP thinks you're using the other notation (below).

Secondly, you have the curly-braces notation, which looks similar to C or Perl style code, and looks like this:

if(whatever) {
   do something
} else {
   do something else.
}

With this style notation, you are allowed to leave the pairs of curly-braces off if your block is only going to be one line long. (I personally think it's bad practice to leave them off like this, but plenty of people swear by it, and it is perfectly valid syntax. But I've seen PHP get confused over single-line blocks when you're switching between PHP code and HTML; this is why I always prefer to use braces even if I'm only writing one line).

The problem in your case is that you've mixed the two notations. You's trying to us the wordy notation, but don't have the colons on the lines, so PHP thinks you mean the braces notation. But because the braces notation allows for the braces to be missed out, PHP is seeing your code as valid syntax, even if it isn't going to work quite as you planned.

Your solution is to tidy it up so that you are definitely using one syntax or the other. Either add braces ({ and }}) to the start and end of each block as shown in my example, or add colons and an endif; line.

So your code should look like one of these two examples:

<?php if(check if user is logged in): ?>
    <display users profile in html>
<?php else: ?>
    <display an error>
<?php endif; ?>

or...

<?php if(check if user is logged in) { ?>
    <display users profile in html>
<?php } else { ?>
    <display an error>
<?php } ?>

Hope that helps.

Spudley
Wow, that was quite elaborate. Thanks ! I'm <3'in !SO :)
Hardik Ruparel