I have an if statement which is throwing a syntax error, unexpected T_VARIABLE error and I have no idea why.
The code:
<?php if($contentTop || $contentLeft || $contentCenter || $contentRight): ?>
<div>a bunch of html</div>
<?php endif; ?>
The variable contains eiter an empty string or a bunch of html.
I have tried changing the || to or but it doesn't seem to change anything. neither does opening and closing the if statement with curly brackets. It only seems to work when i only have one variable in the statement. I have also tried using !empty($contentTop) to check the variables.
The server is running PHP version 5.3.10-1ubuntu3.26 but as far as I can see, that should not change anything regarding if statements
EDIT:
On request of the full markup:
<?php
$contentTop = get_field('section2new_content');
$contentLeft = get_field('section2new_left');
$contentCenter = get_field('section2new_center');
$contentRight = get_field('section2new_right');
if( $contentTop || $contentLeft || $contentCenter || $contentRight ): ?>
<div class="bbh-inner-section teaser-3-col" id="section2temp">
<?php if($contentTop): ?>
<div class="grid-container top">
<div class="row">
<div class="col-sm-12">
<?php echo $contentTop; ?>
</div>
</div>
</div>
<?php endif; ?>
<?php if($contentLeft || $contentRight || $contentCenter): ?>
<div class="grid-container bottom">
<div class="row">
<div class="col-sm-4 left">
<?php echo $contentLeft; ?>
</div>
<div class="col-sm-4 center">
<?php echo $contentCenter; ?>
</div>
<div class="col-sm-4 right">
<?php echo $contentRight; ?>
</div>
</div>
</div>
<?php endif; ?>
</div>
<?php endif; ?>
As i noticed myself i have second if / || statement further down, which seems to work fine. It's also not that I mistakenly inserted a wrong type of space or whateever, I have manually rewritten the code multiple times
I tried #Poiz answer which worked. Being a bit suspicious i tried my own original code again, which now also worked. Human error once again. Maybe it was some illegal version of a space or tab.
Would you be open to trying something a little odd? Anyways, it is essentially the same thing you did except that we turned the if Statements into Ternary Operations. This is because like You said; you've tried almost all combinations of if without success, except when only one Condition is supplied.... The Idea here is simply to crunch down the Line that does the main Comparison to a single, simple Boolean Value, which can then be used within the Subsequent if. Perhaps this works for you since your Magical if works only in the Case you have but One Condition to test for...
<?php $state = ($contentTop || $contentLeft || $contentCenter || $contentRight) ? true : false; ?>
<?php if($state): ?>
<div>a bunch of html</div>
<?php endif; ?>
Related
This is the code that's creates the error.
<div>
<?php while($row2 = mysqli_fetch_assoc($get_comment)){?>
<div class="comment">
<p><?php echo $row2['comment']?></p>
</div>
<?php}?>
</div>
Problem is here <?php}?>. You should use an space between php tags like <?php } ?>
For example if you write like <?phpecho "Hello";?> This is invalid. Because compiler will not found any starting tag like <?php Because it is <?phpecho. In your case it is <?php}
Using this code:
<div class="venus"><span class="ricon">Established</span><span class="locko"><?php the_field('br_estd'); ?></span></div>
However, I need to wrap this within another PHP IF statement:
<?php $mista = get_field('br_presence'); if ( strval($mista) == 'Worldwide') {echo "PLACE CODE ABOVE HERE" ;} ?>
How could I syntax this?
PHP does have an alternative syntax format for control statements that work well within HTML, although it is generally recommended to use templates and keep logic out of your view as much as possible. That being said, you could do something like:
<?php if (strval(get_field('br_presence')) == 'Worldwide') : ?>
<div class="venus">
<span class="ricon">Established</span>
<span class="locko"><?php the_field('br_estd'); ?></span>
</div>
<?php endif; ?>
could be you are looking for
<?php
$mista = get_field('br_presence');
if ( strval($mista) == 'Worldwide') {
echo '<div class="venus">
<span class="ricon">Established</span>
<span class="locko"><?php the_field("br_estd"); ?></span>
</div>';
//echo "PLACE CODE ABOVE HERE" ;}
?>
I have following code.
<?php if(ot_get_option('wide') == 'on') : ?>
<div class="nothing">
<?php else : ?>
<div class="container">
<?php endif; ?>
Some other codes
</div>
Can I put two divs in if statement and close it with one div? Or what is the best practice?
Yes you can in this case, as there will always be one div tag and one closing div tag.
The only issue you might encounter is if you're using an IDE, it might validate your HTML and give you an error due to that, as it'll see two divs and only one closing tag.
If you want a more "proper" way to do it or you want to avoid what I've mentioned, you can do this:
<?php $myClass = null; ?>
<?php if(ot_get_option('wide') != 'on') $myClass = 'container'; ?>
<div class="<?php echo $myClass; ?>">
</div>
I am trying to do an if statement if a function returns true and the else if it doesn't
Basically i need to show a html class in the return 1. I have try that below but I got an error.
syntax error, unexpected 'if' (T_IF)
This is what I have tried
<div class="inner">
<div class="stat"
<?php $access->getAccess()
if (getAccess == 1) {
class="unpublished"
}
else {
}
?>
>
</div>
</div>
this is what the function looks like
public function getAccess()
{
return $this->access;
}
How can I do this right?
Try with:
<div class="stat <?php echo $idea->getAccess() ? 'unpublished' : ''; ?>"></div>
You PHP code isn't echoing anything, plus you are missing semicolons.
Try to simplify this a bit:
<div class="stat <?=$idea->getAccess() == 1 ? 'unpublished' : ''?>">
P.S. You need to put all your classes into the same attribute.
To call a function you need to use brackets:
$access = getAccess();//Not getAccess
I note that you call another getAccess function too so you may have meant to do:
$access = $idea->getAccess() ;
if ($access == 1) {
Edit: Others have pointed out you are also missing a ;.
You need to conceptually separate PHP from your HTML. What is happening is that your PHP is being interpreted by your web server resulting in a HTML document that will be delivered to the client. What you want is to add a class to the div if an $idea has access, something like this:
<div class="inner">
<div class="stat <?php echo $idea->getAccess() ? "unpublished" : ""?>">
</div>
</div>
This is equivalent to a much more complex (and in my opinion harder to read):
<div class="inner">
<div class="stat <?php
if ($idea->getAccess()) {
echo "unpublished";
}
?>">
</div>
</div>
You forgot a semi-colon:
getAccess();
I am working in a Drupal template. I want to know if two variables exist, and if they do not then do or show something else. I know how to do this for only one variable, but what is the correct syntax to look for two? My code is below.
<?php if (!empty($right) && !empty($left)): ?>
<div id="content-main">
<?php endif; ?>
I also tried it this way.
<?php if (!empty($right)&&($left)): ?>
<div id="content-main">
<?php endif; ?>
and this way.
<?php if (!isset($right)&&($left)): ?>
<div id="content-main">
<?php endif; ?>
None of them work, how do I fix this?
empty() doesn't check variable initialization, it only checks if it contains a defined set of values that are considered empty ( "", 0, for example).
Your third example is in the right direction, but needs a small tweak:
<?php if (!isset($right)&&!isset($left)): ?>
<div id="content-main">
<?php endif; ?>
The second conditional check after && requires its own isset() as well.
hope this helps.