Академический Документы
Профессиональный Документы
Культура Документы
PHP
HYPERTEXT
PREPROCESSOR
PHP started out as a small open source project that evolved as more and more people
found out how useful it was. Rasmus Lerdorf unleashed the first version of PHP way back
in 1994.
• PHP is a server side scripting language that is embedded in HTML. It is used to manage
dynamic content, databases, session tracking, even build entire ecommerce sites.
• It is integrated with a number of popular databases, including MySQL, PostgreSQL, Oracle,
Sybase, Informix, and Microsoft SQL Server.Ajouter votre troisième point ici
• PHP can handle forms, i.e. gather data from files, save data to a file, through email you can
send data, return data to the user.
• You add, delete, modify elements within your database through PHP.
• Access cookies variables and set cookies.
• Using PHP, you can restrict users to access some pages of your website.
• It can encrypt data.
PHP ─ Variable &
Data Types
PHP ─ Variable &
Data Types
PHP VARIABLE RULES
• All variables in PHP are denoted with a leading dollar sign ($).
• The value of a variable is the value of its most recent assignment.
• Variables are assigned with the = operator, with the variable on the
left-hand side and the expression to be evaluated on the right.
• Variables can, but do not need, to be declared before assignment.
• Variables in PHP do not have intrinsic types - a variable does not
know in advance
• whether it will be used to store a number or a string of characters.
TYPES OF PHP VARIABLE
1. Local Variables:
A variable declared in a function is considered local; that is, it can be referenced solely in
that function. Any assignment outside of that function will be considered to be an
entirely
different variable from the one contained in the function
2. PHP Global Variables:
In contrast to local variables, a global variable can be accessed in any part of the
program.
However, in order to be modified, a global variable must be explicitly declared to be
global
in the function in which it is to be modified. This is accomplished, conveniently enough,
by
placing the keyword GLOBAL in front of the variable that should be recognized as global.
Placing this keyword in front of an already existing variable tells PHP to use the variable
having that name.
TYPES OF PHP VARIABLE
3. Static Variables :
The final type of variable scoping that I discuss is known as static. In contrast to the
variables declared as function parameters, which are destroyed on the function's exit, a
static variable will not lose its value when the function exits and will still hold that value
should the function be called again.
You can declare a variable to be static simply by placing the keyword STATIC in front of
the variable name.
4. Function Parameters:
PHP Functions are covered in detail in PHP Function Chapter. In short, a function is a
small
unit of program which can take some input in the form of parameters and does some
processing and may return a value.
Function parameters are declared after the function name and inside parentheses. They
are declared much like a typical variable would be:
PHP DATA TYPES
• Integers: are whole numbers, without a decimal point, like 4195.
• Doubles: are floating-point numbers, like 3.14159 or 49.1.
• Booleans: have only two possible values either true or false.
• NULL: is a special type that only has one value: NULL.
• Strings: are sequences of characters, like 'PHP supports string operations.'
• Arrays: are named and indexed collections of other values.
• Objects: are instances of programmer-defined classes, which can package
up both other kinds of values and functions that are specific to the class.
• Resources: are special variables that hold references to resources external
to PHP (such as database connections).
PHP DATA TYPES- INTEGERS
They are whole numbers, without a decimal point, like 4195. They
are the simplest type .they correspond to simple whole numbers,
both positive and negative. Integers can be assigned to variables,
or they can be used in expressions, like so:
• $int_var = 12345;
• $another_int = -12345 + 12345;
PHP DATA TYPES- Doubles
They like 3.14159 or 49.1. By default, doubles print with the
minimum number of decimal places needed. For example, the
code:
• $many = 2.2888800;
• $many_2 = 2.2111200;
• $few = $many + $many_2;
• print(.$many + $many_2 = $few<br>.);
PHP DATA TYPES- Boolean
They have only two possible values either true or false. PHP
provides a couple of constants especially for use as Booleans:
TRUE and FALSE, which can be used like so:
if (TRUE)
print("This will always print<br>");
else
print("This will never print<br>");
PHP DATA TYPES- NULL
NULL is a special type that only has one value: NULL. To give a
variable the NULL value, simply assign it like this:
$my_var = NULL;
The special constant NULL is capitalized by convention, but actually it is case
insensitive, you could just as well have typed:
$my_var = null;
PHP DATA TYPES- Strings
They are sequences of characters, like "PHP supports string
operations". Following are valid examples of string:
To define a constant you have to use define() function and to retrieve the value of a
constant, you have to simply specifying its name. Unlike with variables, you do not
need to have a constant with a $. You can also use the function constant() to read a
constant's value if you wish to obtain the constant's name dynamically.
constant() function
As indicated by the name, this function will return the value of the constant.
This is useful when you want to retrieve value of a constant, but you do not know its name,
i.e., it is stored in a variable or returned by a function.
Differences between constants and
variables
• There are no need to write a dollar sign ($) befor a constant, where
as one has to write the dollar sign ($).
• Constants can not be defined by simple assignments, they may only
be defined using the defined function.
• Constants may be defined and accessed anywhere without regards
to variable scoping rules.
• Once a constant is set it may not be redefined or undefined.
PHP CONSTANTS EXAMPLE
<?php
define("MINSIZE", 50);
echo MINSIZE;
echo constant("MINSIZE"); // same thing as the previous line
?>
PHP CONSTANTS EXAMPLE
<?php
// Valid constant names
define("ONE", "first thing");
define("TWO2", "second thing");
define("THREE_3", "third thing")
?>
PHP ─ Operator Types
What is Operator? Simple answer can be given using
expression 4 + 5 is equal to 9.
Here 4 and 5 are called operands and + is called operator. PHP
language supports following
type of operators.
• Arithmetic Operators
• Comparison Operators
• Logical (or Relational) Operators
• Assignment Operators
• Conditional (or ternary) Operators
Arithmetic Operators
OPERATORS DESCRIPTION
+ Subtracts second operand from the first
Syntax
if (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;
The ElseIf Statement
If you want to execute some code if one of the several conditions is
true, then use the elseif statement.
Syntax
if (condition)
code to be executed if condition is true;
elseif (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;
The Switch Statement
If you want to select one of many blocks of code to be executed, use the Switch statement. The
switch statement is used to avoid long blocks of if..elseif..else code.
Syntax
switch (expression)
{
case label1:
code to be executed if expression = label1;
break;
case label2:
code to be executed if expression = label2;
break;
default:
code to be executed
if expression is different
from both label1 and label2;
}
PHP ─ Loop Types
• for - loops through a block of code a specified number of times.
• while - loops through a block of code if and as long as a specified
condition is true.
• do...while - loops through a block of code once, and then repeats
the loop as long as a special condition is true.
• foreach - loops through a block of code for each element in an
array.
The for loop statement
The for statement is used when you know how many times you want
to execute a statement or a block of statements
Syntax
Syntax
while (condition)
{
code to be executed;
}
The do...while loop statement
The do...while statement will execute a block of code at least once - it
will then repeat the loop as long as a condition is true.
Syntax
do
{
code to be executed;
}while (condition);
The foreach loop statement
The foreach statement is used to loop through arrays. For each pass
the value of the current array element is assigned to $value and the
array pointer is moved by one and in the next pass next element will
be processed.
Syntax
• Numeric array - An array with a numeric index. Values are stored and accessed in linear fashion
• Associative array - An array with strings as index. This stores element values in association with
key values rather than in a strict linear index order.
• Multidimensional array - An array containing one or more arrays and values are accessed using
multiple indices
Php Strings Function
• strlen() -The strlen() function is used to find the length of a string.
• strpos() -The strpos() function is used to search for a string or
character within a string.
• strtoupper() - This function changes all string characters to
uppercase.
• strtolower() - This function changes all string characters to
lowercase.
Difference between print and echo
They are both used to output data to the screen.
The differences are small: echo has no return
value while print has a return value of 1 so it can
be used in expressions. echo can take multiple
parameters (although such usage is rare)
while print can take one argument. echo is
marginally faster than print .
PHP SUPER GLOBAL VARIABLES
• $GLOBALS
• $_SERVER
• $_REQUEST
• $_POST
• $_GET
• $_FILES
• $_ENV
• $_COOKIE
• $_SESSION
PHP $GLOBALS
$GLOBALS is a PHP super global variable which is
used to access global variables from anywhere in
the PHP script (also from within functions or
methods).
PHP stores all global variables in an array called
$GLOBALS[index].
PHP $_SERVER
$_SERVER is a PHP super global variable which
holds information about headers, paths, and
script locations.
PHP $_REQUEST
PHP $_REQUEST is used to collect data after
submitting an HTML form.
PHP $_POST
PHP $_POST is widely used to collect form data after
submitting an HTML form with method="post". $_POST is also
widely used to pass variables.
When to use POST?
• Information sent from a form with the POST method
is invisible to others (all names/values are embedded within
the body of the HTTP request) and has no limits on the
amount of information to send.
• Moreover POST supports advanced functionality such as
support for multi-part binary input while uploading files to
server.
PHP $_GET
PHP $_GET can also be used to collect form data after submitting an
HTML form with method="get".
$_GET can also collect data sent in the URL.
When to use GET?
• Information sent from a form with the GET method is visible to
everyone (all variable names and values are displayed in the URL).
GET also has limits on the amount of information to send. The
limitation is about 2000 characters. However, because the variables
are displayed in the URL, it is possible to bookmark the page. This
can be useful in some cases.
• GET may be used for sending non-sensitive data.
PHP Cookies
A cookie is a small text file that lets you store a
small amount of data (nearly 4KB) on the user's
computer. They are typically used to keeping
track of information such as username that the
site can retrieve to personalize the page when
user visit the website next time.
Setting a Cookies
Syntax
setcookie(name, value, expire, path, domain, secure);
Parameter Description
name The name of the cookie.
value The value of the cookie. Do not store sensitive information since this value is stored on the
user's computer.
expires The expiry date in UNIX timestamp format. After this time cookie will become inaccessible. The
default value is 0.
path Specify the path on the server for which the cookie will be available. If set to /, the cookie will
be available within the entire domain.
domain Specify the domain for which the cookie is available to e.g www.example.com.
secure This field, if present, indicates that the cookie should be sent only if a secure HTTPS connection
exists.
PHP Sessions
Although you can store data using cookies but it has some security issues. Since
cookies are stored on user's computer it is possible for an attacker to easily
modify a cookie content to insert potentially harmful data in your application that
might break your application.
Also every time the browser requests a URL to the server, all the cookie data for a
website is automatically sent to the server within the request. It means if you
have stored 5 cookies on user's system, each having 4KB in size, the browser
needs to upload 20KB of data each time the user views a page, which can affect
your site's performance.
You can solve both of these issues by using the PHP session. A PHP session stores
data on the server rather than user's computer. In a session based environment,
every user is identified through a unique number called session identifier or SID.
This unique session ID is used to link each user with their own information on the
server like emails, posts, etc.
Starting a PHP Session
Before you can store any information in session variables, you must
first start up the session. To begin a new session, simply call the PHP
session_start() function. It will create a new session and generate a
unique session ID for the user.
<?php
// Starting session
session_start();
?>
Storing and Accessing Session Data
You can store all your session data as key-value pairs in the
$_SESSION[] superglobal array. The stored data can be accessed
during lifetime of a session. Consider the following script, which
creates a new session and registers two session variables.
<?php
// Starting session
session_start();
// Storing session data
$_SESSION["firstname"] = "Peter";
$_SESSION["lastname"] = "Parker";
?>
Destroying a single Session
If you want to remove certain session data, simply unset the
corresponding key of the $_SESSION associative array, as shown in
the following example:
<?php
// Starting session
session_start();
// Removing session data
if(isset($_SESSION["lastname"])){
unset($_SESSION["lastname"]);
}
?>
Destroying all Session
However, to destroy a session completely, simply call the
session_destroy() function. This function does not need any argument
and a single call destroys all the session data.
<?php
// Starting session
session_start();
// Destroying session
session_destroy();
?>
TO BE CONTINUED
AND
THANKS FOR YOUR ATTENTION
Contact @:
radjimubarak@gmail.com
Telephone:
+229 66 33 49 80