php inside an "if"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sergio V Modest
    New Member
    • Oct 2010
    • 2

    php inside an "if"

    I am very new to PHP and this is my question:
    I have this:
    Code:
    if (something) {
        echo 'blablabla';
        echo '<button type="button" title="<?php echo $this->__('foo') ?>" class="button" onclick="setLocation('<?php echo $this->getUrl('foo') ?>')"><span><span><?php echo $this->__('foo') ?></span></span></button>';
    ] else
    why this isn't right? Not possible php inside an 'if'?
    Last edited by Atli; Oct 21 '10, 04:40 PM. Reason: Please use [code] tags when posting code.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    You seem to be opening and closing PHP blocks inside a PHP string, which is not going to work. When the PHP parser finds a <?php in your HTML it opens a "PHP block" and starts parsing everything it finds as PHP, until it finds the closing ?> tag.

    While inside a PHP block, you can access all PHP variables. Opening another PHP block is redundant and will cause errors.

    If you want to place a PHP variable inside a string (such as HTML output you want echoed) you can do:
    [code=php]<?php
    $name = 'Atli';

    # Either do:
    echo '<p>My name is: ' . $name . '</p>';

    # Or:
    echo "<p>My name is: {$name}</p>";
    ?>[/code]
    In the first version I use single-quotes, so I have to break out of the string and concat the variable to it. (The dot does that; adds two strings to each other.)

    In the second version I use double-quotes, which allows me to put the variable into the string. PHP searches the string for variables and replaces them with their appropriate values.

    Both versions print out: <p>My name is: Atli</p>

    Comment

    • Sergio V Modest
      New Member
      • Oct 2010
      • 2

      #3
      I think I didn't explain well: I want to show a button with a text if my question is right and another if not.
      Something like this:
      Code:
      if (something) {
          echo 'blablabla';
          echo '<button type="button" title="<?php echo $this->__('foo') ?>" class="button" onclick="setLocation('<?php echo $this->getUrl('foo') ?>')"><span><span><?php echo $this->__('foo') ?></span></span></button>';
      ] else
          echo '<button type="button" title="<?php echo $this->__('foo2') ?>" class="button" onclick="setLocation('<?php echo $this->getUrl('foo2') ?>')"><span><span><?php echo $this->__('foo2') ?></span></span></button>';
      Last edited by Atli; Oct 22 '10, 12:25 AM. Reason: Please use [code] tags when posting code.

      Comment

      • Jay Garland

        #4
        I personally choose the easy method, may not be the right one but if it's a script for yourself then why not =)

        So I'd do something like
        Code:
        if (something){ ?>
        <?php ALL PHP CONTENT HERE ?>
        <?php } else { ?>
        <?php OTHER PHP CONTENT HERE ?>
        <?php } ?>
        Sadly, I haven't come across an easier way to do it, but I don't read PHP tutorials much anymore.
        Last edited by Atli; Oct 22 '10, 08:50 AM. Reason: Please use [code] tags when posting code.

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Originally posted by Sergio V Modest
          I think I didn't explain well: I want to show a button with a text if my question is right and another if not.
          OK. If you apply what I said in my previous post to the code you posted, and replace the ] on line #4 with a }, then your code should do that.

          It's just a matter of setting up a simple IF-ELSE block to print the proper button.

          Originally posted by Jay Garland
          I personally choose the easy method, may not be the right one but if it's a script for yourself then why not =)

          So I'd do something like
          Code:
          if (something){ ?>
          <?php ALL PHP CONTENT HERE ?>
          <?php } else { ?>
          <?php OTHER PHP CONTENT HERE ?>
          <?php } ?>
          Sadly, I haven't come across an easier way to do it, but I don't read PHP tutorials much anymore.
          You don't have to close and open PHP blocks for each line. In fact, it's usually a pretty bad idea. (Makes it a nightmare to read the code.)

          Your code is exactly equivalent to doing:
          Code:
          <?php
          if (something){
            ALL PHP CONTENT HERE
          } else {
            OTHER PHP CONTENT HERE
          } 
          ?>
          It's also a good idea use templates and design patterns that promote separation of logic and presentation. Mixing the two usually generates pretty hard to maintain code. - A lot of PHP frameworks are build on the MVC pattern. A very good thing to get used to.

          Comment

          • ziycon
            Contributor
            • Sep 2008
            • 384

            #6
            You need do change your code to be formatted like so:
            Code:
            echo '<button type="button" title="'.$this->__("foo").'"/>';

            Comment

            Working...