Multiple If Statements using variable - php

I am trying to make some logic controls on input variables $ContentPicture1Title and $ContentPicture1URL. In particular I am trying to get two rules applied in a single if statement.
In the first piece of code was everything ok. Then I messed out the code as you can see in the second snippet.
First statement (OK):
<?php if (is_null($ContentPicture1Title)){ ?>
<div class="letter-badge"><img src="image1.jpg"></div>
<?php } else { ?>
<div class="letter-badge"><img src="image2.jpg"></div>
<?php }?>
Second (messed) statement:
<?php if (is_null($ContentPicture1Title) && ($ContentPicture1URL)){ ?>
<div class="letter-badge"><img src="image1.jpg"></div>
<?php } else { ?>
<div class="letter-badge"><img src="image2.jpg"></div>
<?php }?>
Thank you in advance for your help.

Unless $ContentPicture1URL is a boolean variable, you need to invoke a function that can be evaluated as a boolean and/or compare it to another variable/value.
e.g.
if(is_null($variable1) && is_null($variable2)) {
//Do something
}
or
if(is_null($variable1) && $variable2 == 5) {
//Do something
}

As you can read in official documentation you need to use the second construct:
<?php if (is_null($ContentPicture1Title) && $ContentPicture1URL): ?>
<div class="letter-badge"><img src="image1.jpg"></div>
<?php else: ?>
<div class="letter-badge"><img src="image2.jpg"></div>
<?php endif; ?>
This will run if $ContentPicture1URL is a boolean, otherwise you need to use compare operators ( ==, !=, &&, ||, etc.) or boolean functions (is_null()) in order to verify the condition properly.

Related

PHP if statement: Zero vs 'empty'

I'm creating a WordPress website using the Advanced Custom Fields plugin. I have a set of fields for tennis score results. In the template, I'm showing these fields like this:
<?php if(get_field('my_field')) : ?>
<?php echo get_field('my_field'); ?>
<?php endif; ?>
The problem is that some scores are zero, so they're not showing up. I understand that this is because 0 basically equals null, so the statement is false.
One solution I found and tried was this:
<?php if(get_field('my_field') !== false) : ?>
<?php echo get_field('my_field'); ?>
<?php endif; ?>
However, this means that empty fields now show up too, which is not desirable since there are a lot of fields that are intended to be hidden if empty.
So, my question is, is there a way to phrase an if statement that allows for zeros, while still returning false if the field is empty? Please note that some scores aren't purely numeric, with values like '6(1)'.
In order to check for empty strings you have to explicitly check them in your if condition.
<?php if(get_field('my_field') !== '') : ?>
<?php echo get_field('my_field'); ?>
<?php endif; ?>
The reason is 0, null, empty string, empty array all evaluate to (but are not exactly) false, in case of a boolean check.

In my case, i want to make a multiple menu using if statement. How can i make the other if statement work for another menu?

I've made one menu work here, the other is doesn't work. I want to make both menu accessible either the "persegi" or the "persegi panjang"
<div id="content">
<div id="kirikolom">
<?php
if(isset($_GET['menu']))
{
if($_GET['menu']="persegi")
{
input_persegi();
} elseif($_GET['menu']="persegi_panjang")
{
input_persegi_panjang();
}
}
?>
</div>
You need to use "==" to equal variable, if just use "=" you give a variable value, so your code must be :
<?php
if($_GET['menu']=="persegi"){
input_persegi();
}
else if($_GET['menu']=="persegi_panjang"){
input_persegi_panjang();
}
?>
try to print and check the result of print_r($_GET['menu']);
Maybe different output when you access another url so it's not match in you if else statment.

Strange thing on PHP simple IF

