1

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?

2
  • 1
    if (isset($right) && isset($left)) otherwise make var_dump($right,$left); and check the content of the variables Commented Feb 16, 2012 at 3:34
  • I altered it a little. I didn't show 2 other if statements I also need to pass thru. The final code is <?php if (empty($right) && empty($left)): ?> <div id="content-main"> <?php else: ?> <?php endif; ?> Commented Feb 16, 2012 at 4:04

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.