PHP and HTML

  PurplePenny 21:59 30 Apr 04
Locked

I've seen two different ways of "mixing" (for want of a better word) PHP and HTML. I know which one makes more sense to me but I'd be interested in what others felt.

Method 1:

?php

PHP stuff

?>

HTML

HTML

HTML ? little bit of PHP?>HTML

?php

more PHP

?>

Method 2:

?php

PHP stuff

print " HTML HTML HTML"

little bit of PHP

print "HTML"

more PHP

?>

That's simplified but I hope that you get the picture.

Penny

  PurplePenny 22:38 30 Apr 04

... a question....

Would the two techniques be used in different circumstances and if so when?

  PurplePenny 23:29 30 Apr 04

Is this the answer?

If the HTML occurs inside a pair of PHP curly brackets it has to be written as print ("HTML");

(If I were a patient person I'd just wait until the course in a couple of weeks time.)

  Taran 23:49 30 Apr 04

You've asked a big question in just a few words Penny.

In brief:

PHP print is, on most standard {if there is such a thing} web servers, marginally slower than echo. You get the odd curved ball where an echo statment gets processed really quickly, but in general echo is slower than print.

I know you haven't mentioned echo but I'm leading up to something - humour me for now.

Now, because of its inherent speed advantage, using print on a large web application decreases server load and increases response times. So far so good.

I myself prefer to use the first method you outlined, for a number of reasons.

1. It's how I cut my teeth on PHP and old habits die hard ;o)

2. All programmers establish a coding standard and most try to follow a standard that would allow another developer to walk straight in and continue where they left off.

3. While print has its uses I find the

<?php ?>

<html>

<?php ?>

<html>

method to be far easier to read, far simpler to code, and largely more efficient to work with while coding.

4. I've yet to have a convincing argument put before me in favour of using print to output all HTML to the browser.

There are other factors that spring to mind but, from a purely personal point of view, I rather prefer, where possible, keep things as simple as I can to deliver as much output in as efficient a manner as I am able.

If I was convinced that print was, in fact, the best method to use I'd use it. I'm not, so I don't.

There you go.

I've seen a lot of code in the past that read along these lines:

<?php

print "<html><head>";

print "<title>Page Title</title>";

print "</head>";

print "<body><p>Lots, and lots of text copy:</p>";

blah, blah, blah...

Anyone who can convince me that such bloat is the best road to take better have a very, very strong argument.

I like simplicity and efficiency. I strive for maximum output from minimum input - which makes me a lazy hack by default...

To bring me back to my point, this is why I more often than not code up a PHP page that consists of:

<?php require_once('database connection details'); ?>

<?php include ("header.inc"); ?>

<?php include ("navigation.inc"); ?>

<html>

<?php page body content ?>

</html>

<?php include ("footer.inc"); ?>



The four PHP blocks connect me to my database and load the documents header, footer and navigation structure into the page. All that's left is the HTML middly twiddly bit, and its content is normally pulled from the database anyway with further PHP code.

I'm not sure if I've answered your question or just raised more issues, but there we are.

T

  Taran 00:03 01 May 04

I think I totally misunderstood your question.

*sigh*

You can include variables and arrays into a print statement, and for general use print works with or without the parentheses.

<?php

print("Hello Penny");

?>

is the same as

<?php

print "Hello Penny";

?>

I'm struggling to try and think of a situation where using print for HTML would be a better choice, although much would depend on what you want to see on your page(s).

The whole point for most web pages is to seprate your PHP and HTML as much as possible. By doing so you normally build in efficiency and you make life a little easier for the web server which scans through the document, completely ignoring the HTML, in favour of processing the PHP blocks it finds.

  PurplePenny 00:50 01 May 04

I think you understood it the first time :-)

The first method is the one that makes sense in my brain.

Is it OK to separate the two ends of a PHP curly bracket pair thusly :

<? if something { ?>

HTML

<? } else { ?>

HTML

<? } ?>

(I'm busy fiddling with a package that I bought which uses the second method. The developer runs a forum where users can post mods and I'm working on one that a lot of people seem to want. I've almost got it but can't quite pinpoint where one particular action happens. I think that I might understand better if I can separate the PHP and HTML a bit more.)

  PurplePenny 00:54 01 May 04

I think I'm still not really explaining it clearly so I'll go to bed and try again tomorrow.

(Up early tomorrow .... err ... this morning - May Morning, a big celebration in Oxford but it means getting up around 4am ... so that would be three hours away)

  Talented Monkey 02:29 01 May 04

I think I have understood what you meant penny.

Yes you can break in and out of php and html in the way you described However with certain sections like IF ELSE statements this is a very messy way of doing things. If you only need one line for output in HTML within the IF ELSE then I would use the print or echo eg.

<?php
$name="penny";
if ($name=="penny"){

print ("<p> hello penny </p>");

}else{

print ("<p> who are you?</p>");

}

?>

However if you require multiple lines then personally I would write a separate HTML file and then call it as an include . so if the name matches we load a personal page otherwise we load a visitors page eg

<?php
$name="penny";
if ($name=="penny"){

include $include_path."pennys_page.html";

}else{

include $include_path."visitors_page.html";

}

?>

the following would work too, the way you suggest, but as you can see it is rather messy and somtimes confusing compared to the above.
<?php
$name="penny";
if ($name=="penny"){

?>
<p> hello penny </p>

<?php }else{ ?>

<p> who are you?</p>

<php

}

?>
hope this helps.

  PurplePenny 09:33 01 May 04

Thanks Taran and Talented Monkey, that answers my questions. Good, now I can carry on fiddling with this package.

I'm determined to get my modification to work so I can post it to the user forum. It half works. Or more to the point it works the first time a calculation is performed but not the second time. I even know why it doesn't work - a variable has a value the first time but is empty the second time through. The other variables still have their values but not this one. So I need to get the code into a form that has a logical flow to my brain so that I can follow what is happening. (I took the calculation out and made it into an include - I was dead chuffed at finding that one out for myself!)

BTW - Taran, you recall suggesting that I ask the IT folk at work about loading ISS and installing PHP and MySQL? I e-mailed and asked them whether I could do it myself and they've said yes; provided I give a solemn oath, signed in blood, that I'll keep Windows and Sophos up to date (because they've had some security problems with ISS on unpatched machines). With luck I'll get the chance to do it all some time next week. IT are so busy that they are only too glad to have someone ask to DIY istallation :-)

Thanks again,

Penny (Housework? What housework? There's code to be fiddled with .......)

This thread is now locked and can not be replied to.

How to get Windows 10 for free | How to install Windows 10: There is still a way to avoid paying…

1995-2015: How technology has changed the world in 20 years

Hands-on with the Star Wars fighting drones you can fly yourself

15 macOS Sierra tips | How to use macOS Sierra: Secret tricks and best new features in Apple's new…