+ All Categories

Php

Date post: 25-Jun-2015
Category:
Upload: vineet-vats
View: 904 times
Download: 1 times
Share this document with a friend
Description:
PHP training material
Popular Tags:
101
PHP
Transcript
  • PHP

  • What is PHP? PHP (recursive acronym for PHP: Hypertext Pre-processor) is a

    widely-used open source general-purpose scripting language that

    is especially suited for web development and can be embedded

    into HTML.

    Example

  • The best things in using PHP are that it is extremely simple for a newcomer, but offers

    many advanced features for a professional programmer.

    Don't be afraid reading the long list of PHP's features. You can jump in, in a short time,

    and start writing simple scripts in a few hours.

    WHAT IS PHP?Instead of lots of commands to output HTML (as seen in C or Perl),

    PHP pages contain HTML with embedded code that does

    "something" (in this case, output "Hi, I'm a PHP script!").

    The PHP code is enclosed in special start and end processing

    instructions that allow you to jump into and out of "PHP mode."

    What distinguishes PHP from something like client-side JavaScript is that the

    code is executed on the server, generating HTML which is then sent to the

    client. The client would receive the results of running that script, but would not

    know what the underlying code was.

    www.facebook.com/VineetOO7

    file:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\language.basic-syntax.phpmode.htmlfile:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\language.basic-syntax.phpmode.htmlfile:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\language.basic-syntax.phpmode.htmlfile:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\language.basic-syntax.phpmode.htmlfile:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\language.basic-syntax.phpmode.htmlfile:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\language.basic-syntax.phpmode.htmlfile:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\language.basic-syntax.phpmode.htmlfile:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\language.basic-syntax.phpmode.html
  • Anything. PHP is mainly focused on server-side scripting, so you can do anything

    any other CGI program can do, such as collect form data, generate dynamic page

    content, or send and receive cookies. But PHP can do much more

    There are three main areas where PHP scripts are used.

    Server-side scripting. This is the most traditional and main target field for PHP.

    You need three things to make this work. The PHP parser (CGI or server module),

    a web server and a web browser. You need to run the web server, with a connected

    PHP installation. You can access the PHP program output with a web browser,

    viewing the PHP page through the server. All these can run on your home machine

    if you are just experimenting with PHP programming.

    Command line scripting. You can make a PHP script to run it without any

    server or browser. You only need the PHP parser to use it this way. This type

    of usage is ideal for scripts regularly executed using cron (on *nix or Linux) or

    Task Scheduler (on Windows). These scripts can also be used for simple text

    processing tasks.

    Writing desktop applications. PHP is probably not the very best language to create a

    desktop application with a graphical user interface, but if you know PHP very well,

    and would like to use some advanced PHP features in your client-side applications

    you can also use PHP-GTK to write such programs.

    WHAT CAN PHP DO ?

    www.facebook.com/VineetOO7

  • PHP can be used on all major operating systems, including Linux, many Unix

    variants (including HP-UX, Solaris and OpenBSD), Microsoft Windows, Mac

    OS X, RISC OS, and probably others. PHP has also support for most of the web

    servers today. This includes Apache, IIS, and many others. And this includes

    any web server that can utilize the FastCGI PHP binary, like lighttpd and

    nginx. PHP works as either a module, or as a CGI processor.

    So with PHP, you have the freedom of choosing an operating system and a web

    server. Furthermore, you also have the choice of using procedural programming or

    object oriented programming (OOP), or a mixture of them both.

    One of the strongest and most significant features in PHP is its support for a wide

    range of databases. Writing a database-enabled web page is incredibly simple

    using one of the database specific extensions (e.g., for mysql)

    WHAT CAN PHP DO ?

    www.facebook.com/VineetOO7

    file:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/install.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/refs.database.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/refs.database.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/book.mysqli.html
  • Here we would like to show the very basics of PHP in a short, simple tutorial.

    This text only deals with dynamic web page creation with PHP, though PHP is

    not only capable of creating web pages.

    PHP-enabled web pages are treated just like regular HTML pages and you can

    create and edit them the same way you normally create regular HTML pages.

    Example #1 Our first PHP

    script: hello.php

    PHP Test

    EXAMPLE

    Hello.html

    PHP Test

    Hello World

    This program is extremely simple and you really did not need to use PHP to

    create a page like this. All it does is display: Hello World using the PHP echo() statement. www.facebook.com/VineetOO7

    file:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/function.echo.html
  • Let us do something more useful now. We are going to check what sort of

    browser the visitor is using. For that, we check the user agent string the

    browser sends as part of the HTTP request. This information is stored in a

    variable. Variables always start with a dollar-sign in PHP. The variable we are interested in right now is $_SERVER['HTTP_USER_AGENT'].

    $_SERVER is a special reserved PHP variable that contains all web server information. It is known as a superglobal

    EXAMPLE

    now you must be thinking what is $_SERVER ?

    www.facebook.com/VineetOO7

    file:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/reserved.variables.server.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/reserved.variables.server.html
  • LANGUAGE BASIC

    PHP tags

    Instruction separation

    Comments

    PHP tags

    When PHP parses a file, it looks for opening and closing tags, which

    are which tell PHP to start and stop interpreting the code between them. Parsing in this manner allows PHP to be embedded in all sorts

    of different documents, as everything outside of a pair of opening and closing

    tags is ignored by the PHP parser.

    PHP also allows for short tags (which are discouraged because they are only available if enabled

    with short_open_tag php.ini configuration file directive, or if PHP was configured with the --enable-short-tags option.

    If a file is pure PHP code, it is preferable omit the PHP closing tag at

    the end of the file. This prevents accidental whitespace or new lines after PHP

    closing tag which may cause unwanted effects because PHP will start output

    buffering when there is no intention from the programmer to send any output

    at that point in the script.www.facebook.com/VineetOO7

    file:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/ini.core.html
  • LANGUAGE BASIC

    2.

    echo is it good to write code like this...? No i don't think so.';

    3.

  • Comments

    LANGUAGE BASIC

    PHP supports 'C', 'C++' and Unix shell-style (Perl style) comments. For

    example:

  • Types

    LANGUAGE BASIC

    PHP supports these primitive types

    boolean

    integer

    Float

    string

    array

    object

    resource

    NULL

    Some examples:

    To forcibly convert a variable to a

    certain type, either cast the variable

    or use the settype() function on it.

    Two compound types:

    special types:

    Four scalar types:

    Php supports some more pseudo type like mixed , number , callback

    but for the time being the these are sufficient

    www.facebook.com/VineetOO7

    file:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.types.boolean.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.types.integer.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.types.float.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.types.string.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.types.array.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.types.object.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.types.resource.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.types.null.htmlfile:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\language.types.type-juggling.htmlfile:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\function.settype.htmlfile:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\function.settype.html
  • void

    void as a return type means that the return value is useless. void in a parameter list means that the function doesn't accept any parameters.

    mixed

    mixed indicates that a parameter may accept multiple (but not necessarily all) types.

    gettype() for example will accept all PHP types,

    number

    number indicates that a parameter can be either integer or float.

    ...$... in function prototypes means and so on. This variable name is used when a function can take an endless number of arguments.

    LANGUAGE BASIC

    www.facebook.com/VineetOO7

    file:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/function.gettype.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/function.gettype.html
  • LANGUAGE BASIC

    A boolean expresses a truth value. It

    can be either TRUEor FALSE.

    BooleansThis is the simplest type.

    To specify a boolean literal, use the

    keywords TRUEor FALSE. Both are case-insensitive.

    Typically, the result of

    an operator which returns

    a boolean value is passed on to a control

    structure.

    www.facebook.com/VineetOO7

    file:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\language.types.boolean.htmlfile:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\language.operators.htmlfile:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\language.types.boolean.htmlfile:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\language.control-structures.htmlfile:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\language.control-structures.html
  • Converting to boolean

    LANGUAGE BASIC

    To explicitly convert a value

    to boolean, use

    the (bool) or (boolean) casts. However, in most cases the cast

    is unnecessary, since a value

    will be automatically converted

    if an operator, function or

    control structure requires

    aboolean argument.

    When converting to boolean, the following values are

    considered FALSE:

    the boolean FALSE itself

    the integer 0 (zero)

    the float 0.0 (zero)

    the empty string, and the string "0"

    an array with zero elements

    an object with zero member variables (PHP 4 only)

    the special type NULL (including unset variables)

    SimpleXML objects created from empty tags

    Every other value is considered TRUE

    www.facebook.com/VineetOO7

    file:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.types.boolean.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.types.boolean.htmlfile:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\language.types.boolean.htmlfile:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\language.types.boolean.htmlfile:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\language.types.integer.htmlfile:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\language.types.float.htmlfile:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\language.types.string.htmlfile:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\language.types.string.htmlfile:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\language.types.array.htmlfile:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\language.types.object.htmlfile:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\language.types.null.htmlfile:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\ref.simplexml.html
  • Integers

    An integer is a number of the set = {..., -2, -1, 0, 1, 2, ...}.

    Integers can be specified in decimal (base 10), hexadecimal (base 16), octal

    (base 8) or binary (base 2) notation, optionally preceded by a sign (- or +).

    To use octal notation, precede the number with a 0 (zero). To use hexadecimal notation precede the number with 0x.

    Example #1 Integer literals

    The size of an integer is platform-dependent,

    Converting to integer

    To explicitly convert a value to integer, use either the (int) or (integer) casts. However, in most cases the cast is not needed, since a value will be

    automatically converted if an operator, function or control structure requires

    an integer argument. A value can also be converted to integer with

    the intval() function.

    www.facebook.com/VineetOO7

    file:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.types.integer.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.types.integer.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.types.integer.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.types.integer.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.types.integer.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.types.integer.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/function.intval.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/function.intval.html
  • Floating point numbers (also known as "floats", "doubles", or "real

    numbers") can be specified using any of the following syntaxes:

    Comparing floats

    As noted in the warning above, testing floating point values for equality is

    problematic, due to the way that they are represented internally. However,

    there are ways to make comparisons of floating point values that work

    around these limitations.

    To test floating point values for equality, an upper bound on the relative error

    due to rounding is used. This value is known as the machine epsilon, or unit

    roundoff, and is the smallest acceptable difference in calculations.

    $a and $b are equal to 5 digits of precision.

    Formally:LNUM [0-9]+

    DNUM ([0-9]*[\.]{LNUM}) | ({LNUM}[\.][0-9]*)

    EXPONENT_DNUM [+-]?(({LNUM} | {DNUM}) [eE][+-]? {LNUM})

    exp.

    Floating point numbers

    www.facebook.com/VineetOO7

  • Strings

    A string is series of characters, where a character is the same as a byte. This

    means that PHP only supports a 256-character set, and hence does not offer

    native Unicode support.

    Note: It is no problem for a string to become very large. PHP imposes no

    boundary on the size of a string; the only limit is the available memory of the

    computer on which PHP is running.

    A string literal can be specified in four different ways:

    single quoted

    double quoted

    heredoc syntax

    nowdoc syntax (since PHP 5.3.0)

    Single quoted

    The simplest way to specify a string is to enclose it in single quotes

    To specify a literal single quote, escape it with a backslash (\). To specify a literal backslash, double it (\\).

  • Double quoted

    If the string is enclosed in double-quotes ("), PHP will interpret

    more escape sequences for special characters: \n, \r, \t

    Heredoc

    A third way to delimit strings is the heredoc syntax:

  • example

    Strings

    www.facebook.com/VineetOO7

  • An array in PHP is actually an ordered map. A map is a type that

    associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map),

    dictionary, collection, stack, queue, and probably more. As array values can be

    other arrays, trees and multidimensional arrays are also possible.

    Explanation of those data structures is beyond the scope of this manual, but at

    least some example is provided

    An array can be created using the array() language construct. It takes any number of comma-

    separated key => value pairs as arguments.

    array(

    key => value,

    key2 => value2,

    key3 => value3,

    ...

    )

    Arrays

    www.facebook.com/VineetOO7

    file:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.types.array.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.types.array.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.types.array.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.types.array.htmlfile:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\language.types.array.htmlfile:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\function.array.html
  • Additionally the following key casts will occur:

    Strings containing valid integers will be cast to the integer type. E.g. the

    key "8" will actually be stored under 8. On the other hand "08" will not be cast, as it isn't a valid decimal integer.

    Floats are also cast to integers, which means that the fractional part will be

    truncated. E.g. the key 8.7 will actually be stored under 8.Bools are cast to integers, too, i.e. the key true will actually be stored under 1 and the key false under 0.Null will be cast to the empty string, i.e. the key null will actually be stored under "".Arrays and objects can not be used as keys. Doing so will result in a warning: Illegal offset type.

    Array

    www.facebook.com/VineetOO7

    file:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.types.string.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.types.integer.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.types.integer.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.types.float.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.types.integer.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.types.boolean.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.types.integer.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.types.null.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.types.array.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.types.object.html
  • Objects

    To create a new object, use the new statement to instantiate a class:

    Object Initialization

    For more well be back very sooooooon.......

    www.facebook.com/VineetOO7

    file:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\language.types.object.html
  • Resources

    A resource is a special variable, holding a reference to an external resource.

    Resources are created and used by special functions.

    NULL

    The special NULL value represents a variable with no value. NULL is the only

    possible value of type NULL.

    A variable is considered to be null if:

    it has been assigned the constant NULL.

    it has not been set to any value yet.

    it has been unset().

    There is only one value of type null, and that is the case-insensitive constant NULL.

    www.facebook.com/VineetOO7

    file:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.types.resource.htmlfile:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\language.types.null.htmlfile:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\language.types.null.htmlfile:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\function.unset.htmlfile:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\language.types.null.html
  • PHP does not require (or support) explicit type definition in variable

    declaration; a variable's type is determined by the context in which the

    variable is used. That is to say, if a string value is assigned to

    variable $var, $varbecomes a string. If an integer value is then assigned to $var, it becomes an integer.

    Type Juggling

    www.facebook.com/VineetOO7

    file:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.types.string.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.types.string.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.types.integer.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.types.integer.html
  • The casts allowed are:

    (int), (integer) - cast to integer

    (bool), (boolean) - cast to boolean

    (float), (double), (real) - cast to float

    (string) - cast to string

    (array) - cast to array

    (object) - cast to object

    (unset) - cast to NULL (PHP 5)

    Type casting in PHP works much as it does in C: the name of the

    desired type is written in parentheses before the variable which is to be

    cast.

    Type Casting

    www.facebook.com/VineetOO7

    file:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\language.types.integer.htmlfile:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\language.types.boolean.htmlfile:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\language.types.float.htmlfile:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\language.types.string.htmlfile:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\language.types.array.htmlfile:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\language.types.object.htmlfile:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\language.types.null.html
  • Variables

    Basics

    Variables in PHP are represented by a dollar sign followed by the name of the

    variable. The variable name is case-sensitive.

    Variable names follow the same rules as other labels in PHP. A valid variable

    name starts with a letter or underscore, followed by any number of letters,

    numbers, or underscores

    By default, variables are always assigned by value. That is to say, when you assign an expression

    to a variable, the entire value of the original expression is copied into the destination variable.

    This means, for instance, that after assigning one variable's value to another, changing one of

    those variables will have no effect on the other.

    www.facebook.com/VineetOO7

  • To assign by reference, simply prepend an ampersand (&) to the beginning of the variable which

    is being assigned (the source variable). For instance, the following code snippet outputs 'My name

    is Bob' twice:

    Variables

    www.facebook.com/VineetOO7

  • PHP provides a large number of predefined variables to any script which it

    runs.

    Variable scope

    Using global

    Predefined Variables

    www.facebook.com/VineetOO7

  • Using $GLOBALS instead of global

    www.facebook.com/VineetOO7

    file:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\reserved.variables.globals.html
  • The $GLOBALS array is an associative array with the name of the global variable being the key and the contents of that variable being the value of the

    array element. Notice how $GLOBALS exists in any scope, this is because $GLOBALS is a superglobal.

    Example demonstrating superglobals and scope

  • Another important feature of variable scoping is the static variable. A static variable exists only in a local function scope, but it does not lose its value when

    program execution leaves this scope. Consider the following example:

    Now, $a is initialized only in first call of function and every time the test() function is called it

    will print the value of $a and increment it.

    This function is quite useless since every time it is called it sets $a to 0 and prints 0. The $a++ which increments the variable serves no purpose since as soon as the function exits the $a variable disappears

    Using static variables

    www.facebook.com/VineetOO7

  • Declaring static variables

    Note:

    Static declarations are resolved in compile-time.

    www.facebook.com/VineetOO7

  • Sometimes it is convenient to be able to have variable variable names. That is, a variable name

    which can be set and used dynamically. A normal variable is set with a statement such as:

    A variable variable takes the value of a variable and treats that as the name of a variable. In the

    above example, hello, can be used as the name of a variable by using two dollar signs. i.e.

    At this point two variables have been defined and stored in the PHP symbol

    tree: $a with contents "hello" and $hello with contents "world". Therefore, this statement:

    produces the exact same output ,they both produce: hello world.

    Variable variables

    www.facebook.com/VineetOO7

  • Class properties may also be accessed using variable property names. The

    variable property name will be resolved within the scope from which the

    call is made. For instance, if you have an expression such as $foo->$bar, then the local scope will be examined for $bar and its value will be used as the name of the property of $foo. This is also true if $bar is an array access.

    The above example will output:

    I am bar.

    I am bar.

    Example: Variable property example

    www.facebook.com/VineetOO7

  • Variables From External Sources

    HTML Forms (GET and POST)

    When a form is submitted to a PHP script, the information from

    that form is automatically made available to the script. There are

    many ways to access this information, for example:

    Name:

    Email:

    www.facebook.com/VineetOO7

  • Constants

    A constant is an identifier (name) for a simple value. As the name

    suggests, that value cannot change during the execution of the script

    (except for magic constants, which aren't actually constants). A

    constant is case-sensitive by default. By convention, constant

    identifiers are always uppercase.

    Valid and invalid constant names

    Defining Constants using

    the const keyword

    www.facebook.com/VineetOO7

    file:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.constants.predefined.html
  • Operators

    An operator is something that takes one or more values (or

    expressions, in programming jargon) and yields another

    value

    www.facebook.com/VineetOO7

  • Control Structures

    if

    else

    elseif/else if

    while

    do-while

    for

    foreach

    break

    continue

    switch

    Return

    Include

    include_once

    Skip the content...

    www.facebook.com/VineetOO7

  • Functions

    User-defined functions

    Function arguments

    Returning values

    Variable functions (skiped)

    Internal (built-in) functions

    Anonymous functions

    Pseudo code to demonstrate function uses

    User-defined functionsA function may be defined using syntax such as the following:

    www.facebook.com/VineetOO7

    file:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/functions.user-defined.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/functions.user-defined.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/functions.user-defined.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/functions.arguments.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/functions.returning-values.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/functions.variable-functions.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/functions.internal.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/functions.internal.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/functions.internal.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/functions.anonymous.html
  • Conditional functions

  • Functions within functions

  • information may be passed to functions via the argument list,

    which is a comma-delimited list of expressions. The arguments

    are evaluated from left to right.

    Function arguments

    PHP supports passing arguments by value (the

    default), passing by reference, and default argument

    values. Variable-length argument lists are also supported,

    Passing arrays to functions

    Passing function parameters by

    reference

    www.facebook.com/VineetOO7

    file:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/functions.arguments.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/functions.arguments.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/functions.arguments.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/functions.arguments.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/functions.arguments.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/functions.arguments.html
  • Use of default parameters in functions

    Default argument values

    A function may define C++-style default values for scalar

    arguments as follows:

    The above example will output:Making a cup of cappuccino.

    Making a cup of .

    Making a cup of espresso.

    www.facebook.com/VineetOO7

  • PHP has support for variable-length argument lists in

    user-defined functions. This is really quite easy, using

    the func_num_args(), func_get_arg(),

    and func_get_args() functions.

    func_num_args Returns the number of arguments passed to the function

    func_num_args() exampleNumber of arguments: 3

    Variable-length argument lists

    No special syntax is required, and argument lists may

    still be explicitly provided with function definitions and will

    behave as normal.

    www.facebook.com/VineetOO7

  • func_get_arg Return an item from the argument list

    func_get_arg() example

    func_get_args Returns an array comprising a function's argument list

    www.facebook.com/VineetOO7

  • Variable functions

    PHP supports the concept of variable functions. This

    means that if a variable name has parentheses appended to it,

    PHP will look for a function with the same name as whatever

    the variable evaluates to, and will attempt to execute it.

    Among other things, this can be used to implement callbacks,

    function tables, and so forth.

    Variable function example www.facebook.com/VineetOO7

  • Anonymous functions

    Anonymous functions, also known as closures, allow the creation of functions which have no specified name. They are most useful as

    the value of callback parameters, but they have many other uses.

    Anonymous function variable assignment example

    www.facebook.com/VineetOO7

    file:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.pseudo-types.html
  • OBJECT-ORIENTED-PROGRAMMING

    Object: Repository of

    data. Milk

    Jam

    Honey

    Bread

    Biscuits

    myList

    myList is an object of class

    ShoppingList

    Terminology

    Class: Type of object ShoppingList

    ShoppingCart

    For different types of objects

    the methods of putting milk in

    them vary.

    Method: Procedure or function that operates on an object or on a class of

    objects.Milk

    Jam

    myList

    addItem(Honey)

    myList

    myShoppingCart

    www.facebook.com/VineetOO7

  • OBJECT-ORIENTED-PROGRAMMING

    Terminology

    Polymorphism: One method call can work on several different classes of

    objects, even if the classes need different implementations e.g.

    addItem method on every kind of List, even though adding

    item to ShoppingList is very different from adding milk to

    ShoppingCart.

    This is done using dynamic method

    Object Oriented: Each object knows its class and knows which methods work on

    that class. Each ShoppingList and ShoppingCart knows which

    addItem method it should use.

    Inheritance: A class can inherit properties from a more general class.

    ShoppingList inherits from List class the property of storing a

    sequence of items.

    www.facebook.com/VineetOO7

  • class

    Basic class definitions begin with the keyword class, followed by a class name, followed by a pair of curly braces which enclose the definitions of the

    properties and methods belonging to the class.

    The class name can be any valid label which is a not a PHP reserved word. A

    valid class name starts with a letter or underscore, followed by any number

    of letters, numbers, or underscores.

    A class may contain its own constants, variables (called "properties"),

    and functions (called "methods").Simple Class definition

  • To create an instance of a class, the new keyword must be used. An object will always be created unless the object has

    a constructor defined that throws an exception on error. Classes should

    be defined before instantiation (and in some cases this is a

    requirement).

    www.facebook.com/VineetOO7

    new

    file:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.oop5.decon.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.exceptions.html
  • The inherited methods and properties can be overridden by

    redeclaring them with the same name defined in the parent class. However, if

    the parent class has defined a method as final, that method may not be

    overridden. It is possible to access the overridden methods or static properties

    by referencing them with parent::.

    When overriding methods, the parameter

    signature should remain the same

    Simple Class Inheritance

    www.facebook.com/VineetOO7

    extends

    A class can inherit the methods and properties of another class by

    using the keyword extends in the class declaration. It is not possible to extend multiple classes; a class can only inherit from one base class.

    file:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.oop5.final.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.oop5.paamayim-nekudotayim.html
  • Class member variables are called "properties". You may also see them

    referred to using other terms such as "attributes" or "fields", but for the purposes of

    this reference we will use "properties". They are defined by using one of the

    keywords public, protected, or private, followed by a normal variable declaration. This declaration may include an initialization, but this initialization must

    be a constant value--that is, it must be able to be evaluated at compile time and

    must not depend on run-time information in order to be evaluated

    property declarations

    www.facebook.com/VineetOO7

    Properties

  • It is possible to define constant values on a per-class basis remaining

    the same and unchangeable. Constants differ from normal variables in that you

    don't use the $ symbol to declare or use them.The value must be a constant expression, not (for example) a variable, a

    property, a result of a mathematical operation, or a function call.

    Defining and using a constant

    www.facebook.com/VineetOO7

    Class Constants

  • Constructors and Destructors

    void __construct ([ mixed $args [, $... ]] )

    PHP 5 allows developers to declare constructor methods for classes. Classes which

    have a constructor method call this method on each newly-created object, so it is suitable for

    any initialization that the object may need before it is used.

    Note: Parent constructors are not called implicitly if the child class defines a constructor. In

    order to run a parent constructor, a call to parent::__construct() within the child

    constructor is required.

    Constructor

    Its slightly differ from java or other Object Oriented programming language..

    using new unified constructors www.facebook.com/VineetOO7

    file:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\language.pseudo-types.html
  • Constructors in namespaced classes

    www.facebook.com/VineetOO7

  • Destructor Example

    Destructor

    void __destruct ( void )

    PHP 5 introduces a destructor concept similar to that of other object-

    oriented languages, such as C++. The destructor method will be called as soon as

    there are no other references to a particular object, or in any order during the

    shutdown sequence

    Like constructors, parent destructors will not be called implicitly by the engine. In order to run

    a parent destructor, one would have to explicitly call parent::__destruct() in the destructor

    body.

    The destructor will be called even if script execution is stopped using exit(). Calling exit() in a

    destructor will prevent the remaining shutdown routines from executing.www.facebook.com/VineetOO7

    file:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/function.exit.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/function.exit.html
  • Property declaration

    /**

    * Define MyClass2

    */

    class MyClass2 extends MyClass

    {

    // We can redeclare the public and protected

    method, but not private

    protected $protected = 'Protected2';

    function printHello()

    {

    echo $this->public;

    echo $this->protected;

    echo $this->private;

    }

    }

    $obj2 = new MyClass2();

    echo $obj2->public; // Works

    echo $obj2->private; // Undefined

    echo $obj2->protected; // Fatal Error

    $obj2->printHello();

    // Shows Public, Protected2, Undefined

    ?>

  • Class methods may be defined as public, private, or protected. Methods

    declared without any explicit visibility keyword are defined as public.

  • Object Inheritance

    Inheritance is a well-established programming principle, and PHP

    makes use of this principle in its object model. This principle will affect

    the way many classes and objects relate to one another.

    when you extend a class, the subclass inherits all of the public and

    protected methods from the parent class. Unless a class overrides

    those methods, they will retain their original functionality.

    Inheritance Example

    www.facebook.com/VineetOO7

  • Scope Resolution Operator (::)

    ::from outside the class definition

    :: from inside the class definition

    www.facebook.com/VineetOO7

  • Calling a parent's method

  • Static Keyword

    Declaring class properties or methods as static makes them accessible without needing an

    instantiation of the class. A property declared as static can not be accessed with an

    instantiated class object (though a static method can).

    if no visibility declaration is used, then the property or method will be treated as if it was

    declared as public.

    Static properties cannot be accessed through the object using the arrow operator ->.

    Calling non-static methods statically generates an E_STRICTlevel warning.

    Like any other PHP static variable, static properties may only be initialized using a literal

    or constant; expressions are not allowed. So while you may initialize a static property to an integer or

    array (for instance), you may not initialize it to another variable, to a function return value, or to an

    object.

    Static property example www.facebook.com/VineetOO7

    file:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.oop5.visibility.html
  • Class Abstraction

    PHP 5 introduces abstract classes and methods. Classes defined as abstract

    may not be instantiated, and any class that contains at least one abstract method

    must also be abstract. Methods defined as abstract simply declare the method's

    signature - they cannot define the implementation.

  • Object Interfaces

    Object interfaces allow you to create code which specifies

    which methods a class must implement, without having to define how

    these methods are handled.

    Interfaces are defined using the interface keyword, in the same way as

    a standard class, but without any of the methods having their contents defined.

    All methods declared in an interface must be public, this is the nature of an

    interface.

    implements

    To implement an interface, the implements operator is used. All methods in the interface must be implemented within a class; failure to do so will result in a fatal

    error. Classes may implement more than one interface if desired by separating

    each interface with a comma.

    Interfaces can be extended like classes using the extends operator.

    The class implementing the interface must use the exact same method signatures as

    are defined in the interface. Not doing so will result in a fatal error.

    Constants

    Its possible for interfaces to have constants. Interface constants works exactly like class

    constants except they cannot be overridden by a class/interface that inherits it.www.facebook.com/VineetOO7

    file:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.oop5.inheritance.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.oop5.constants.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.oop5.constants.html
  • Interface example

    www.facebook.com/VineetOO7

  • Extendable Interfaces

    www.facebook.com/VineetOO7

  • Multiple interface inheritance

    www.facebook.com/VineetOO7

  • Interfaces with constants

    www.facebook.com/VineetOO7

  • Overloading

    Overloading in PHP provides means to dynamically "create" properties and methods.

    All overloading methods must be defined as public.

  • /** As of PHP 5.1.0 */

    public function __isset($name)

    {

    echo "Is '$name' set?\n";

    return isset($this-

    >data[$name]);

    }

    /** As of PHP 5.1.0 */

    public function __unset($name)

    {

    echo "Unsetting '$name'\n";

    unset($this->data[$name]);

    }

    /** Not a magic method, just h

    ere for example. */

    public function getHidden()

    {

    return $this->hidden;

    }

    }

    echo "\n";

    $obj = new PropertyTest;

    $obj->a = 1;

    echo $obj->a . "\n\n";

    var_dump(isset($obj->a));

    unset($obj->a);

    var_dump(isset($obj->a));

    echo "\n";

    echo $obj->declared . "\n\n";

    echo "Let's experiment with the pri

    vate property named 'hidden':\n";

    echo "Privates are visible inside t

    he class, so __get() not used...\n"

    ;

    echo $obj->getHidden() . "\n";

    echo "Privates not visible outside

    of class, so __get() is used...\n";

    echo $obj->hidden . "\n";

    ?>

    www.facebook.com/VineetOO7

  • Method overloading

    public mixed __call ( string $name , array $arguments )

    public static mixed __callStatic ( string $name , array $arguments )

    __call() is triggered when invoking inaccessible methods in an object context.

    __callStatic() is triggered when invoking inaccessible methods in a static context.

    The $name argument is the name of the method being called. The $arguments argument is an

    enumerated array containing the parameters passed to the $name'ed method.

    Overloading methods via

    the __call() and __callStatic() methods

  • Object Iteration

    PHP 5 provides a way for objects to be defined so it is possible to iterate through a list of

    items, with, for example a foreach statement. By default, all visible properties will be used

    for the iteration

    The above example will

    output:

    var1 => value 1

    var2 => value 2

    var3 => value 3

    MyClass::iterateVisible:

    var1 => value 1

    var2 => value 2

    var3 => value 3

    protected => protected

    var

    private => private var

    www.facebook.com/VineetOO7

    file:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/control-structures.foreach.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.oop5.visibility.html
  • Object Iteration implementing Iterator

    rewinding

    valid: 1

    current: 1

    key: 0 0: 1

    next: 2

    valid: 1

    current: 2

    key: 1 1: 2

    next: 3

    valid: 1

    current: 3

    key: 2 2: 3

    next:

    valid:

    www.facebook.com/VineetOO7

  • Final Keyword

    PHP 5 introduces the final keyword, which prevents child classes from overriding a

    method by prefixing the definition with final. If the class itself is being defined final

    then it cannot be extended.

    Final methods example

    Final class example

  • Object Cloning

    Creating a copy of an object with fully replicated properties is not always the wanted behavior. A

    good example of the need for copy constructors,

    An object copy is created by using the clone keyword (which calls the

    object's __clone() method if possible). An object's __clone() method cannot be called directly.

    $copy_of_object = clone $object;

    When an object is cloned, PHP 5 will perform a shallow copy of all of the object's properties.

    Any properties that are references to other variables, will remain references.

    Original Object:

    MyCloneable

    Object (

    [object1] =>

    SubObject

    Object (

    [instance] => 1 )

    [object2] =>

    SubObject

    Object (

    [instance] => 2 )

    ) Cloned Object:

    MyCloneable

    Object (

    [object1] =>

    SubObject

    Object (

    [instance] => 3 )

    [object2] =>

    SubObject

    Object (

    [instance] => 2 )

    )

    www.facebook.com/VineetOO7

    file:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.oop5.cloning.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.oop5.cloning.html
  • Comparing Objects

    In PHP 5, object comparison is more complicated than in PHP 4 and more in accordance to what

    one will expect from an Object Oriented Language (not that PHP 5 is such a language).

    When using the comparison operator (==), object variables are compared in a simple manner,

    namely: Two object instances are equal if they have the same attributes and values, and are

    instances of the same class.

    On the other hand, when using the identity operator (===), object variables are identical if and

    only if they refer to the same instance of the same class.

  • class OtherFlag

    {

    public $flag;

    function OtherFlag($flag = true

    ) {

    $this->flag = $flag;

    }

    }

    $o = new Flag();

    $p = new Flag();

    $q = $o;

    $r = new OtherFlag();

    echo "Two instances of the same cla

    ss\n";

    compareObjects($o, $p);

    echo "\nTwo references to the same

    instance\n";

    compareObjects($o, $q);

    echo "\nInstances of two different

    classes\n";

    compareObjects($o, $r);

    ?>

    Two instances of the

    same class

    o1 == o2 : TRUE

    o1 != o2 : FALSE

    o1 === o2 : FALSE

    o1 !== o2 : TRUE

    Two references to the

    same instance

    o1 == o2 : TRUE

    o1 != o2 : FALSE

    o1 === o2 : TRUE

    o1 !== o2 : FALSE

    Instances of two

    different classes

    o1 == o2 : FALSE

    o1 != o2 : TRUE

    o1 === o2 : FALSE

    o1 !== o2 : TRUE

    www.facebook.com/VineetOO7

  • Late Static Bindings

    HP implements a feature called late static bindings which can be used to

    reference the called class in a context of static inheritance.

    More precisely, late static bindings work by storing the class named in the last "non-forwarding

    call". In case of static method calls, this is the class explicitly named (usually the one on the left

    of the :: operator); in case of non static method calls, it is the class of the object. A "forwarding

    call" is a static one that is introduced by self::, parent::, static::, or, if going up in the class

    hierarchy, forward_static_call(). The function get_called_class() can be used to retrieve a string

    with the name of the called class and static::introduces its scope

    This feature was named "late static bindings" with an internal perspective in mind. "Late binding"

    comes from the fact that static:: will not be resolved using the class where the method is defined

    but it will rather be computed using runtime information. It was also called a "static binding" as it

    can be used for (but is not limited to) static method calls.

    Limitations of self::

    Static references to the current class

    like self:: or __CLASS__ are resolved using the class in which

    the function belongs, as in where it was defined:

    self:: usage

    The above example will output:

    A

    forward_static_call() - Call a

    static method

    www.facebook.com/VineetOO7

    file:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/language.oop5.paamayim-nekudotayim.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/function.forward-static-call.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/function.forward-static-call.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/function.get-called-class.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/function.get-called-class.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/function.forward-static-call.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/function.forward-static-call.html
  • Late Static Bindings' usage

    Late static bindings tries to solve that limitation by introducing a keyword that references the

    class that was initially called at runtime. Basically, a keyword that would allow you to

    reference B from test() in the previous example. It was decided not to introduce a new keyword

    but rather use static that was already reserved.

    Example #2 static:: simple usage

    The above example will output:

    B

    In non-static contexts, the called class will be the class of the

    object instance. Since $this->will try to call private methods

    from the same scope, using static:: may give different results.

    Another difference is that static:: can only refer to static

    properties.

    www.facebook.com/VineetOO7

  • static:: usage in a non-static context

    The above example will output:

    success!

    success!

    success!

    Late static bindings' resolution will stop at a fully

    resolved static call with no fallback. On the other hand,

    static calls using keywords like parent:: or self:: will

    forward the calling information.

    www.facebook.com/VineetOO7

  • Objects and references

    One of the key-points of PHP 5 OOP that is often mentioned is that "objects are passed by

    references by default". This is not completely true. This section rectifies that general thought

    using some examples.

    A PHP reference is an alias, which allows two different variables to write to the same value. As of

    PHP 5, an object variable doesn't contain the object itself as value anymore. It only contains an

    object identifier which allows object accessors to find the actual object. When an object is sent by

    argument, returned or assigned to another variable, the different variables are not aliases: they

    hold a copy of the identifier, which points to the same object.

    References and Objects

    The above example will output:

    2 2 2

    www.facebook.com/VineetOO7

  • Object Serialization

    Serializing objects - objects in sessions

    serialize() returns a string containing a byte-stream representation of any value that can be stored in

    PHP.unserialize() can use this string to recreate the original variable values. Using serialize to save an object

    will save all variables in an object. The methods in an object will not be saved, only the name of the class.

    In order to be able to unserialize() an object, the class of that object needs to be defined. That is, if you have

    an object of class A and serialize this, you'll get a string that refers to class A and contains all values of

    variables contained in it. If you want to be able to unserialize this in another file, an object of class A, the

    definition of class A must be present in that file first. This can be done for example by storing the class

    definition of class A in an include file and including this file or making use of

    the spl_autoload_register()function.

    www.facebook.com/VineetOO7

    file:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/function.serialize.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/function.unserialize.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/function.unserialize.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/function.unserialize.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/function.unserialize.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/function.spl-autoload-register.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/function.spl-autoload-register.html
  • Namespaces What are namespaces?

    In the broadest definition namespaces are a way of encapsulating items. This can be seen as an

    abstract concept in many places.

    For example, in any operating system directories serve to group related files, and act as a

    namespace for the files within them.

    In the PHP world, namespaces are designed to solve two problems that authors of libraries and

    applications encounter when creating re-usable code elements such as classes or functions:

    1.Name collisions between code you create, and internal PHP classes/functions/constants or

    third-party classes/functions/constants.

    2.Ability to alias (or shorten) Extra_Long_Names designed to alleviate the first problem,

    improving readability of source code.

    PHP Namespaces provide a way in which to group related classes, interfaces, functions and

    constants. Here is an example of namespace syntax in PHP:

    www.facebook.com/VineetOO7

  • Defining namespaces

    Although any valid PHP code can be contained within a namespace, only four types of

    code are affected by namespaces: classes, interfaces, functions and constants.

    Namespaces are declared using the namespace keyword. A file containing a

    namespace must declare the namespace at the top of the file before any other code

    Declaring a single namespace

    The only code construct allowed before a namespace declaration is the declare statement,

    for defining encoding of a source file. In addition, no non-PHP code may precede a

    namespace declaration, including extra whitespace:

    www.facebook.com/VineetOO7

  • Declaring sub-namespaces

    Much like directories and files, PHP namespaces also contain the ability to specify a

    hierarchy of namespace names. Thus, a namespace name can be defined with sub-levels:

    Defining multiple namespaces in the same file

    Multiple namespaces may also be declared in the same file. There are two allowed syntaxes.

    This syntax is not recommended for combining namespaces into a single file.www.facebook.com/VineetOO7

  • Declaring multiple namespaces,

    bracketed syntax

    Declaring multiple namespaces and

    unnamespaced code

    www.facebook.com/VineetOO7

  • /* Unqualified name */

    foo(); // resolves to function Foo\Bar\foo

    foo::staticmethod(); // resolves to class Foo\Bar\foo, method staticmethod

    echo FOO; // resolves to constant Foo\Bar\FOO

    /* Qualified name */

    subnamespace\foo(); // resolves to function Foo\Bar\subnamespace\foo

    subnamespace\foo::staticmethod(); // resolves to class Foo\Bar\subnamespace\foo,

    // method staticmethod

    echo subnamespace\FOO; // resolves to constant Foo\Bar\subnamespace\FOO

    /* Fully qualified name */

    \Foo\Bar\foo(); // resolves to function Foo\Bar\foo

    \Foo\Bar\foo::staticmethod(); // resolves to class Foo\Bar\foo, method staticmethod

    echo \Foo\Bar\FOO; // resolves to constant Foo\Bar\FOO

    ?>

    file1.php

    file2.php

  • Using namespaces: Aliasing/Importing

    The ability to refer to an external fully qualified name with an alias, or importing, is an important

    feature of namespaces. This is similar to the ability of unix-based filesystems to create symbolic

    links to a file or to a directory.

    PHP namespaces support three kinds of aliasing or importing: aliasing a class name, aliasing an

    interface name, and aliasing a namespace name. Note that importing a function or constant is not

    supported.

    In PHP, aliasing is accomplished with the use operator. Here is an example showing all 3 kinds of

    importing:

    importing/aliasing with the use operator

    www.facebook.com/VineetOO7

  • Exception

    PHP 5 has an exception model similar to that of other programming languages. An

    exception can be thrown, and caught ("catched") within PHP. Code may be surrounded in a try block, to facilitate the catching of potential exceptions.

    When an exception is thrown, code following the statement will not be executed, and PHP

    will attempt to find the first matching catch block. If an exception is not caught, a PHP Fatal Error will be issued with an "Uncaught Exception ..."

    Each try must have at least one corresponding catch block. Multiple catch blocks can be used to catch different classes of exceptions. Normal execution (when no exception is

    thrown within the try block, or when a catch matching the thrown exception's class is not present) will continue after that last catch block defined in sequence. Exceptions

    can be thrown (or re-thrown) within a catch block.

    www.facebook.com/VineetOO7

  • Nested / Custom Exception

    www.facebook.com/VineetOO7

  • Exception

    Exception is the base class for all Exceptions.

    Exception::getMessage() example

    The above example will output something similar to:

    Some error message

    Exception::getPrevious() example

    Looping over, and printing out, exception trace.

    The above example will output something

    similar to:

    /home/bjori/ex.php:8 Something happend

    (911) [MyCustomException]

    /home/bjori/ex.php:6 You are doing it

    wrong! (112) [InvalidArgumentException]

    www.facebook.com/VineetOO7

  • Protocol support by php

    Filesystem

    ftp

    http

    www.facebook.com/VineetOO7

  • Cookies

    PHP transparently supports HTTP cookies. Cookies are a mechanism for storing data in the

    remote browser and thus tracking or identifying return users. You can set cookies using

    the setcookie() function. Cookies are part of the HTTP header, so setcookie() must be called

    before any output is sent to the browser.

    Any cookies sent to you from the client will automatically be included into

    a $_COOKIE auto-global array

    setcookie

    setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like

    other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any

    output, including and tags as well as any whitespace.

    Once the cookies have been set, they can be accessed on the next page load

    with the $_COOKIE or $HTTP_COOKIE_VARS arrays. Cookie values also exist in $_REQUEST.

    Http protocol

    www.facebook.com/VineetOO7

    file:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\function.setcookie.htmlfile:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\function.setcookie.htmlfile:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\function.setcookie.htmlfile:///D:\study Material\tutorial & ebooks\php\php-chunked-xhtml\function.setcookie.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/reserved.variables.cookies.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/reserved.variables.cookies.htmlfile:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/reserved.variables.request.html
  • setcookie() send example

    setcookie() delete

    www.facebook.com/VineetOO7

  • The above example

    will output:

    three : cookiethree two

    : cookietwo one :

    cookieone

    www.facebook.com/VineetOO7

  • Sessions

    Introduction

    Session support in PHP consists of a way to preserve certain data across

    subsequent accesses. This enables you to build more customized applications and

    increase the appeal of your web site.

    A visitor accessing your web site is assigned a unique id, the so-called session

    id. This is either stored in a cookie on the user side or is propagated in the

    URL.

    The session support allows you to store data between requests in

    the $_SESSION superglobal array

    www.facebook.com/VineetOO7

    file:///D:/study Material/tutorial & ebooks/php/php-chunked-xhtml/reserved.variables.session.html
  • bool session_start ( void )

    session_start() creates a session or resumes the current one based on a session

    identifier passed via a GET or POST request, or passed via a cookie.

    session_start Start new or resume existing session

    page1.php

    page2.php

    www.facebook.com/VineetOO7

  • session_regenerate_id

    session_regenerate_id Update the current session id with a newly

    generated one

    A session_regenerate_id() example

    www.facebook.com/VineetOO7

  • session_name

    session_name Get and/or set the current session name

    session_name() returns the name of the current session. If name is given, session_name() will

    update the session name and return the old session name.

    session_name() example

    string session_name ([ string $name ] )

    www.facebook.com/VineetOO7


Recommended