Sunteți pe pagina 1din 26

PHP DEMO

How to run PHP files on localhost?


1. First download the xampp from

http://www.apachefriends.org/en/xampp.htm l
2. Extract the file and double click the exe.

This will install Mysql and Apache servers.

Cont..
3. Once the installation is complete, you will find

XAMPP under Start / Programs / XAMPP.


4. You can use the XAMPP Control Panel to start/stop

all servers. Start Mysql and Apache servers.


5. C:/Program Files/XAMPP/htdocs/
6. To run the php file, you just need to browse

http://localhost/test.php

C-like language
Free format - white space is ignored.

Statements are terminated by semi-colon ;


Statements grouped by { } Comments begin with // or a set of

comments /* */

Continue..
Assignment is =: $a=6

Relational operators are ,< , > == ( not a single equal) Control structures include if (cond) {..} else { }, while (cond) { .. } , for(startcond; increment; endcond) { }

Continue..
Functions are called with the name followed by arguments in a fixed order enclosed in ( ) :

substr(fred,0,2)

Case sensitive - $fred is a different variable to

$FRED

Arrays are accessed with [ ] : $x[4] is the 5th element of the array $x indexes start at 0

Demo
Example script <? PHP ..?> <script language="PHP">

</script>

Comments
PHP allows a couple of additional ways to create comments.
<?php phpinfo(); # This is a built-in function ?>

Multiple line comments.

<?php /* A script that gets information about the PHP version being used. */ <? phpinfo(); ?>

If Use Improper Syntax


Suppose you use the wrong syntax:

<?php print ( A simple initial script); ?>

A Little About PHP's Syntax


Some PHP Syntax Issues:
Be careful to use quotation marks, parentheses, and

brackets in pairs.
Most PHP commands end with a semicolon (;). Be careful of case. PHP ignores blank spaces.

OOModel
Procedural language

Extensive Function Library


Not fully object-oriented
Java is fully object oriented all functions have to be

in a class In PHP, classes are additional but quite simple to use

DECLARING A CLASS
class MyClass {

... // List of methods ... ... // List of properties ... }

Constructor
Unied constructor name __construct().

Instead of the constructor being the name of the class, it is now declared as __construct
class MyClass { function __construct() { print "Inside constructor"; } }

Destructor
Object destructor support by dening a

__destructor() method. Allows dening a destructor function that runs when an object is destroyed: class MyClass { function __destruct() { print Destroying object; } }

Class constants
Class constants. Class denition can now include constant

values. using the class: class MyClass { const SUCCESS = "Success"; const FAILURE = "Failure"; } print MyClass::SUCCESS;

Interfaces
Gives the ability for a class to fulll more than one is-a

relationships. A class can inherit only from one class, but may implement as many interfaces as it wants:

interface Display {

function display(); } class Circle implements Display { function display() { print "Displaying circle\n"; } }

Instanceof
instanceof operator. Language-level support for is-a relationship

checking. if ($obj instanceof Circle) { print '$obj is a Circle'; }

Final methods
The final keyword allows you to mark

methods so that an inheriting class cannot overload them. class MyClass { final function getBaseClassName() { return __CLASS__; } }

Final classes.
After declaring a class as final It cannot be

inherited. final class FinalClass { } class BogusClass extends FinalClass {}

Array
An array in PHP is a collection of key/value

pairs. This means that it maps keys (or indexes) to values.


Array indexes can be either integers or

strings whereas values can be of any type (including other arrays).

array()
Arrays can be declared using the array()

construct. Language construct, which generally takes the following form (elements inside square brackets, [], are optional):

Syntax:
array([key =>] value, [key =>] value, ..)

Array Example
array(1, 2, 3) is the same as the more explicit

array(0 => 1, 1 => 2, 2 => 3). array("name" => "John", "age" => 28) array(1 => "ONE", "TWO", "THREE") is equivalent to array(1 => "ONE", 2 => "TWO", 3 => "THREE"). array() an empty array

nested array() statement:

array(array("name" => "John", "age" => 28),

array("name" => "Barbara", "age" => 67))

OPERATORS
PHP contains three types of operators:

unary operators, binary operators, and one ternary operator. Binary operators are used on two operands: 2+3 14 * 3.1415 $i 1

Cont
These examples are also simple examples of expressions. PHP can only perform binary operations on two operands that have the same type. However, if the two operands have different types, PHP automatically converts one of them to the others type, according to the following rules (unless stated differently, such as in the concatenation operator).

Queries?
Any Questions www.php.net

Community www.phpbuilder.com: articles on PHP, discussion forums Newsgroups comp.lang.php

S-ar putea să vă placă și