Thursday, September 30, 2010

Operators and Expressions in PHP

You can now assign data to variables. You can even investigate and change the data
type of a variable. A programming language isn't very useful, though, unless you
can manipulate the data you can store. Operators are symbols that make it possible
to use one or more values to produce a new value. A value that is operated on by an
operator is referred to as an operand.
NEW
TERM
An operator is a symbol or series of symbols that, when used in
conjunction with values, performs an action and usually produces a
new value.
NEW
TERM
An operand is a value used in conjunction with an operator. There
are usually two operands to one operator.
Let's combine two operands with an operator to produce a new value:
4 + 5
4 and 5 are operands. They are operated on by the addition operator (+) to produce
9. Operators almost always sit between two operands, though you will see a few
exceptions later in this hour.
The combination of operands with an operator to manufacture a result is called an
expression. Although most operators form the basis of expressions, an expression
need not contain an operator. In fact in PHP, an expression is defined as anything
that resolves to a value. This includes integer constants such as 654, variables such
as $user, and function calls such as gettype(). The expression (4 + 5), therefore is
an expression that consists of two further expressions and an operator.
NEW
TERM
An expression is any combination of functions, values, and
operators that resolve to a value. As a rule of thumb, if you can use
it as if it were a value, it is an expression.

Now that we have the principles out of the way, it's time to take a tour of PHP4's
more common operators.

Wednesday, September 29, 2010

Changing Data Type Type by Casting in Php

By placing the name of a data type in brackets in front of a variable, you create a
copy of that variable's value converted to the data type specified. The principal
difference between settype() and a cast is the fact that casting produces a copy,
leaving the original variable untouched. Listing 4.6 illustrates this.

1: <html>
2: <head>
3: <title>Listing 4.6 Casting a variable</title>
4: </head>
5: <body>
6: <?php
7: $undecided = 3.14;
8: $holder = ( double ) $undecided;
9: print gettype( $holder ) ; // double
10: print " -- $holder<br>"; // 3.14
11: $holder = ( string ) $undecided;
12: print gettype( $holder ); // string
13: print " -- $holder<br>"; // 3.14
14: $holder = ( integer ) $undecided;
15: print gettype( $holder ); // integer
16: print " -- $holder<br>"; // 3
17: $holder = ( double ) $undecided;
18: print gettype( $holder ); // double
19: print " -- $holder<br>"; // 3.14
20: $holder = ( boolean ) $undecided;
21: print gettype( $holder ); // boolean
22: print " -- $holder<br>"; // 1
23: ?>
24: </body>
25: </html>
We never actually change the type of $undecided, which remains a double
throughout. In fact, by casting $undecided, we create a copy that is then converted
to the type we specify. This new value is then stored in the variable $holder.
Because we are working with a copy of $undecided, we never discard any
information from it as we did in Listing 4.5.

Changing Data Type with settype() in php

PHP provides the function settype() to change the type of a variable. To use
settype(), you must place the variable to change (and the type to change it to)
between the parentheses and separated by commas. Listing 4.5 converts 3.14 (a
double) to the four types that we are covering in this hour.

1: <html>
2: <head>
3: <title>Listing 4.5 Changing the type of a variable with settype()</title>
4: </head>
5: <body>
6: <?php
7: $undecided = 3.14;
8: print gettype( $undecided ); // double
9: print " -- $undecided<br>"; // 3.14
10: settype( $undecided, string );
11: print gettype( $undecided ); // string
12: print " -- $undecided<br>"; // 3.14
13: settype( $undecided, integer );
14: print gettype( $undecided ); // integer
15: print " -- $undecided<br>"; // 3
16: settype( $undecided, double );
17: print gettype( $undecided ); // double
18: print " -- $undecided<br>"; // 3.0
19: settype( $undecided, boolean );
20: print gettype( $undecided ); // boolean
21: print " -- $undecided<br>"; // 1
22: ?>
23: </body>
24: </html>
In each case, we use gettype() to confirm that the type change worked and then
print the value of the variable $undecided to the browser. When we convert the
string "3.14" to an integer, any information beyond the decimal point is lost forever.
That's why $undecided still contains 3 after we have changed it back to a double.
Finally, we convert $undecided to a boolean. Any number other than 0 becomes true
when converted to a boolean. When printing a boolean in PHP, true is represented
as 1 and false as an empty string, so $undecided is printed as 1.

