Embedded html php syntax error while nesting if-else - php

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'])) : ?>

Related

open&close php tags on each line, within arrays, functions

I collaborate with a web-programmer on a php project based in kirby cms, and he wants to open and close every line as such:
<main>
<?php /*php code here/* ?>
<?php /*more php here*/ ?>
...
Trying to follow this style, I found some errors in my code. The first is that it seems I canNOT do this in the middle of an array as such:
BAD CODE
<?php $oo = array( ?>
<?php 'h' => 100, ?>
<?php 'v' => 100, ?>
<?php ); ?>
but I can do it in the middle of a foreach loop as such:
<?php foreach ($p as $subp): ?>
<div id='<?= $subp->title() ?>'>
<?php endforeach; ?>
Are there any other cases such as array in which I canNOT do this?
/edit
According to the answer, there can only be tag-breaks within 'foreach', 'while', or 'if' blocks.
How about a 'foreach', 'while' or 'if' within a function? is that 'legal'?:
<?php
function myFunction($arg){
if($arg === 'this'): ?>
<?= '<p>yep</p>' ?>
<?php else: ?>
<?= '<p>nop</p>' ?>
<?php endif;
};
?>
And how about nesting if within foreach within function?:
<?php
function myFunction($arr){
foreach($arr as $val): ?>
<p><?= $val ?> <p>
<?php if($val === 'this'): ?>
<?= '<p>yep</p>' ?>
<?php else: ?>
<?= '<p>nop</p>' ?>
<?php endif;
endforeach;
};
?>
edit/
Thank you
you cannot "break out of php" mid statement. wline defining an array for example you cannot close the php tags. The only time when you can "break out" of php is between opening and closing a loop or an if/else statement. This actualy does not break the statement as <?php foreach: ?> is a complete statement whereas <?php foreach{ ?> is not. Here some examples of what you can do:
<?php if($this!=$that): ?>
{something}
<?php endif ?>
<?php foreach($things as $thing): ?>
{something}
<?php endforeach ?>
<?php $while($this): ?>
{something}
<?php endwhile ?>
I think you get the message. you must have complete statements within php tags, without interruptions.
P.S. Also avoid using the shorthand <? instead of <?php at all cost, moving your project to a different hosting or an upgrade of your hosting might break your project as per default short tags are not activated. <?= ?> shorthand is safe as this is unaffected by the setting for newer php versions.
P.P.S Do not listen to the guy who wants php in one line, this will make your code hard to read and maintain. Stand strong and write beautiful code :)
UPDATE: (after the update on the question from #Jaume Mal)
I did not mean the examples in my answer as exclusive but as examples of statements that are complete vs statements that are incomplete. (I also forgot to mention closing php tags mid fuction, wich also work but I despise and woudl strongly advise against.) So for example <?php function foo(){ is a complete statement of starting a function but (as the other cases with loops etc..) it needs a closing statement, in this case }. This is true for if / else or foreach and so on:
<?php if($this){ ?>
some code
<?php } ?>
is a valid code, as the code pieces within the php tags are complete statements.

How to separate switch-case block in different PHP tags

I'm trying to separate a switch-case code block in different PHP tags like so. but It's returning a Parsing error. Is there a way to separate them code lines like so? :
<?php $case = 1 ?>
<?php switch($case) { ?>
<?php case 1: ?>
...
<?php break; ?>
<?php } ?>
After rereading the switch-case documentation you could rewrite the code like so.
<?php
switch(1){
case 1:
?>
...
<?php break;?>
<?php case 2: ?>
...
<?php break;?>
<?php } ?>
Notice the the first case is inside the same PHP as the switch.

Alternative syntax for control structures in 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
} ?>

How to enclose HTML in a ternary operator condition as you would with a normal if statement

When I'm using the IF Statement, I normally do something like this:
<?php
if ($var ===0){ ?>
<section>We broke out of PHP</section>
<?php ////// we are back in PHP
} else {
}
?>
How can I archive the same thing with a ternary operator, something like:
<?php
$var ===0?: $_breakHere?>
<section>We broke out of PHP</section>
<?php ////// we are back in PHP
$_ContinueHere
} else {
}
?>
I'd put all the html block into a PHP variable, but I want to break our of PHP and back into it, for syntax reading purposes.
Syntactically, the expression with the ternary operator, is a single instruction, and you cannot break a single instruction across PHP tags, regardless of nesting PHP and HTML code blocks.
For example, the followings generate syntax errors:
<?php echo 1==2 ? "eq" : ?><?php "ne"; ?> // syntax error, unexpected '?>'
<?php $seven = ?><?php 7; ?> // syntax error, unexpected '?>'

Simple PHP syntax - How to get vSlider names to use post-ID

vSlider - in WordPress I'm using the function supplied in the FAQs:
<?php if(function_exists('vslider')){ vslider('vslider_options'); } ?>
And I'm trying to do this. So its knows to get the post-ID as its name. But its not working.
<?php if(function_exists('vslider')){ vslider('<?php the_ID(); ?>'); } ?>
You cannot nest <?php ?> inside an already open <?php ?>. That is unsupported and syntactically invalid. Just call the function in place.
Apparently, the_ID() is one of those Wordpress functions which prints to the output buffer without returning its value. To get the id returned where it can be useful in a function, use get_the_ID() instead.
<?php if(function_exists('vslider')){ vslider(get_the_ID()); } ?>
The syntax issue becomes more obvious when expressed as properly indented code.
<?php
if (function_exists('vslider')){
vslider(get_the_ID());
}
?>
Yes you can do this by removing the php tags
<?php if(function_exists('vslider')){ vslider('<?php the_ID(); ?>'); } ?>
should be
<?php if(function_exists('vslider')){ vslider(the_ID()); } ?>
and your the_ID() function should return a string at the end

Categories