In my code there is some IFs that all are the same and working, Except one!
Here is the code
<?php
if($tmp['data']=='0')
{
?>
some code...
<?php
}
?>
<?php
if($tmp['data']=='1')
{
?>
some code...
<?php
}
?>
<?php
if($tmp['data']=='a')
{
?>
some code...
<?php
}
?>
<?php
if($tmp['data']=='b')
{
?>
some code...
<?php
}
?>
<?php
if($tmp['data']=='c')
{
?>
some code...
<?php
}
?>
$tmp['data'] is a value that is fetched from SQL database (varchar type).
IFs are working for all values, but when I set the value in phpmyadmin to 'c', related IF doesn't execute.
Any idea?
You should really use else if if more than one condition is never going to be true.
to debug try to comment everything else and just keeping the if of c, make sure it is not case sensitive (eg. server may return C, but you may check for c)
Try this peice of code to see if it works (though its bit complicated)
if( strcasecmp((string)$tmp['data'],"c")==0)
{
.. code here
}

PHP Show menus/submenus according to the value of a variable

I have a navigation bar in which I am trying to show menus/buttons, according to the type of user. I get the type of user via a variable called $isManager.
The good news is that it works on every browser, except firefox.
Code looks like this:
<?php
if ($isManager === '2'){
?>
<li>View</li>
<?php
}
?>
Can you suggest an alternative to this, or is Firefox somehow ignoring or not accepting the true condition here ?
When you use ===, it is for strict checking. So make sure that your$isManager is string type. If it is integer then try
<?php
if ($isManager === 2){
?>
<li>View</li>
<?php
}
?>
You are Using === it means you want to check by its typeof too.
and after that you wrote '2', so it will missmatch the results and not going to the condition, instead try the following.
<?php
if ($isManager === 2){
?>
<li>View</li>
<?php
}
?>

php if foreach statement works, but else message not displaying

I'm getting an array of data from my model, and am using a php foreach statement to display it in a view. I'm adding another layer of logic to show only certain bookmark_ids.
The data is displaying fine; but for some reason the "else" message (tied to the first if clause) isn't showing if no data is returned. I need to figure out how to get that message to display.
<?php
if ($bookmark):
foreach($bookmark as $b): ?>
<?php if ($b->bookmark_id != 0) { ?>
<li><?php echo $bk->user_id; ?> <?php echo $bk->bookmark_name; ?></li>
<?php } ?>
<?php
endforeach;
else:
print "Your bookmark list is empty.";
endif;
?>
You a testing if $bookmark exists! I assume that it always exists, either empty or with array of values!
Try this:
<?php
if (is_array($bookmark) && count($bookmark)>=1):
foreach($bookmark as $b): ?>
<?php if ($b->bookmark_id != 0) { ?>
<li><?php echo $bk->bookmark_name; ?></li>
<?php } ?>
<?php
endforeach;
else:
print "Your bookmark list is empty.";
endif;
?>
Read: PHP is_array() | count()
EDITED
Related to the recently posted comment "Yes, the array is returning results; I am using the second if statement to limit what is shown. It sounds like my else statement should be tied to the second if clause, instead of the first. The issue for me isn't whether there are results; it's whether after the results are filtered anything remains.":
<?php
// reset variable
$count = 0;
// if it is an array and is not empty
if (is_array($bookmark) && count($bookmark)>=1):
foreach($bookmark as $b):
if ($b->bookmark_id != 0) {
echo '<li>' . $bk->bookmark_name . '</li>';
} else {
$count++; // no data, increase
}
// check if the counter was increased
if ($count>=1) {
print "Your bookmark list is empty.";
}
endforeach;
else:
print "bookmark not found.";
endif;
?>
For one reason or another, $bookmark is evaluating to true. Since empty strings and arrays already evaluate to false in PHP, we might reasonably suppose that $bookmark is in fact an object. Since we can iterate over it with foreach, it is probably an instance of ArrayObject, or a class that extends it. We could debug the exact type of $bookmark by writing:
var_dump($bookmark);
Since an empty object instance evaluates to true in a PHP conditional, we need to be more specific about what we're checking for.
if(count($bookmark) > 0):
This should trigger the else condition properly. As a side note, you should really indent your code properly. :)
if (is_array($bookmark) && !empty($bookmark)) {
foreach ($bookmark as $item) {
...
}
} else {
echo "Your bookmark list is empty.";
}

Categories