Give help. Get help.

  • # August 10, 2012 at 7:57 am

    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:

    < ?php if(conditions) { ?>... HTML CODE ...< ?php } ?>

    Why is this syntax correct? I thought this would cause an error, because < ?php if(conditions) { ?> is not correct “if ” syntax (should end with “}”). I know you may say that we are ending “if ” with new < ?php } ?> 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:

    < ?php echo?>< ?php $some_variable; ?>

    This correct version is this

    < ?php echo $some_variable; ?>

    I’m wondering what’s going on here.

    # August 10, 2012 at 8:53 am

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

    < ?php if(conditions) {
    } ?>

    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:

    < ?php if(conditions) {
    < ?php
    } ?>

    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.

    # August 10, 2012 at 9:31 am

    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

    # August 11, 2012 at 1:54 am

    Please help!!! :(

    # August 11, 2012 at 4:58 am

    If you want embed php code in html.just open

    < ?php //

    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 Lasane's blog

    # August 11, 2012 at 5:55 am

    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).

    # August 11, 2012 at 6:36 pm

    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:

    < ?php
    if ($var==3) {
    echo 'The variable is 3';

    But this if there’s a lot of lines:

    < ?php
    if ($var==3) {


    Here are some of the results:

    The variable is 3.

    < ?php

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

    # August 13, 2012 at 2:25 am

    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)

You must be logged in to reply to this topic.