0

I am trying to print the content of the variable and a <hr> tag after doing a check if the variable is not empty. The <hr> is getting echoed even if the variable is empty.

Here is what I have

<?php if (!empty($content['relationship_graph'])){ 
            print render($content['relationship_graph']);
            echo '<hr>';
          }
         ?>

Not very pro in PHP but looked at some documentation...Cant seem to figure out what I might be doing wrong

9
  • What does render() do? Can you echo out $content['relationship_graph'] inside your if statement? Commented Aug 9, 2013 at 20:04
  • 2
    try var_dump($content['relationship-graph']); Commented Aug 9, 2013 at 20:05
  • 2
    @Fred that's totally wrong. It will only echo if the variable is empty. You don't need an else statement if the only other option is to do nothing. Commented Aug 9, 2013 at 20:07
  • 1
    You don't need an else statement for not printing the statement. Commented Aug 9, 2013 at 20:12
  • 2
    Are you sure that $content['relationship_graph'] is really empty? Commented Aug 9, 2013 at 20:13

2 Answers 2

1

Value in it could be a blank space.

try..

<?php 
    if(!empty($content['relationship_graph']) && $content['relationship_graph']!=''){ 
        print render($content['relationship_graph']);
        echo '<hr>';
    }
?>
Sign up to request clarification or add additional context in comments.

3 Comments

@Rahul-- Thanks for posting the solution. Apparently it didnt really work. I got it working any way. Thanks :-)
@soum: if your issue was resolved, consider posting it as an answer to this question.
$content['relationship_graph']!='' after !empty() is silly if you know what !empty() means.
0

Here is what I ended up doing. It worked.

if($content['relationship_graph']['#markup']['length'] != 0){
                  print render($content['relationship_graph']);
                  echo '<hr>';
              }

1 Comment

Note that $content['relationship_graph'] is NOT empty because it is an array with at least one element name '#markup'. Only arrays with no elements inside are considered empty.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.