Using conditional statement to switch out content blurbs?

by Synnuh
5 replies
I've spent the last 4 hours trying to maneuver around a 500 code, to no avail.

I'm trying to get the homepage to display the_content() and a paywall page to show the PayPal subscribe buttons and a small blurb of copy.

Here's the code I'm using right now:

PHP Code:
<?php if ( is_front_page() ) : { ?>

                        <?php the_content(); ?>

<?php elseif( is_page('49')) : ?>

<div style="width:150px;height:25px;">
<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="YN5GWV4G5WMB8">
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_cart_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!" width="300px" height="25px">
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form></div>

<?php else : ?>

<?php endif; ?>
I know I'm missing something, but PHP isn't my strong suit. ;/ Can anyone lend me some insight into what's missing?
#blurbs #conditional #content #statement #switch
  • Profile picture of the author Synnuh
    Edit. Figured it out. The { in the first line was throwing me off.
    {{ DiscussionBoard.errors[10608946].message }}
  • Profile picture of the author noah.whitmore
    Conditional statements like this...

    PHP Code:
    <?php if ( $variable ) : ?>

      <p>This is some text that will be displayed if true.</p>

    <?php else : ?>

       <p>This will be displayed otherwise.</p>

    <?php endif; ?>
    are using what is called the alternative control structure syntax (http://php.net/manual/en/control-str...ive-syntax.php). It's pretty sweet. WordPress uses this all over, if you want to learn how to use this syntax practically - just pop open the hood and take a look at the files that come packed with WordPress. It makes your code much more readable in my opinion. It appears that the code you posted is from WordPress, so that's why I mention it.

    I know that you have gotten this particular issue figured out, but I just want to comment on it because this issue is pretty common. Developers will switch back and forth between the standard and alternative syntax within the same application. They'll have one control statement that uses brackets - the standard control statement syntax...

    PHP Code:
    <?php

    if ( $a $b ) {
      echo 
    'Here is some text that will be displayed';
    }

    ?>
    and they will also use the the alternative syntax somewhere down the line, maybe even in the same PHP file. But, at some point they might accidentally mix them. They will use the alternative syntax but accidentally put in a bracket...

    PHP Code:
    <?php if ( $foo $bar ) : { ?>

      <p>Here is some text and some junk!</p>

    <?php endif; ?>
    That seems to be what has happened in your situation. All too common a mistake and one that I have made a few times as well. That's why it's very important to stick to one syntax throughout your PHP file.

    -Noah
    Signature
    No Pitch For The Moment - Just A Nice Hello.
    So... 'Hello'
    Feel free to PM me if you have any questions about my posts. I'd like to hear from you!
    {{ DiscussionBoard.errors[10610467].message }}
  • Profile picture of the author Synnuh
    That's my problem, I think.

    I've never actually taken the time to learn the structure of PHP. I've always just hacked code together until I don't get errors lol -- my downfall!

    Thanks for the in-depth explanation. Do you happen to know a resource tailored to helping beginners get their bearings in PHP?
    {{ DiscussionBoard.errors[10610509].message }}
    • Profile picture of the author noah.whitmore
      Originally Posted by Synnuh View Post

      That's my problem, I think.

      I've never actually taken the time to learn the structure of PHP. I've always just hacked code together until I don't get errors lol -- my downfall!

      That's how we all start, haha. I wish that someone would have explained a few basic principles to me early on...
      • Learn the foundation of how to code
      • Bad habits become very hard to break - so do things correctly from the start
      • Don't just hack your way through it because you will always end up better in the end if you KNOW what you're doing
      • Any code that looks TOO confusing is the exact type of code that you need to figure out - I mean, line by line, figure out EXACTLY what that confusing code is doing. That's the best way to learn
      • Copy and pasting seems easiest, but once you hack enough copy/paste jobs together to create a "working" script, you will realize that it would have been easier if you would have just learned what the code was doing INSTEAD of just copying and pasting. If you understood what the code was doing, then you would have been able to create that entire script by hand yourself!


      Originally Posted by Synnuh View Post

      Thanks for the in-depth explanation. Do you happen to know a resource tailored to helping beginners get their bearings in PHP?
      Yes, I know of a few...

      First off, people put it down pretty often because they really dumb stuff down, but I really think that w3Schools is a great resource for beginners. They have a pretty good PHP turorial - PHP 5 Tutorial

      Also, here's one of the tutorials that I used when I first learned PHP - PHP Tutorial - Introduction - it's pretty solid!

      If you want to get info straight form the source, try out the official PHP documentation's 'PHP: A simple tutorial' - PHP: A simple tutorial - Manual - it gets a little 'wordy' though which is why I wouldn't necessarily recommend it to an absolute beginner.

      However, after you learn PHP, you will find yourself referencing the official documentation about 100 times each day - PHP: PHP Manual - Manual - there is a search field in the top right corner of the official docs page that you can use to search for specific PHP functions. I must have used that search box 100,000 times by now.

      - Noah
      Signature
      No Pitch For The Moment - Just A Nice Hello.
      So... 'Hello'
      Feel free to PM me if you have any questions about my posts. I'd like to hear from you!
      {{ DiscussionBoard.errors[10611674].message }}
  • Profile picture of the author Synnuh
    Right on, gracias!

    I'm not completely new -- grew up on VB, ASP, HTML, SSI and CSS. Tried learning CGI and Perl then PHP came around and I abandoned it.

    As far as PHP goes, I dunno what does what, or the syntax to use, so I guess I'm a newb to it lol

    Finally in a spot where I can devote some hours to actually learning code again.

    I'd love to be able to crank out SaaS that solve my own problems, then sell them to other people.
    {{ DiscussionBoard.errors[10612452].message }}

Trending Topics