Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional Statements in PHP code Between HTML Code

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 ?

like image 908
0xff0000 Avatar asked Sep 28 '10 12:09

0xff0000


People also ask

Can you use conditional statements HTML?

How to Build HTML for Conditional Links. Like we learned in the above example, conditional statements are best used outside your the Insert Link html elements. You will want to define your primary condition, then build the content to display after. Use {% else %} for your fallback content, and end with {% endif %}.

What are the 4 conditional statements in PHP?

PHP Conditional Statements if statement - executes some code if one condition is true. if...else statement - executes some code if a condition is true and another code if that condition is false. if... elseif...else statement - executes different codes for more than two conditions.

Can HTML be embedded inside PHP if statement?

Yes, HTML can be embedded inside an 'if' statement with the help of PHP.

What is conditional statement in HTML?

Conditional StatementsUse if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false.

How to use conditional statements in PHP?

You can use conditional statements in your code to do this. In PHP we have the following conditional statements: if statement - executes some code if one condition is true. if...else statement - executes some code if a condition is true and another code if that condition is false. if...elseif...else statement - executes different codes for more ...

What is the use of if else statement in PHP?

The if statement is used to execute a block of code only if the specified condition evaluates to true. This is the simplest PHP’s conditional statements and can be written like: The if, else statement executes some code if a condition is true and another code if that condition is false.

What is the most common conditional statement?

The most common conditional statement is “if-then”. Conditional statements give you the power to control your program flow by branching. Branching is the ability to control a program to choose one of two or more tasks. Branching to change the flow of your program allows you to build a decision-making mechanism into your code.

Why are there colons at the end of PHP IF-IF-ELSE lines?

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:


2 Answers

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.

like image 80
Gumbo Avatar answered Sep 21 '22 12:09

Gumbo


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.

like image 34
Spudley Avatar answered Sep 21 '22 12:09

Spudley