Tuesday, December 2, 2008

Part 5: Managing Sessions

What is a Session Variable?
Session variables are similar to cookies, that is they are used to store information for a particular period of time. The values in session variables exist only till the session exists. They are used to carry information from one page to another of a web site. We can create a session using a session identifier and store it in the server. When the client makes any request, the data stored in the session variable can be accessed by PHP any number of times until the session is ended.

Why do we need a Session Variable?
In web sites, passing information between pages using a query string is very difficult. For example once you log into a site, passing the username to all the pages of the site using query string is very difficult, but this can be done easily using Session variables. These variables can be used to pass information from one page to another without using a query string, since it is easy to maintain and retrieve.

How to set Session Variables?
Session variables can set using 'session_register' function.

Part4: Functions in PHP

In this part 4, you'll learn about Functions in PHP. This includes - What is a Function?, How to define and call functions in PHP.

What is a Function?

A Function is a small set of statements defined by the programmer to do a specific action. They take input values in the form of 'arguments' and return values after execution. They can be written anywhere in the program. They are used to reduce the programming complexity and to handle the programming structures easily. The function takes an input, performs some operation with it and returns a value after successful execution. Functions are basically of two types, namely:

Functions with no return value
Functions with return values


How to define & call functions?

Functions are defined using the keyword 'function' followed by the function name. The input parameters are listed in parentheses after the function name, followed by the function code within braces after the arguments

Part 3: Using Cookies in PHP

What is a Cookie?

Cookies are small bits of information that can be stored on a client computer. Once a cookie is created, it will expire after a specified time period. All the information stored in a cookie exist until it expires or deleted by the user.

Why do we need Cookies?
Now-a-days most of the websites use cookies to store small amounts of information. Websites can read the values from the cookies and use the information as desired. The browser is capable of keeping track of the websites and their corresponding cookies and is capable of reading the information from relevant cookies. Some common use of cookies include:

User's aesthetic preference for a specific site.
User keys to link them with their personal data - as used by many Shopping Cart Applications.
Allowing a user to remain 'logged on' until he explicitly logs out or the browser window is closed.

How to create a cookie?

Cookies can be set using the 'setcookie' function in PHP. This function takes three arguments - name of the variable, value of the variable and the expiry time period.

Saturday, September 20, 2008

Part Two: PHP Debugging Basics

We assume that you have some knowlege of programming in PHP. Basics to debug your code are summarised here.

Configuring Error Reporting

To make error messages display in the browser, set the display_errors configuration directive to On. To send errors to the web server error log, set log_errors to On. You can set them both to On if you want error messages in both places.

An error message that the PHP interpreter generates falls into one of five different categories:

Parse error: A problem with the syntax of your program, such as leaving a semicolon off of the end of a statement. The interpreter stops running your program when it encounters a parse error.
Fatal error: A severe problem with the content of your program, such as calling a function that hasn't been defined. The interpreter stops running your program when it encounters a fatal error.
Warning: An advisory from the interpreter that something is fishy in your program, but the interpreter can keep going. Using the wrong number of arguments when you call a function causes a warning.
Notice: A tip from the PHP interpreter, playing the role of Miss Manners. For example, printing a variable without first initializing it to some value generates a notice.
Strict notice: An admonishment from the PHP interpreter about your coding style. Most of these have to do with esoteric features that changed between PHP 4 and PHP 5, so you're not likely to run into them too much.

Fixing Parse Errors
The first time you write a PHP program, you discover that the PHP interpreter is extremely picky. If you leave out a necessary semicolon or start a string with a single quote but end it with a double quote, the interpreter doesn't run your program. It throws up its (virtual) hands, complains about a parse error, and leaves you stuck in the debugging wilderness.

Another feature of these editors is quote and bracket matching. This helps to make sure that your quotes and brackets are balanced. When you type a closing delimiter such as }, the editor highlights the opening { that it matches. Different editors do this in different ways, but typical methods are to flash the cursor at the location of the opening {, or bold the { } pair for a short time. This behavior is helpful for pairs of punctuation marks that go together: single and double quotes that delimit strings, parentheses, square brackets, and curly braces.

Once you've got error reporting set up as you like it, and you know how to find parse errors, and you can inspect program data, you're on your way to a fruitful debugging career. However, a fully fleshed-out PHP programmer's toolbox consists of much more than just the tips in this article.

Monday, May 12, 2008

Part One: An Introduction to PHP

What is PHP?

PHP, stands for "Hypertext Preprocessor".

Definition from php.net :
"PHP is an HTML-embedded scripting language. Much of its syntax is borrowed from C, Java and Perl with a couple of unique PHP-specific features thrown in. The goal of the language is to allow web developers to write dynamically generated pages quickly."

Why PHP5 ???

1. Robust Support for Object-Oriented Programming

It offers:

Constructors
Destructors
Public, protected, and private properties and methods
Interfaces
Abstract classes
Class type hints
Static properties and methods
Final properties and methods
A whole suite of magical methods

2. A Completely Rewritten MySQL Extension

It offers:

Prepared statements
Bound input and output parameters
SSL connections
Multi-query functions

3. A Suite of Interoperable XML Tools
The new XML extensions:

Work together as a unified whole.
Are standardized on a single XML library: libxml2.
Fully comply with W3 specifications.
Efficiently process data.
Provide you with the right XML tool for your job.

4. An Embedded Database with SQLite
While MySQL is greater than ever, it's actually "too much database" for some jobs. SQLite is an embedded database library that lets you store and query data using an SQL interface without the overhead of installing and running a separate database application.
Despite the name, SQLite is nowhere close to a "lite" database. It supports:

Transactions
Subqueries
Triggers
And many other advanced database features

5. Cleaner Error Handling with Exceptions

PHP 5 offers a completely different model of error checking than what's available in PHP 4. It's called exception handling. With exceptions, you're freed from the necessity of checking the return value of every function. Instead, you can separate programming logic from error handling and place them in adjoining blocks of code.

6. A First-Class SOAP Implementation

SOAP is a key component of the fast-growing web services field. This extension lets developers create SOAP clients with or without a Web Services Description Language (WSDL) file, and also implement SOAP servers in PHP.

7. Iterators

Iterators are a completely new PHP 5 feature. They allow you to use a for-each loop to cycle through different types of data: directory listings, database results, and even XML documents. SPL -- Standard PHP Library -- is a collection of iterators that provide this functionality and also filter, limit, cache, and otherwise modify iterator results.

Iterators are an incredibly handy way to abstract away messy details from your code.

Installation

Download Wamp(Windows, Apache, Mysql, PHP) server, a complete package for PHP and Mysql on windows platform from following link.

Download WAMP from here

double click on the .exe file and install it in ur preferred location. If not sure, let it be with defaults.

after completion, start wamp server.

you will see a small icon in your taskbar.

Congratulations!!!!
You are done with installation :)