Forums

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

Home Forums Back End Please Help the Beginner? Embedded PHP in HTML.

  • This topic is empty.
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #39313
    alextash
    Participant

    Hi everyone, I’m trying to learn php (after learning html and css) but can’t really understand the syntax of embedding php into html.
    Here’s the case:

    ... HTML CODE ...

    Why is this syntax correct? I thought this would cause an error, because is not correct “if ” syntax (should end with “}”). I know you may say that we are ending “if ” with new block; I cant understand how this seperation works and how do we split php code like this. Ok, if above example is correct , then why is the following is incorrect:

    This correct version is this

    I’m wondering what’s going on here.

    #107674
    Senff
    Participant

    I know how this could be confusing, but this is basically how it works….

    
    [DO PHP STUFF]
    } ?>

    Now, instead of [DO PHP STUFF] you can obviously put some PHP code there. But if you want plain old HTML, that’s allowed, so you’d get this:

    
    ?>
    [DO HTML STUFF]
    } ?>

    So basically, you keep the PHP logic/functionality, but you just close the PHP (temporarily) to enable some HTML and then open the PHP again.

    Your second example with the echo won’t work because you’re “breaking” the PHP logic in the middle, so to speak.

    Hope this helps.

    #107676
    alextash
    Participant

    Thank you very much Senff. Yes, you are right it is confusing. I didnt understand what you meant when you said “Your second example with the echo won’t work because you’re “breaking” the PHP logic in the middle”. Can you please explain that in more detail. Thanks. Appreciate your help

    #107727
    alextash
    Participant

    Please help!!! :(

    #107731
    kedarlasane
    Member

    If you want embed php code in html.just open

    here you can add whatever php code. i.e

    echo "this displays texts or tags of html";

    After Coding close the php block by following tag.

    ?>

    Remeber to add semicolon(;) after every php statement.


    Kedar
    Kedar Lasane's blog

    #107733
    alextash
    Participant

    Thanks kedarlasane, but I know this structure too. I’m just confused with the one I posted. I know I could use the the way you mentioned but I dont really want to mix php code with html ( I mean using echo and prints).

    #107747
    Senff
    Participant

    In theory, you can “break” PHP and switch back to HTML as long as you don’t break it in the middle of a line. echo $somethingwould be a line that you can’t break, for example; you also can’t break in the middle of a line like this: if ($var==3) {, but it’s fine to break the PHP after that whole line.

    In general, I try to avoid breaking at all and use echo to write out HTML, but when the HTML would be a lot of lines, I do switch. So, like this for one line:

    
    if ($var==3) {
    echo 'The variable is 3';
    }
    ?>

    But this if there’s a lot of lines:

    
    if ($var==3) {
    ?>

    Results


    Here are some of the results:


    The variable is 3.



    }
    ?>

    Silly example maybe, but hope you know what I mean.

    #107828
    alextash
    Participant

    Thanks man so much. I really appreciate you help. I guess I need a little more practive to get used to it.

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