Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Do not understand the use of php tag to open an if statement and another php tag to close the statement

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #205619
    Eben
    Participant

    <?php if(condition) { ?>
    <?php //do code ?>
    <?php } else { ?>
    <?php //do the code ?>
    <?php } ?>

    #205620
    Ilan Firsov
    Participant

    Well, neither do I :P
    For simple PHP script it’s just doesn’t look good, bloats the file and makes the code harder to read. simply use this:

    <?php
    if(condition) {
        echo "something";
        ...
    } else {
        echo "something else";
        ...
    }
    ?>
    

    unless you mix in HTML, then it’s better to close the PHP tag and write your HTML without the need of echoing and escaping characters:

    <?php
    if(condition) {
    ?>
    <div>
        <span><?= $someVarToEcho ?></span>
        ...
    </div>
    <?php
    } else {
    ?>
    <div>
        <p>Something else</p>
    </div>
    <?php } //endif ?>
    
    Also I prefer the use of: ([link](http://php.net/manual/en/control-structures.alternative-syntax.php))
    ```php
    &lt;?php if(condition): ?&gt;
        ...
    &lt;?php else: ?&gt;
        ...
    &lt;?php endif; ?&gt;
    

    this makes the code quite a bit more readable

    #205729
    Eben
    Participant

    thanks bro. but am also thinking if i should take an online course from Lynda.com need what to do

Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘Back End’ is closed to new topics and replies.