Vous êtes sur la page 1sur 6

Super- global variables

These include $_GET, $_POST, $_COOKIE, $_SESSION, $_SERVER, $_ENV, $_FILES and
$_REQUEST. Most of these are fairly self-explainatory. $_GET are the variables passed by forms with
a GET method or after the end of the path such as example.com/search.php?
GETARG1=GETVAL1&foo=bar. $_POST is any variable submitted by a form with method POST.
$_COOKIE is full of cookies setup by your PHP script. $_FILES is full of any uploaded files submitted
through a form with enctype multipart/data and method POST. $_SESSION are variables you
have set and committed to the session. $_ENV are environment settings from the operating system.
$_SERVER are variables set up by Apache. They’re all fairly self explainatory.

$_REQUEST seems sporadic. Sometimes it matches $_POST, sometimes $_GET, sometimes it seems
to pick and choose.
Its logic is fairly simple. Request is full of the “unsafe” variables. It will generally contain $_GET,
$_POST and $_COOKIE. This is configurable and is documented. In the beginning of execution,
$_REQUEST is a clone of $_GET. $_POST is then merged into the array, overwriting keys if they exist
in both $_GET and $_POST. Finally, $_COOKIE is merged into the array, again overwriting old
values.

What are Magic Quotes


When on, all ' (single-quote), " (double quote), \ (backslash) and NULL characters are escaped with a
backslash automatically. This is identical to what addslashes() does.
There are three magic quote directives:
• magic_quotes_gpc Affects HTTP Request data (GET, POST, and COOKIE). Cannot be set at
runtime, and defaults to on in PHP. See also get_magic_quotes_gpc().
• magic_quotes_runtime If enabled, most functions that return data from an external source,
including databases and text files, will have quotes escaped with a backslash. Can be set at
runtime, and defaults to off in PHP. See also set_magic_quotes_runtime() and
get_magic_quotes_runtime().
• magic_quotes_sybase If enabled, a single-quote is escaped with a single-quote instead of a
backslash. If on, it completely overrides magic_quotes_gpc. Having both directives enabled
means only single quotes are escaped as ''. Double quotes, backslashes and NULL's will remain
untouched and unescaped. See also ini_get() for retrieving its value.

Removing Backslashes - stripslashes()


Before you use PHP's backslash removal function stripslashes it's smart to add some magic quote
checking like our "Are They Enabled?" section above. This way you won't accidentally be removing
slashes that are legitimate in the future if your PHP's magic quotes setting changes in the future.
magic-quotes.php Code:
<?php
echo "Removed Slashes: ";
// Remove those slashes
if(get_magic_quotes_gpc())
echo stripslashes($_POST['question']);
else
echo $_POST['question'];

?>

<form method='post'>
Question: <input type='text' name='question'/><br />
<input type='submit'>

</form>

Class

Class – Class is a collection of properties and methods.

Constructor
In PHP 5 developers can declare constructor methods for classes. In this, those classes which have a
constructor method call this method for each newly created object.

Destructors
The destructor concept is introduced in PHP 5. This destructor concept is similar to other object
oriented languages. In this the destructor will be called as soon as all references to a particular object
have been removed or when the object has been explicitly destroyed.

Abstract Class - In the simplest sense, an abstract class is a class that cannot (and should not) be
instantiated. Abstract class shouldn’t provide any explicit definition for its methods or properties. These
methods and properties should be specifically defined within the subclasses, which automatically
would inherit them from the base class.

Magic Methods
The function names _construct, _sleep, _wakeup, _tostring are all magical methods in PHP. There
cannot be any functions with these names in the classes. These functions can be had only if we want the
magical functionality associated with them.
_sleep
The _sleep magic method is used to close any database connections that the object may have. It is also
used to commit pending data and perform similar cleanup tasks. It is also used if there are very large
objects which do not have to be saved completely.
_wakeup
The _wakeup magic method is used to reconstruct any resources that the object may have. The basic
function of _wakeup is to reestablish any database connections that may have been lost during
serialization and perform other initialization tasks.
_tostring
The _tostring magic method allows a class to decide that how it will react when it is converted to
string.

The Global Statement


The global statement comes in handy when you want to access variables that are declared outside the
function. In this if a variable is declared and assigned a value outside a function, and then we declare a
global variable of the same name in the function, then that function can access the variable that has
been declared outside the function.

The Static Keyword


The static keyword is used when declaring variables in functions. The purpose of the static variable is
to inform PHP that this variable should not be destroyed when the given function executes. Instead of
destroying that variable, the value of that variable is saved for the next time needed.
The static variable is used to retain certain variables within the function for the life of the execution of
the script.

The Keyword Final


The final keyword prevents the child classes from overriding a method. This can be done by prefixing
the method with the keyword final. If the complete class is being defined as final then that class cannot
be extended.

