[thelist] php form

Dougal Campbell dougal at gunters.org
Fri Nov 22 10:55:01 CST 2002


On Fri, 22 Nov 2002, Tom Dell'Aringa wrote:

> Since I am fairly new to PHP I have some questions about 'best
> practices' for some things. I am kinda used to doing things the way I
> learned them in ASP. I've been looking at others code and seeing some
> differences.
>
> 1 - When writing out code to your page, is it best to:
>
> a) echo("foo");
> b) print "foo";

For the most part, there's no differences. But there are a few *subtle*
differences where print will work, and echo will not. See the PHP manual
for details and examples:
  http://www.php.net/manual/en/function.echo.php
  http://www.php.net/manual/en/function.print.php

> along with that, when you have a lot of code (say more than one line)
> to output, it seems to me better to do this (usually looping through
> a result set)
>
> <?
> sql etc
> being some loop
> ?>
>
> <normal html code with <?=$vars?> inside>
>
> <?
> end some loop
> ?>
>
> I seem to find that much easier when there is a lot of stuff instead
> of worrying about packing it all into one string with quote issues.

Yes, this is good practice. Avoids the need to escape quote characters
and (IMHO) makes the code much easier to read. Even better than that
approach is to use a template class, but if you're just getting started,
that might be a layer of complexity that you don't need yet:
  http://smarty.php.net/

> 2 - What is the difference between <?php and simply <? - i'd prefer
> not to type php all the time if I don't have to.

In general, no difference. It's only a factor if the server admin has
disabled the short form of the tag, or maybe if you're embedding PHP
code in an XML document.
  http://www.php.net/manual/en/language.basic-syntax.php

> 3 - question about isset() - will a form field that has no value
> (i.e. <input type='text' value=''>) be considered set or not? I
> seemed to have some trouble with that.

In that case, isset() will return true, and the variable in question
would just be an empty string.

  <?php
    // A pedantic example
    if (! isset($foo)) {
      print "There was no foo variable!";
    } else {
      if (empty($foo)) {  // or, 'if (!$foo)'
        print "foo is set, but empty.";
      } else {
        print "foo is '$foo'";
      }
    }
  ?>

--
Ernest MacDougal Campbell III, MCP+I, MCSE <dougal at gunters.org>
http://dougal.gunters.org/             http://spam.gunters.org/
  Web Design & Development:  http://www.mentalcollective.com/
       This message is guaranteed to be 100% eror frea!




More information about the thelist mailing list