October 17th, 2008

PHP

What is PHP?

PHP is an open-source, HTML-embedded scripting language that can be used to develop web applications.

Where do I find information about PHP?

www.php.net Where do I download PHP? www.php.net Where is the Interactive PHP Manual? www.php.net Where can i find live help. IRC: pgardner.net, Port: 6667, Channel: #php OR http://www.mnetweb.co.uk/irc

MySQL

Where do I find information about MySQL?

www.mysql.com Where do I find the MySQL manual? www.mysql.com If you are unfamiliar with MySQL, I suggest installing a database web based graphical interface and helper application, such as phpMyAdmin ( www.phpmyadmin.net ).

Free PHP & MySQL Hosting

http://www.tripod.co.uk http://www.coolfreepages.com

Apache

Where do I find the Apache web server?

www.apache.org

The Apache web server is open source, very popular, free and can run PHP as a server module for increased efficiency.

*nix Linux/FreeBSD

Where do I find open source *nix? www.linux.org www.freebsd.org What is LAMP? It stands for Linux Apache MySQL PHP working together as a single web applcations platform. It is regarded by some as the premier or at least most popular open source platforms. Some prefer FreeBSD over Linux. LAMP is a model for WAMP a similar Windows package.

How To Learn PHP Programming

Sometimes the following ways of learning programming are more helpful than reading books. You can learn by helping others learn by building open source software for a community learn by reading the specs and docs and articles available on the web learn by lurking in discussion groups/use discussion archives to their fullest as a reference learn by reading the manual for your programming language (an interactive manual such as php.net can be a true learning environment as well as a place for developers to pick up hints on bugs and features or helpful hints and code)

PHP Editors

Editors and development enviroments to choose from.

  • HomeSite (my favorite, PHP highlighting plus extensive HTML/XML features and integration with other apps, an IDE type enviroment) http://www.allaire.com
  • TextPad (a favorite text editor) www.textpad.com
  • Davor’s PHP Editor http://www.pleskina.com/dphped/
  • EditPlus http://www.editplus.com/” (this list is far from complete)

PHP and XML

