Friday, October 24, 2008

Overview of PHP Data Types and Concepts

PHP began life as a way to manage a small personal website and was imagined and realized
by just one man, Ramsus Lerdorf. Originally dubbed Personal Home Page Tools, PHP quickly
evolved over the years from the basic scripting engine for a personal website into a highly
competitive, extremely robust code engine that is deployed on millions of websites across
the globe. PHP’s fast, effective engine; its widespread, open-source developer base; and its
platform flexibility have all come together to create one of the world’s most effective online
scripting languages.
Throughout the years PHP has continued to improve on its foundations, providing
increased functionality and scalability. Because of PHP’s standard of listening to the community,
fresh functionality is consistently added to every new release, allowing for more versatile code and upgrades to its already substantial library of built-in methods. For years, people have been using the PHP 4 series of code to create robust and powerful applications.

There is always room for improvement, however. Although PHP 4 is considered to be an
object-oriented programming (OOP) language, the class functionality found within it was not
entirely as flexible as some developers wanted it to be. Older OOP languages that have had
more time to grow have some strong functionality that PHP simply was not able to roll out in
its PHP 4 releases.
But that was then, and this is now. A very exciting occasion occurred for PHP developers
everywhere on July 13, 2004: PHP released its long-anticipated version 5. Sporting a new
object model powered by the already superb Zend II engine, PHP was ready to bring OOP
to a new level with this release.
On top of new, more powerful class structures and functionality, PHP 5 has introduced
many exciting features, some of which the community has been clamoring about for ages.
Say “hello (world)” to proper exception handling; new, simple-to-implement XML support;
more verbose Simple Object Access Protocol (SOAP) functionality for web services; and much,
much more.
This book will provide you with highly versatile recipes for improving and expanding
things with the new PHP 5 release. However, before we dive into that, in this chapter we will
give you a simple overview of what PHP can do, what is new with PHP 5, and how you can
apply these new concepts.

1-1.Variables
Variables in PHP are handled somewhat differently than in other similar programming languages.
Rather than forcing the developer to assign a given variable a data type and then
assign a value to it (as in languages such as C++ and Java), PHP automatically assigns a data
type to a variable when a value is allocated to it. This makes PHP rather simple to use when
declaring variables and inputting values into them.
PHP variables, of course, follow a certain set of rules. All variables must begin with $ and
must be immediately followed by a letter or an underscore. Variables in PHP are indeed casesensitive
and can contain any number of letters, numbers, or underscores after the initial $
and first letter or underscore.
Although initially variables in PHP were always assigned by value, since the PHP 4 release
(and including PHP 5), you can now assign variables by reference. This means you can create
something of an alias to a variable that will change the original value if you modify the alias.
This is quite different from value-assigned variables that are essentially copies of the original.
The following example shows a couple blocks of code to give you a good handle on PHP 5
variable functionality.
The Code
//sample1_1.php
//A properly set-up PHP variable.
$myvar = 0;
//An improper PHP variable.
//$1myvar = 0;
$yourvar = "This is my value
";
//An example of assigning variables by value.
$myvar = $yourvar;
//If we were to change it.
$myvar = "This is now my value.
";
echo $yourvar; //Echoes This is my value
//An example of assigning a variable by reference.
$myvar = &$yourvar;
$myvar = "This is now my value.
";
echo $yourvar; //Echoes This is now my value.

?>
This is my value
This is now my value.

How It Works
Using superglobals has taken precedent while people slowly migrate their code from the old,
variable-based method (which requires register_globals to be set to on in the php.ini file) to
the new superglobal array method (which does not require register_globals to be set to on).
Basically, rather than using the old method of gathering data from places such as cookies, sessions,
and form variables, PHP 5 is moving its focus toward the concept of superglobals. A few
custom PHP globals can gather information from different sources. By using these superglobals,
a developer can keep order within a script by knowing and managing exactly where a
variable has come from or will be going to. Considered largely more secure because you can
build code to tell exactly where variables are coming from, rather than just accepting a variable
at face value, superglobals are becoming the standard.
The default configuration for PHP 5 insists that the register_globals value be set to off.
This means you have to put a little more thought into your code. Rather than just receiving a
value and running with it, you must specify to PHP where the value is coming from and potentially
where you are going to put it. The following is an example of some superglobals in
action:
//Rather than accepting a value from a form like this:
$formvar = $formvar;
//Or like this:
$formvar = $HTTP_POST_VARS['formvar'];
//The new, way to receive a form var is as such:
$formvar = $_POST['formvar'];
?>
Similarly, get variables, session variables, cookies, files, and a few others are now handled
in much the same way. Consider this example with sessions that will check for a valid login:
if ($_SESSION['loggedin']){
echo "Proper login";
} else {
echo "You are not logged in.";
}
?>
By knowing exactly where your data has come from, you can prevent malicious people
from inserting false code into your premade scripts through, say, the address bar.
To get a full understanding of PHP 5 and its variable system, please see Chapter 10 by
Frank M. Kromann, where he will cover the wide world of variables in depth.

Source : PHP5 Recipes A Problem Solution Approach

0 komentar:

Post a Comment