Re: [PHP] Couple of beginner questions



On Sat, Jan 10, 2009 at 09:46:14AM -0500, tedd wrote:

At 8:48 AM -0500 1/10/09, Gary wrote:
Looks like a great link, thank you.

But am I to understand that all I need to do is change the extention on a
file to php from html for all to be right with the world?

Yup.

By changing the suffix (extension), you are telling the server that
this file is to be treated differently than a html file. As such, the
php interpreter will process the file before it is sent to the
browser.

A statement like:

<?php echo('Hello'); ?>

Will print "Hello" to the browser. In fact, the browser will never
see your php code unless you make a mistake. The file will be
processed and delivered to the browser as html.

Please note that my use of echo above does not require the (), that's
a habit I practice for no good reason whatsoever other than I like
it. I think it's because I'm dyslectic and it makes sense to me.

In any event, I would consider the following"bad practice":

<?php echo('<h1>Hello</h1>'); ?>

Whereas, the following I would consider "good practice".

<h1><?php echo('Hello'); ?></h1>

As best as you can, try to keep php and html separate.

I know that some have different ideas on "good/bad" practices, but
you'll develop your own views/habits as you grow and learn.

And let me present an alternative perspective. Never do something like:

<?php echo 'Hellow world'; ?>

Let Apache (or whatever) interpret HTML as HTML, and don't make it
interpret PHP code as HTML.

Instead, do:

<h1>Hello world</h1>

If you're going to use PHP in the middle of a bunch of HTML, then only
use it where it's needed:

<h1>Hello <?php echo $name; ?></h1>

The contents of the PHP $name variable can't be seen by the HTML, which
is why you need to enclose it in a little PHP "island". Naturally, if
you're going to put PHP code in the middle of a HTML page, make the
extension PHP. Otherwise, Apache will not interpret the PHP code as PHP
(unless you do some messing with .htaccess or whatever). It's just
simplest to call a file something.php if it has PHP in it.

Paul
--
Paul M. Foster
.



Relevant Pages

  • Re: How do we get there from here?
    ... > then sub the whole of that generated markup into the template? ... layed out on the fly, a simple IMG tag, or even an entire HTML document. ... PHP scripting provides 10 times the features of both of these ... idea as tokens can eliminate a huge amount of maintance, ...
    (comp.databases.pick)
  • Re: query string passing woes........ help... please....
    ... |> | offer any help other than saying that my validation could be FAR more ... I'm a total newbie at php. ... The easiest way for you would be to make the html form called form.php ... $_SESSION array using the same names. ...
    (alt.php)
  • Re: HELP - Cant change Include Path
    ... Here is my php.ini path for the php.ini in both php and sql dirs: ... which did work on the remote linux server and sent me some mail ... Although my gmail acct picked the mail up as ... an attachment instead of as html - but gmail is really wierd about ...
    (comp.lang.php)
  • Re: How do we get there from here?
    ... server-side-scripted html. ... This is a simple example with very little php scripting. ... means that the version of the php pre-processor on your web server must ... >>> The browser never sees anything not sent to it by the script. ...
    (comp.databases.pick)
  • Re: [PHP] eof bof in php
    ... Anthony Gentile wrote: ... it is bad practice to mix PHP and HTML as horridly as I just showed you. ...
    (php.general)