If you are going to start learning PHP and XML, I recommend HTML-KIT (http://www.chami.com).

In general, if you are going to use PHP’s internal XML DOM processor, go to Google and search on “PHP XML”. There are some great suggestions there. The PHP manual itself is where I got started (http://www.php.net/manual/en/ref.xml.php).

Programming Hints

How do I make a PHP page?

PHP pages are text files like any other text file and can be edited in any text editor. Create a text file, write some PHP code, save it or uploaded it to the web server, give it a .php extension (or other extension if you’re using something else) and access it in your browser.

Accepting Variables from Forms, URLS and Cookies

As PHP has matured as an application development platform some of the original design decisions have been reconsidered. To increase security of applications were mischeif is likely, such as online forums and to satisfy security requirements of enterprise (big business) and institutions, the way PHP handles variables coming from outside PHP has been changed. To make PHP an easy to learn and use language, previous versions allowed access to a form input variable simply by creating it automatically. So you could access the variable by

                  // excepts $title
                  // automatically created by input title on form
                  print $title;

To make this process less prone to spoofing and other security problems PHP now recommends you accept variables only from the ‘superglobal’ arrays designed to contain variables from outside of PHP. It is now preferable to write:

                  // title form input is
                  // available only in post mode array print
                  $_POST['title'];

When you want to access a value stored in an array by using it’s key ( i.e. $_POST[ ‘varname’ ] where ‘varname’ is the key ), then you should put either single or double quotes around the string. Otherwise, PHP may think you meant a constant and reprot and a use of undefined constant error. (BTW: Without the quotes PHP first checks to see if there is a constant defined for that string, if one is not found it assumes you were just too lazy (or forgot) to put the quotes in. Also, you will get an undefined variable error if Register_Globals are set to OFF in the php configuration and you try to run code that uses variables in the old way).

Assignment Vs. Equals

In PHP, = is the assignment operator. Use == to compare values for equality in conditionals.

Warnings, Notices and Errors

Before you start programming in PHP it is advisable to study PHP Error Reporting, otherwise depending on your configuration you may see any number of seemingly mysterious error messages. To suppress notices PHP generates for mostly harmless transgressions, place the following code before any other code:

                  // This will NOT report uninitialized
                  variables
                  error_reporting (E_ERROR | E_WARNING |
                  E_PARSE);

To explicity see notices when your hosting provider sets the configuration to suppress them:

 error_reporting (E_ERROR |
                  E_WARNING | E_PARSE | E_NOTICE); // show notices

Why do I get an error when I have <?xml version=”1.0″> in a PHP page?

If you are converting your pages to XHTML, you may recieve an error when including the XML processing directive. The reason this occurs is that ‘short tags’ is enabled on your installation of PHP. This allows programmers to substitute <? … ?> for <?php … ?> One solution is to print the line so that PHP does interpret it: print <?xml version=”1.0″>; Otherwise, disable short tags in php.ini, which are a bad idea anyway.

Why do I get a Not Found submitting a form to PHP?

If you get a Not Found submitting a form to a PHP page, it usually means that you have a HTTP redirection (using header() function) and the URL location is giving a 404 Error, i.e. the page you’re redirecting to is Not Found.

How to Validate an Email Address

The most comprehensive regular expression currently available for testing if an email address is valied was published in Mastering Regular Expressions from O’Reilly Publishing. It appears on page 316 and weighs in at more than six thousand characters. A reasonable approach I’ve used is a simple regular expression that checks for one ‘@’ symbol. This test catches people who mistakenly enter their user name instead of an email address (you would be surprised how many people do this). The only true solution for validating an email address is to send a test message to the user’s address. If it does not bounce the address is good. Why is it so difficult to validate an email address? Many non technical people and more programmers than I care to see believe that checking for a valid email address is simple. Because the internet email specification was intended to retain a human readable format to text messages. The specification is fraught with ambiguity and complicated syntax. It might be possible to write a parser to check if an email address has valid syntax, but this is beyond the capability of any regular expression. It’s just too much trouble. That leaves us with a regular expression solution that invariably is going to reject some valid email addresses. Before you decide to validate, please check this article (and there are others on the same topic at O’Reilly). http://www.oreillynet.com/pub/wlg/2379

Are you asking a question like ‘Can you tell me if this regular expression (regex) works?’

Have you tried writing a small PHP page to test this regular expression? I find that helps a lot. I keep a small PHP page with a form for entering text for testing regular expressions. Have you browsed the PHP Manual for help with regular expressions? It is often easier to deal with regexes using the Perl Compatible Regular Expression functions of PHP4 than the POSIX based ‘ereg’ functions. You will find more examples (hint: look to the Perl community for regex example code) of those.

ePHP (A Complete ‘LAMP’ Solution for Windows)

ePHP (e_novative) The WAMP Windows PHP Solution. Assists novices in installing and configuring a LAMP solution on Windows. Includes PHP, Apache, MySQL, PHPMyAdmin. Please remember to get the correct download for your operating system Ie: Windows XP/NT/2000, Windows 95,98,ME,Longhorn, Linux or mac. Download http://php.e-novative.de/ephp_download.php (Windows Only) Home http://php.e-novative.de/ephp.php

Improving Your Code

Profile your code. Information on profiling PHP stuff can be found at ONLamp:

http://www.onlamp.com/pub/a/php/2002/02/28/profilingphp.html?page=1

PHP_SELF Fails Mysteriously

In the past, there were reports the PHP_SELF variable is unavailable. PHP_SELF has been around since PHP 3.0.6, but there were some bugs in early Windows CGI installations that caused it to fail. The problem was that various web servers use different environmental variables to point to the current script. You should not encounter this problem in any current version of PHP. (Thanks to Jason Birch).

There are no comments yet, add one below.

Leave a Comment


You must be logged in to post a comment.