Abstract
A new concept of abstract classes and methods has been introduced in PHP5. When a class is defined
as abstract then it is not allowed to create the instance of that class. A class that contains at least one
abstract method must also be abstract. The methods defined as abstract cannot define the
implementation; they just declare the method’s signature.
When a child class is inheriting from an abstract parent class, then all the methods marked abstract in
parent class declaration must also be additionally defined by the child class. These methods must be
defined with the same or weaker access. This means that if an abstract method is declared as protected
in the parent class then it must be declared either as protected or public in the child class.
Static Keyword
When class members or methods are declared as static then there is no need to instantiate that class.
These members and methods are accessible without the instantiation of the class. If a member is
declared as static then it cannot be accessed by an instantiated class object, but a method declared as
static can be accessed by an instantiated class object.
The static declaration of a class must be after the visibility declaration (means that after the member or
method has been declared as public, protected, or private).
The static method calls are resolved at compile time and static properties cannot be accessed through
the object through the arrow operator (->).
Interfaces
Object interfaces allow the creation of a code which specifies that which method a class must
implement, without having to define how these methods have to be handled.
Interfaces are defined in the same way as a class is defined. These interfaces are defined with the
keyword “interface”. In the interface the contents of the methods do not have to be defined and all the
methods declared in the interface must be declared as public.
Implementation of Interfaces
To implement an interface, the implements operator is used. The methods must be defined before
implementation and all the methods in the interface must be implemented within a class.
Exceptions
Exception handling in PHP is similar to that of other programming languages. Within a PHP block of
code we can throw, try and catch an exception. There must be at least one catch block in a try block. In
this multiple catch blocks can be used to catch different class types. In exception handling the
execution will continue after the last catch block has been encountered and exceptions can be thrown
within catch blocks.
In exception handling when an exception is thrown, the code following the statement will not be
executed rather PHP will attempt to find the first matching catch block. If the exception is not caught
then it will result in a fatal error with an uncaught exception message.
ob_start

bool ob_start ([ callback $output_callback [, int $chunk_size [, bool $erase ]]] )

This function will turn output buffering on. While output buffering is active no output is sent from the
script (other than headers), instead the output is stored in an internal buffer.
The contents of this internal buffer may be copied into a string variable using ob_get_contents(). To
output what is stored in the internal buffer, use ob_end_flush(). Alternatively, ob_end_clean() will
silently discard the buffer contents.
Output buffers are stackable, that is, you may call ob_start() while another ob_start() is active. Just
make sure that you call ob_end_flush() the appropriate number of times. If multiple output callback
functions are active, output is being filtered sequentially through each of them in nesting order.

MySQL 5.1 supported storage engines


• MyISAM: The default MySQL storage engine and the one that is used the most in Web, data
warehousing, and other application environments. MyISAM is supported in all MySQL
configurations, and is the default storage engine unless you have configured MySQL to use a
different one by default.
• InnoDB: A transaction-safe (ACID compliant) storage engine for MySQL that has commit,
rollback, and crash-recovery capabilities to protect user data. InnoDB row-level locking
(without escalation to coarser granularity locks) and Oracle-style consistent nonlocking reads
increase multi-user concurrency and performance. InnoDB stores user data in clustered indexes
to reduce I/O for common queries based on primary keys. To maintain data integrity, InnoDB
also supports FOREIGN KEY referential-integrity constraints.
• Memory: Stores all data in RAM for extremely fast access in environments that require quick
lookups of reference and other like data. This engine was formerly known as the HEAP engine.
• Merge: Enables a MySQL DBA or developer to logically group a series of identical MyISAM
tables and reference them as one object. Good for VLDB environments such as data
warehousing.
• Archive: Provides the perfect solution for storing and retrieving large amounts of seldom-
referenced historical, archived, or security audit information.
• Federated: Offers the ability to link separate MySQL servers to create one logical database
from many physical servers. Very good for distributed or data mart environments.
• NDBCLUSTER (also known as NDB)—This clustered database engine is particularly suited for
applications that require the highest possible degree of uptime and availability.
Note
The NDBCLUSTER storage engine is not supported in standard MySQL 5.1
releases. MySQL Cluster users wishing to upgrade from MySQL 5.0 should
instead migrate to MySQL Cluster NDB 6.3, 7.0, or 7.1; these are based on
MySQL 5.1 but contain the latest improvements and fixes for NDBCLUSTER.
• CSV: The CSV storage engine stores data in text files using comma-separated values format.
You can use the CSV engine to easily exchange data between other software and applications
that can import and export in CSV format.
• Blackhole: The Blackhole storage engine accepts but does not store data and retrievals
always return an empty set. The functionality can be used in distributed database design where
data is automatically replicated, but not stored locally.

Drop a single column


The query to drop a single column is very simple. In the example table "test" there is a field called
"foo" that is to be dropped. This is done like so:
ALTER TABLE test
DROP COLUMN foo;

Depending how large the table is this may take a few milliseconds to a number of minutes to run.

Drop multiple columns


Multiple columns can be dropped with a single query by simply comma separating a list of DROP
COLUMN statments. To drop both the "foo" and "bar" columns from the "test" table do this:
ALTER TABLE test
DROP COLUMN foo,
DROP COLUMN bar;

As many additional columns can be added to the list as required. Again, this may only take a few
milliseconds to many minutes depending on the size of the data in the table.

To detect the operating system on the client machine, your script should analyze the
navigator.appVersion string. Below is a simple example of a script that sets the variable
OSName to reflect the actual client OS.
// This script sets OSName variable as follows:
// "Windows" for all versions of Windows
// "MacOS" for all versions of Macintosh OS
// "Linux" for all versions of Linux
// "UNIX" for all other UNIX flavors
// "Unknown OS" indicates failure to detect the OS

var OSName="Unknown OS";


if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";
if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";

document.write('Your OS: '+OSName);

Vous aimerez peut-être aussi