- This topic is empty.
-
AuthorPosts
-
August 10, 2012 at 7:57 am #39313alextashParticipant
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.
August 10, 2012 at 8:53 am #107674SenffParticipantI 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.
August 10, 2012 at 9:31 am #107676alextashParticipantThank 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 #107727alextashParticipantPlease help!!! :(
August 11, 2012 at 4:58 am #107731kedarlasaneMemberIf 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
August 11, 2012 at 5:55 am #107733alextashParticipantThanks 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 #107747SenffParticipantIn 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 $something
would 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.
August 13, 2012 at 2:25 am #107828alextashParticipantThanks man so much. I really appreciate you help. I guess I need a little more practive to get used to it.
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.