Data Types in php

Different types of data take up different amounts of memory and may be treated
differently when they are manipulated in a script. Some programming languages
therefore demand that the programmer declare in advance which type of data a
variable will contain. PHP4 is loosely typed, which means that it will calculate data
types as data is assigned to each variable. This is a mixed blessing. On the one hand,
it means that variables can be used flexibly, holding a string at one point and an
integer at another. On the other hand, this can lead to confusion in larger scripts if
you expect a variable to hold one data type when in fact it holds something
completely different.

Type                        Example                    Description
Integer                        5                          A whole number
Double                    3.234                     A floating-point
number
String                     "hello"                  A collection ofcharacters
Boolean                   true                          One of the special values true or false
Object                 "Objects"
Array See                "Arrays"

You can use PHP4's built-in function gettype() to test the type of any variable. If you
place a variable between the parentheses of the function call, gettype() returns a
string representing the relevant type. Listing 4.4 assigns four different data types to
a single variable, testing it with gettype() each time.
Note You can read more about calling functions in Hour 6, "Functions."

1: <html>
2: <head>
3: <title>Listing 4.3 Testing the type of a variable</title>
4: </head>
5: <body>
6: <?php
7: $testing = 5;
8: print gettype( $testing ); // integer
9: print "<br>";
10: $testing = "five";
11: print gettype( $testing ); // string
12: print("<br>");
13: $testing = 5.0;
14: print gettype( $testing ); // double
15: print("<br>");
16: $testing = true;
17: print gettype( $testing ); // boolean
18: print "<br>";
19: ?>
20: </body>
21: </html>
This script produces the following:
integer
string
double
boolean
An integer is a whole or real number. In simple terms, it can be said to be a number
without a decimal point. A string is a collection of characters. When you work with
strings in your scripts, they should always be surrounded by double (") or single (')
quotation marks. A double is a floating-point number. That is, a number that
includes a decimal point. A boolean can be one of two special values, true or false.
Note Prior to PHP4, there was no boolean type. Although true was used, it
actually resolved to the integer 1.

References to Variables in php

By default, variables are assigned by value. In other words, if you were to assign
$aVariable to $anotherVariable, a copy of the value held in $aVariable would be
stored in $anotherVariable. Subsequently changing the value of $aVariable would
have no effect on the contents of $anotherVariable. Listing 4.2 illustrates this.
Listing 4.2: Variables Are Assigned by Value
1: <html>
2: <head>
3: <title>Listing 4.2 Variables are assigned by value</title>
4: </head>
5: <body>
6: <?php
7: $aVariable = 42;
8: $anotherVariable = $aVariable;
9: // a copy of the contents of $aVariable is placed in $anotherVariable
10: $aVariable = 325;
11: print $anotherVariable; // prints 42
12: ?>
13: </body>
14: </html>
This example initializes $aVariable, assigning the value 42 to it. $aVariable is then
assigned to $anotherVariable. A copy of the value of $aVariable is placed in
$anotherVariable. Changing the value of $aVariable to 325 has no effect on the
contents of $anotherVariable. The print statement demonstrates this by outputting
42 to the browser.
In PHP4, you can change this behavior, forcing a reference to $aVariable to be
assigned to $anotherVariable, rather than a copy of its contents. This is illustrated in

1: <html>
2: <head>
3: <title>Listing 4.3 Assigning a variable by reference</title>
4: </head>
5: <body>
6: <?php
7: $aVariable = 42;
8: $anotherVariable = &$aVariable;
9: // a copy of the contents of $aVariable is placed in $anotherVariable
10: $aVariable= 325;
11: print $anotherVariable; // prints 325
12: ?>
13: </body>
14: </html>
We have added only a single character to the code in Listing 4.2. Placing an
ampersand (&) in front of the $aVariable variable ensures that a reference to this
variable, rather than a copy of its contents, is assigned to $anotherVariable. Now
any changes made to $aVariable are seen when accessing $anotherVariable. In
other words, both $aVariable and $anotherVariable now point to the same value.
Because this technique avoids the overhead of copying values from one variable to
another, it can result in a small increase in performance. Unless your script assigns
variables intensively, however, this performance gain will be barely measurable.
Note References to variables were introduced with PHP4.

Monday, September 27, 2010

Dynamic Variables in Php

As you know, you create a variable with a dollar sign followed by a variable name.
Unusually, the variable name can itself be stored in a variable. So, when assigning
a value to a variable
$user = "bob";
is equivalent to
$holder="user";
$$holder = "bob";
The $holder variable contains the string "user", so you can think of $$holder as a
dollar sign followed by the value of $holder. PHP interprets this as $user.
Note You can use a string constant to define a dynamic variable instead of a
variable. To do so, you must wrap the string you want to use for the variable
name in braces:
${"user"} = "bob";
This might not seem useful at first glance. However, by using the
concatenation operator and a loop (see Hour 5, "Going with the Flow"), you
can use this technique to create tens of variables dynamically.
When accessing a dynamic variable, the syntax is exactly the same:
$user ="bob";
print $user;
is equivalent to
$user ="bob";
$holder="user";
print $$holder;
If you want to print a dynamic variable within a string, however, you need to give
the interpreter some help. The following print statement:
$user="bob";
$holder="user";
print "$$holder";
does not print "bob" to the browser as you might expect. Instead it prints the strings
"$" and "user" together to make "$user". When you place a variable within
quotation marks, PHP helpfully inserts its value. In this case, PHP replaces $holder
with the string "user". The first dollar sign is left in place. To make it clear to PHP
that a variable within a string is part of a dynamic variable, you must wrap it in
braces. The print statement in the following fragment:
$user="bob";
$holder="user";
print "${$holder}";
now prints "bob", which is the value contained in $user.
Listing 4.1 brings some of the previous code fragments together into a single script
using a string stored in a variable to initialize and access a variable called $user.


Dynamically Setting and Accessing Variables
1: <html>
2: <head>
3: <title>
Dynamically setting and accessing variables</title>
4: </head>
5: <body>
6: <?php
7: $holder = "user";
8: $$holder = "bob";
9:
10: // could have been:
11: // $user = "bob";
12: // ${"user"} = "bob";
13:
14: print "$user<br>"; // prints "bob"
15: print $$holder; // prints "bob"
16: print "<br>";
17: print "${$holder}<br>"; // prints "bob"
18: print "${'user'}<br>"; // prints "bob"
19: ?>
20: </body>
21: </html>

Variables in Php

A variable is a special container that you can define to "hold" a value. A variable
consists of a name that you can choose, preceded by a dollar ($) sign. The variable
name can include letters, numbers, and the underscore character (_). Variable
names cannot include spaces or characters that are not alphanumeric. The following
code defines some legal variables:
$a;
$a_longish_variable_name;
$2453;
$sleepyZZZZ
Remember that a semicolon (;) is used to end a PHP statement. The semicolons in
the previous fragment of code are not part of the variable names.
NEW TERM
A variable is a holder for a type of data. It can hold numbers, strings of
characters, objects, arrays, or booleans. The contents of a variable can
be changed at any time . 
As you can see, you have plenty of choices about naming, although it is unusual to
see a variable name that consists exclusively of numbers. To declare a variable, you
need only to include it in your script. You usually declare a variable and assign a
value to it in the same statement.
$num1 = 8;
$num2 = 23;
The preceding lines declare two variables, using the assignment operator (=) to
give them values. You will learn about assignment in more detail in the Operators
and Expressions section later in the hour. After you give your variables values, you
can treat them exactly as if they were the values themselves. In other words
print $num1;
is equivalent to
print 8;
as long as $num1 contains 8.

Adding Comments to PHP Code

Code that seems clear at the time of writing, can seem like a hopeless tangle when
you come to amend it six months later. Adding comments to your code as you write
can save you time later on and make it easier for other programmers to work with
your code.
NEW TERM
A comment is text in a script that is ignored by the interpreter.
Comments can be used to make code more readable, or to annotate a
script.
Single line comments begin with two forward slashes (/ /) or a single hash sign (#).
All text from either of these marks until either the end of the line or the PHP close tag
is ignored.
// this is a comment
# this is another comment
Multiline comments begin with a forward slash followed by an asterisk (/*) and end
with an asterisk followed by a forward slash (*/).
/*
this is a comment
none of this will
be parsed by the
interpreter
*/

Combining HTML and PHP

The script in Listing 3.1 is pure PHP. You can incorporate this into an HTML
document simply by adding HTML outside the PHP start and end tags, as shown in

1: <html>
2: <head>
3: <title>Listing 3.2 A PHP script including HTML</title>
4: </head>
5: <body>
6: <b>
7: <?php
8: print "hello world";
9: ?>
10: </b>
11: </body>
12: </html>
As you can see, incorporating HTML into a PHP document is simply a matter of
typing in the code. The PHP interpreter ignores everything outside PHP open and
close tags. If you were to view Listing 3.2 with a browser, as shown in Figure 3.4,
you would see the string "hello world" in bold. If you were to view the document
source, as shown in Figure 3.5, the listing would look exactly like a normal HTML
document.
You can include as many blocks of PHP code as you need in a single document,
interspersing them with HTML as required. Although you can have multiple blocks of
code in a single document, they combine to form a single script. Anything defined in
the first block (variables, functions, or classes, for example) usually will be available
to subsequent blocks.

The print() Function

print() is a function that outputs data. In most cases, anything output by print()
ends up in the browser window. A function is a command that performs an action,
usually modified in some way by data provided for it. Data sent to a function is
almost always placed in parentheses after the function name. In this case, you sent
the print() function a collection of characters, or string. Strings must always be
enclosed by quotation marks, either single or double.

Note: Function calls generally require parentheses after their name whether or
not they demand that data be passed to them. print() is an exception,
and enclosing the data you want to print to the browser in parentheses is
optional. This is the more common syntax, so we will usually omit the
brackets in our examples.
You ended your only line of code in Listing 3.1 with a semicolon. The semicolon
informs the interpreter that you have completed a statement.

NEW TERM
A statement represents an instruction to the interpreter. Broadly, it
is to PHP what a sentence is to written or spoken English. A
statement should usually end with a semicolon; a sentence should
end with a period. Exceptions to this include statements that
enclose other statements, and statements that end a block of code.
In most cases, however, failure to end a statement with a semicolon
will confuse the interpreter and result in an error.
Because the statement in Listing 3.1 is the final one in that block of code, the
semicolon is optional.

Beginning and Ending a Block of PHP Statements

When writing PHP, you need to inform the interpreter that you want it to execute
your commands. If you don't do this, the code you write will be mistaken for HTML
and will be output to the browser. Table 3.1 shows the four ways of enclosing PHP
code.

PHP Start and End Tags

Tag Style             Start Tag                                  End Tag
Standard tags        <?php                                        ?>
Short tags              <?                                             ?>
ASP tags                <%                                            %>
Script tags    <SCRIPTLANGUAGE="php">          </script>


only the standard and the script tags can be guaranteed to
work on any configuration. The short and ASP style tags must be explicitly enabled
in your php.ini. You examined the php.ini file in Hour 2.
To activate recognition for short tags, you must make sure that the short_open_tag
switch is set to "On" in php.ini:
short_open_tag = On;
Short tags are enabled by default, so you would only need to edit php.ini if you want
to disable these.
To activate recognition for the ASP style tags, you must enable the asp_tags
setting:
asp_tags = On;
After you have edited php.ini, you should be able to choose from any of the four
styles for use in your scripts. This is largely a matter of preference, although if you
intend to work with XML, you should disable the short tags (<? ?>) and work with
the standard tags (<?php ?>).
Let's run through some of the ways in which you can legally write the code in Listing
3.1. You could use any of the four PHP start and end tags that you have seen:
<?
print("Hello Web!");
?>
<?php
print("Hello Web!");
?>
<%
print("Hello Web!");
%>
<SCRIPT LANGUAGE="php">
print("Hello Web!");
</SCRIPT>
Single lines of code in PHP also can be presented on the same line as the PHP start
and end tags: <? print("Hello Web!"); ?>
Now that you know how to define a block of PHP code, take a closer look at the code
in Listing 3.1 itself.

Your First Php Script

Let's jump straight in with a PHP script. To begin, open your favorite text editor. Like
HTML documents, PHP files are made up of plain text. You can create them with any
text editor, such as Notepad on Windows, Simple Text and BBEdit on MacOS, or VI
and Emacs on UNIX operating systems. Most popular HTML editors provide at least
some support for PHP.
Type in the example in Listing 3.1 and save the file, calling it something like
first.php.

1: <?php
2: print "Hello Web!";
3: ?>
The extension to the PHP document is important because it tells the server to treat
the file as PHP code and invoke the interpreter. The default PHP extension for a PHP
4 document is .php. This can be changed, however, by altering the server's
configuration. You saw how to do this in Hour 2, "Installing PHP."
If you are not working directly on the machine that will be serving your PHP script,
you will probably need to use an FTP client, such as WS-FTP for Windows or Fetch for
MacOS to upload your saved document to the server.
After the document is in place, you should be able to access it via your browser. If
all has gone well, you should see the script's output. If PHP is not installed on your server or your file's extension is not recognized, you
may not see the output shown in Figure 3.2. In these cases, you probably will see
the source code created in Listing 3.1. Figure 3.3 shows what happens when an
unknown extension is encountered.If this happens, first check the extension with which you saved your PHP script. In
Figure 3.3, the document was accidentally called first.nphp. If the file extension is
as it should be, you may need to check that PHP has been installed properly and that
your server is configured to work with the extension that you have used for your
script. You can read more about installing and configuring PHP in Hour 2.
Now that you have uploaded and tested your script, you can take a look at the code
in a little more detail.

Installing PHP4 for Linux and Apache

In this section, we will look at one way of installing PHP4 with Apache on Linux. The
process is more or less the same for any UNIX operating system. You might be able
to find prebuilt versions of PHP for your system, which are simple to install.
Compiling PHP, though, gives you greater control over the features built in to your
binary.
Before you install you should make sure that you are logged into your system as the
root user. If you are not allowed access to your system's root account, you may
need to ask your system administrator to install PHP for you.
There are two ways of compiling an Apache PHP module. You can either recompile
Apache, statically linking PHP into it, or you can compile PHP as a Dynamic Shared
Object (DSO). If your version of Apache was compiled with DSO support, it will be
capable of supporting new modules without the need for recompiling the server.This method is the easiest way to get PHP up and running, and it is the one we will
look at in this section.
In order to test that Apache supports DSOs you should launch the Apache binary
(httpd) with the -l argument.
/www/bin/httpd -l
You should see a list of modules. If you see
mod_so.c
among them, you should be able to proceed; otherwise, you may need to recompile
Apache. The Apache distribution contains full instructions for this.
If you have not already done so, you will need to download the latest distribution of
PHP4. Your distribution will be archived as a tar file and compressed with gzip, so
you will need to unpack it:
tar -xvzf php-4.0.tar.gz
After your distribution is unpacked, you should move to the PHP4 distribution
directory:
cd ../php-4.0
Within your distribution directory you will find a script called configure. This accepts
arguments that will control the features that PHP will support. For this example, we
will include some useful command line arguments, although you might want to
specify arguments of your own. We will discuss some of the configure options
available to you later in the hour.
./configure --enable-track-vars \
--with-gd \
--with-mysql \
--with-apxs=/www/bin/apxs
The path you assign to the --with-apxs argument is likely to be different on your
system. It is possible that you will find apxs in the same directory as your Apache
executable.
After the configure script has run, you can run the make program. You will need a C
compiler on your system to run this command successfully.
make make install
These commands should end the process of PHP4 compilation and installation. You
should now be able to configure and run Apache.
Some configure Options
When we ran the configure script, we included some command-line arguments
that determined the features that the PHP interpreter will include. The configure
script itself gives you a list of available options. From the PHP distribution directory
type the following:
./configure --help
The list produced is long, so you may want to add it to a file for reading at leisure:
./configure --help > configoptions.txt
Although the output from this command is very descriptive, we will look at a few
useful options— especially those that might be needed to follow this book.
--enable-track-vars
This option automatically populates associative arrays with values submitted as part
of GET, POST requests or provided in a cookie. You can read more about arrays in
Hour 7, "Arrays," and about HTTP requests in Hour 13, "Beyond the Box." It is a
good idea to include this option when running configure.
--with-gd
--with-gd enables support for the GD library, which, if installed on your system,
allows you to create dynamic GIF or PNG images from your scripts. You can read
more about creating dynamic images in Hour 14, "Working with Dynamic Images."
You can optionally specify a path to your GD library's install directory:
--with-gd=/path/to/dir
--with-mysql
--with-mysql enables support for the MySQL database. If your system has MySQL
installed in a directory other than the default location, you should specify a path:
--with-mysql=/path/to/dir
As you know, PHP provides support for other databases. Table 2.1 lists some of
them and the configure options you will need to use them.
Table 2.1: Some Database configure Options
Database configure Option
Adabas D --with-adabas
FilePro --with-filepro
msql --with-msql
informix --with-informix
iODBC --with-iodbc
OpenLink
ODBC
--with-openlink
Oracle --with-oracle
PostgreSQL --with-pgsql
Solid --with-solid
Sybase --with-sybase
Sybase-CT --with-sybase-ct
Velocis --with-velocis
LDAP --with-ldap


Configuring Apache
After you have compiled PHP and Apache, you should check Apache's configuration
file, httpd.conf, which you will find in a directory called conf in the Apache install
directory. Add the following lines to this file:
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
This ensures that the PHP interpreter will parse files that end with the .php
extension. Any files with the .phps extension will be output as PHP source. That is,
the source code will be converted to HTML and color-coded. This can be useful for
debugging your scripts.
If you want to offer to your users PHP pages with extensions more familiar to them,
you can choose any extension you want. You can even ensure that files with
the .html extension are treated as PHP files with the following:
AddType application/x-httpd-php .html
Note that treating files with the .html extension as PHP scripts could slow down your
site, because every page with this extension will be parsed by the PHP interpreter
before it is served to the user.
If PHP has been preinstalled and you have no access to the Apache configuration
files, you may be able to change the extensions that will determine which files will
be treated as PHP executables by including an AddType directive in a file
called .htaccess. After you have created this file, the directive will affect the
enclosing directory, as well as any subdirectories. This technique will only work if
the AllowOverride directive for the enclosing directory is set to either FileInfo or All.
Although the filename .htaccess is the default for an access control file, it may have
been changed. Check the AccessFileName directive in httpd.conf to find out. Even if
you don't have root access, you should be able to read the Apache configuration
files.
An .htaccess file can be an excellent way of customizing your server space if you do
not have access to the root account. An additional way of controlling the behavior of
PHP, even as a non-root user, is the php.ini file.


php.ini
After you have compiled or installed PHP, you can still change its behavior with a file
called php.ini. On UNIX systems, the default location for this file is /usr/local/lib;
on a Windows system, the default location is the Windows directory. A php.ini file
in the current working directory will override one in the default location, so you can
change the behavior of PHP on a per-directory basis.
You should find a sample php.ini file in your distribution directory, which contains
factory settings. Factory settings will be used if no php.ini file is used.
The default settings should be adequate for most of the examples in this book,
although you can read about some amendments you might like to make in Hour 22,
"Debugging."
Directives in the php.ini file take the form of a directive and a value separated by an
equals sign. Whitespace is ignored.
If PHP has been preinstalled on your system, you might want to check some of the
settings in php.ini. Remember, if you are not allowed to alter this document, you
can create one in your script's directory that can override the default. You can also
set an environmental variable PHPRC that designates a php.ini file.
You can change your php.ini settings at any time, though if you are running PHP as
an Apache module, you should restart the server for the changes to take effect.
short_open_tag
The short_open_tag directive determines whether you can begin a block of PHP
code with the symbols <? and close it with ?>. If this has been disabled, you will see
one of the following:
short_open_tag = Off
short_open_tag = False
short_open_tag = No
To enable the directive you can use one of the following:
short_open_tag = On
short_open_tag = True
short_open_tag = Yes
You can read more about PHP open and close tags in Hour 3, "A First Script."


Error Reporting Directives
To diagnose bugs in your code, you should enable the directive that allows error
messages to be written to the browser. This is on by default:

Platforms, Servers, Databases, and PHP

PHP is truly cross-platform. It runs on the Windows operating system, most versions
of UNIX including Linux, and even the Macintosh. Support is provided for a range of
Web servers including Apache (itself open source and cross-platform), Microsoft
Internet Information Server, WebSite Pro, the iPlanet Web Server, and Microsoft's
Personal Web Server. The latter is useful if you want to test your scripts offline on a
Windows machine, although Apache can also be run on Windows.
You can also compile PHP as a standalone application. You can then call it from the
command line. In this book, we will concentrate on building Web applications, but
do not underestimate the power of PHP4 as a general scripting tool comparable to
Perl.
PHP is designed to integrate easily with databases. This feature is one of the factors
that make the language such a good choice for building sophisticated Web
applications. Many databases are directly supported, including Adabas D, InterBase,
Solid, dBASE, mSQL, Sybase, Empress, MySQL, Velocis, FilePro, Oracle, UNIX dbm,
Informix, and PostgreSQL. PHP also supports ODBC.
we will be using a combination of Linux, Apache, and MySQL.
All these are free to download and use, and can be installed relatively easily on a PC.
You can find out more about getting Linux for your computer at
<http://www.linux.org/help/beginner/distributions.html>. If you want to run Linux
on a Power PC, you can find information about LinuxPPC at
<http://www.linuxppc.org>.
MySQL, the database we will use, can be downloaded from
<http://www.mysql.com>. There are versions for many operating systems
including UNIX, Windows, and OS/2.
On the other hand, you can easily stick with Windows, NT, or MacOS. PHP is, after
all, a cross-platform scripting language.

Why Choose PHP?

There are some compelling reasons to work with PHP4. For many projects you will
find that the production process is significantly faster than you might expect if you
are used to working with other scripting languages. As an open source product,
PHP4 is well supported by a talented production team and a committed user
community. Furthermore, PHP can be run on all the major operating systems with
most servers.

Speed of Development

Because PHP allows you to separate HTML code from scripted elements, you will
notice a significant decrease in development time on many projects. In many
instances, you will be able to separate the coding stage of a project from the design
and build stages. Not only can this make life easier for you as a programmer, it also
can remove obstacles that stand in the way of effective and flexible design.
PHP Is Open Source
To many people, "open source" simply means free, which is, of course, a benefit in
itself. To quote from the official PHP site at http://www.php.net/:
This may sound a little foreign to all you folks coming from a non-UNIX
background, but PHP doesn't cost anything. You can use it for commercial and/or
non-commercial use all you want. You can give it to your friends, print it out and
hang it on your wall or eat it for lunch. Welcome to the world of Open Source
software! Smile, be happy, the world is good. For the full legalese, see the official
license.
Well-maintained open source projects offer users additional benefits, though. You
benefit from an accessible and committed community who offer a wealth of
experience in the subject. Chances are that any problem you encounter in your
coding can be answered swiftly and easily with a little research. If that fails, a
question sent to a mailing list can yield an intelligent, authoritative response.
You also can be sure that bugs will be addressed as they are found, and that new
features will be made available as the need is defined. You will not have to wait for
the next commercial release before taking advantage of improvements.
There is no vested interest in a particular server product or operating system. You
are free to make choices that suit your needs or those of your clients, secure that
your code will run whatever you decide.

Performance
Because of the powerful Zend engine, PHP4 compares well with ASP in benchmark
tests, beating it in some tests. Compiled PHP leaves ASP far behind.

Portability
PHP is designed to run on many operating systems and to cooperate with many
servers and databases. You can build for a UNIX environment and shift your work to
NT without a problem. You can test a project with Personal Web Server and install it
on a UNIX system running on PHP as an Apache module.

The Zend Engine in Php

When PHP3 was written, an entirely new parser was created from the ground up.
PHP4 represents a similar change to the scripting engine. This rewrite, though, is
more significant by orders of magnitude.Zend is a scripting engine that sits below the PHP-specific modules. It is optimized
to significantly improve performance.
These changes in efficiency will ensure PHP4's continued success. Most code written
for PHP3 will continue to run with no changes; however, these scripts may run up to
200 times faster!
A commercial addition to the Zend engine will be the facility for compiling PHP
scripts. This will provide a further gain in performance that should leave most, if not
all, competitors far behind.
Zend is built to improve performance but is also designed for increased flexibility.
Communication with servers has been improved, so it will be possible to create PHP
modules that work with a wider range of servers. Unlike a CGI interpreter, which sits
outside a server and is initialized every time a script is run, a server module runs in
conjunction with the server. This improves performance because the scripting
engine does not need to be started for a PHP page to be executed.

Sunday, September 26, 2010

What's New in PHP4

PHP4 introduces numerous new features that will make the programmer's life more
interesting. Let's take a quick look at some of them.
A new foreach statement, similar to that found in Perl, makes it much easier to
loop through arrays. We will be using this for most of the array examples in this
book. Additionally, a raft of new array functions have been added, making arrays
easier to manipulate.
The language now includes the boolean data type.
A particularly useful feature of PHP3 was the capability to name form elements
as if they were elements in an array. The elements' names and values are then
made available to the code in array form. This feature has been extended to
support multidimensional arrays.
Support for object-oriented programming was somewhat rudimentary in PHP.
This is significantly extended in PHP4; for example, it is now possible to call an
overridden method from a child class.
PHP4 now provides native support for user sessions, using both cookies and the
query string. You can now "register" a variable with a session, and then access
the same variable name and value in subsequent user requests.
A new comparison operator (===) has been introduced that tests for
equivalence of type as well as equivalence of value.
New associative arrays containing server and environmental variables have
been made available, as well as a variable that holds information about uploaded
files.
PHP4 now provides built-in support for both Java and XML.
Although these and other features significantly improve the language, perhaps the
most significant change has taken place under the hood.

Free Link And Article Directory || Post You Article Here

Post Up to 20 Links Of Your Website Or Blog Instantly, Without Signup. Post Unlimited Articles And Create A Partner Page For Free Link Exchange.

Post Link


Maximum 20 pages of any blog or website will be posted

No reciprocal link or backlink needed.

Instant posting. See your link after submit.

We accept any kind of deeplink.

Its free easy and no signup needed
Post Article


Maximum 400 characters containing 2 links accepted.

No reciprocal link or backlink needed.

Instant posting. See your Article after submit.

We accept any kind of deeplink.

Its free easy and no signup needed



Create Partner Page


You will get your own page in our server.

Backlink needed. Instant Page creation.

you can put upto 5 links in your page.

Its free easy and signup needed.