treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Code verification

  • I want to display the content if it is available and if not display "coming soon"

    Is this right? It works, I just want to make sure it is the correct way and most efficient way.

    <?php if($content = $post->post_content ) {
    echo "<span class='titles'>Description:";
    echo "</span>";
    echo the_content();
    } else {
    echo "<span class='titles'>Coming Soon</span>";
    } ?>
  • I think you're statement will always be true since you're declaring a variable within that if. 1 = is declaration, two is equal to.

    Also, you need to see what $post->post_content would look like if it was empty, is it an empty string?

    If it's an empty string the statement would start with:
    <?php if($post->post_content != '' ) {
    Otherwise it would be:
    <?php if($post->post_content ) {
  • It is an empty string. Basically I want to get
    the_content
    if any content exist. If it does not, I want to get "Coming Soon"

    I was just making sure it was clean or if there was a better way. Thanks!
  • If the_content() doesn't exist and it returns an empty string, you should have this:
    <?php if($post->post_content != '' ) {
    echo "<span class='titles'>Description:</span>";
    the_content();
    } else {
    echo "<span class='titles'>Coming Soon</span>";
    } ?>
  • Awesome! Thanks so much for the help. Sincerely appreciated.
  • If you dont mind? What is the difference between besides $content being a variable.

    <?php if($content = $post->post_content )


    <?php if($post->post_content != '' )
  • if($content = $post->post_content )
    will always be true. You are defining a variable.
    $a = 10;
    $b = 20;

    if ( $a = $b ) {
    echo 'true';
    } else {
    echo 'false';
    }
    You will get true every time, because you are setting the variable, not asking if $a is equal to $b. You are saying $a IS $b, and it goes 'well, that's true'.

    As for the != '' thing, I'm not 100% sure if php sees an empty string as truey or not (I'm more of a js guy, but php and js are basically the same). Now that I think about it, it probably doesn't, so you could just go:
    if($post->post_content)
    but just be sure to check that it gives you the result you want.
  • Thanks a lot for explaining it. It worked both ways. So I am assuming the !='' is saying this is empty. I have been schooled :)