Introduction to php basics

Post on 18-Jan-2015

467 views 8 download

Tags:

description

 

transcript

Introduction to PHP

PHPPHP Hypertext Pre-processor

‚PHP is a server side scripting language‛

“Personal home page”

Generate HTML

Get index.php13

4

pass index.php to PHP interpretor

5

WebServer

Index.php in interpreted HTMl form

Browser

2Get index.php from hard disk

What you mean by hypertext Pre-processor?

Scripting Languages

• A ‚script‛ is a collection of program or sequence of instructions that is

interpreted or carried out by another program rather than by the computer

processor. Two types of scripting languages are

– Client-side Scripting Language

– Server-side Scripting Language

• In server-side scripting, (such as PHP, ASP) the script is processed by the

server Like: Apache, ColdFusion, ISAPI and Microsoft's IIS on Windows.

• Client-side scripting such as JavaScript runs on the web browser.

What you mean by server side scripting??

Generate HTML

Get index.php13

4

pass index.php to PHP interpretor

5

WebServer

Index.php in interpreted HTMl form

Browser

2Get index.php from hard disk

PHP is server side scripting language because the php code runs on the server and returns the result in html form to the

client browser

What you mean by client side scripting??

Generate HTML

Get index.php13

4

pass index.php to PHP interpretor

5

WebServer

Index.php in interpreted HTMl form

Browser

2Get index.php from hard disk

Client side scripting is something where code runs on client

side(browser). For eg checking whether a HTML text field contains data or not can be done by running

a script from browser itself

Creating a PHP page

How to create a PHP page

1. Install Wamp (Windows-Apache-Mysql-PHP)

2. Create a new folder within Wamp\WWW folder with the name

you want to give your project

• Eg: C:\Wamp\WWW\myProject

3. Right click and Create a new text file with extention ‚.php‛

• Eg: index.php

How to run PHP program

1. Start Wamp Server

2. Open your browser and type localhost/myProject/

3. Most of the browsers load the file with name ‚index‛ automatically. So it

is recommended to have the name ‚index‛ for your home page

Getting started with PHP programming

PHP Syntax

Structurally similar to C/C++

Supports procedural and object-oriented paradigm (to some degree)

All PHP statements end with a semi-colon

Each PHP script must be enclosed in the reserved PHP tag

<?php

//PHP code goes here..

?>

Other PHP tags

<?php

... Code

?>

Standard Tags

<?

... Code

?><?= $variable ?>

<script language=“php”>

... Code

</script>

<%

... Code

%>

Short Tags

Script TagsASP Tags

Commenting

// comment single line

/* comment

multiple lines */

//comment single line

/* Comment

multiple Lines*/

# comment single line-shell style

C PHP

Data Types

Data types

• PHP supports many different data types, but they are generally

divided in two categories:

• Scalar

» boolean :A value that can only either be true or false

» Int :A signed numeric integer value

» float : A signed floating-point value

» string : A collection of binary data

• Composite.

» Arrays

» Objects

Type Casting

echo ((0.1 + 0.7) * 10); //Outputs 8

echo (int) ((0.1 + 0.7) * 10); //Outputs 7

• This happens because the result of this simple arithmetic

expression is stored internally as 7.999999 instead of 8; when the

value is converted to int, PHP simply truncates away the fractional

part, resulting in a rather significant error (12.5%, to be exact).

Variables

Declaring a variable

//Declaring a variable

Int a=10;

Char c=‘a’;

Float f=1.12;

//Every variable starts with ‚$‚sign

//No need of prior Declarations

$a=10;

$c=‘a’;

$f=1.12;

C PHP

Variables

• Variables are temporary storage containers.

• In PHP, a variable can contain any type of data, such as, for example,

strings, integers, floating numbers, objects and arrays.

• PHP is loosely typed, meaning that it will implicitly change the type of a

variable as needed, depending on the operation being performed on its

value.

• This contrasts with strongly typed languages, like C and Java, where

variables can only contain one type of data throughout their existence.

Variable Variables

• In PHP, it is also possible to create so-called variable variables.

That is a variable whose name is contained in another variable.

For eg:

$name = ’foo’;

$$name = ’bar’;

echo $foo; // Displays ’bar’

function myFunc()

{

echo ’myFunc!’;

}

$f = ’myFunc’;

$f(); // will call myFunc();

Determining If a Variable Exists

• There is an inbuilt method called isset() which will return true if a

variable exists and has a value other than null

$x=12;

echo isset ($x); // returns true

echo isset($y); // returns false

Constants

Declaring a constant

//Declaring a constant using ‘const’

const int a=10;

int const a=10;

//Declaring a constant using ‘#define’

#define TRUE 1

#define FALSE 0

#define NAME_SIZE 20

define(’name’, ’baabtra’);

define(’EMAIL’, ’davey@php.net’);

echo EMAIL; // Displays ’davey@php.net’

echo name.PHP_EOL.email;

// Displays ’baabtra;

‘davey@php.net’

C PHP

Constants

• Conversely to variables, constants are meant for defining immutable values.

• Constants can be accessed for any scope within a script; however, they can

only contain scalar values.

• The built-in PHP_EOL constant represents the ‚end of line‛ marker.

OutPut

output

Int a=10,b=25;

Printf(‚%d %d‛,a,b);

$a=10; $b=25

Printf(‚%d %d‛,$a,$b);

print $a;

print $b; //Cannot take multiple arguments

echo $a,$b; //Fast and commonly used

die(‚the end‛);

exit(‚the end‛); // both allows to terminate

the script’s output by outputting a string

C PHP

echo Vs print

• echo is a language construct ( Constructs

are elements that are built-into the language

and, therefore, follow special rules)

• echo doesnt have a return value

• print() is not a language construct. It

is an inbuilt function only

• Always return 1 once it print

someting

Control structures

Control Structures

Conditional Control Structures

• If

• If else

• Switch

Loops

For

While

Do while

Other

• Break

• Continue

‚Exactly the same as on left side‛

C PHP

functions

Functions

Int findSum(int a,int b)

{

Int c;

c=a+b;

Return c

}

findSum(10,15);

function findSum($a,$b)

{

$c=$a+$b;

Return $c;

}

findSum(10,15);

C PHP

Function arguments

• You can define any number of arguments and, in fact, you can pass an

arbitrary number of arguments to a function, regardless of how many you

specified in its declaration.PHP will not complain unless you provide fewer

arguments than you declared

Eg:

function greeting($arg1)

{

echo ‚hello $ arg1‛;

}

greeting(‚baabtra‛);

greeting(‚baabtra‛, ‛baabte‛);

greeting(); // ERROR

Function arguments

• You can make arguments optional by giving them a default value.

function hello($who = "World"){

echo "Hello $who";}hello(‘baabtra’); // hello baabtrahello(); // hello world

Function arguments

PHP provides three built-in functions to handle variable-length

argument lists:

– func_num_args() : return the total number of arguments

– func_get_arg() : return the argument at any index position

– func_get_args() : returns an array of all argument

Function argumentsfunction hello(){

if (func_num_args() > 0){

$arg = func_get_arg(0); // The first argument is at position 0echo "Hello $arg";

} else {

echo "Hello World";}

}hello("Reader"); // Displays "Hello Reader"hello(); // Displays "Hello World“

Function return type

• In PHP functions even if you don’t return a value, PHP will still

cause your function to return NULL. So there is no concept of

‘void’

Strings

Char a*+=‚Baabtra‛;

Printf(‚Hello %s‛,a);

$a=‚Baabtra‛;

echo ‚Hello $a‛; //Output: Hello baabtra;

echo ‘Hello $a’; //Outputs : Hello $a

‚Strings will be continued in coming

chapters‛

C PHP

Arrays

Indexed Array

int a[]={1,2,3,4,5,6,7,8,9,10};

for(i=0;i<10;i++)

Printf(‚%d‛,a[i]);

Indexed Array / Enumerated array

$a=array(1,2,3,4,5,6,7,8,9,10)

for($i=0;$i<10;$i++)

echo $a[$i] ;

Associative Array

$a*‘name’+=‚John‛;

$a*‘age’+=24;

$a*‘mark’+=35.65;

‚Array will be continued in coming

chapters‛

C PHP

Operators

Operators

C

Arithmetic Operators

+, ++, -, --, *, /, %

Comparison Operators

==, !=, ===, !==, >, >=, < , <=

Logical Operators

&&, ||, !

Assignment Operators

=, +=, -=, *=, /=, %=

PHP

‚Supports all on left . In addition

PHP supports below operators as well‛

• Execution Operators

• String operators

• Type Operators

• Error Control Operators

• Bitwise Operators

• The backtick operator makes it possible to execute a shell command and

retrieve its output. For example, the following will cause the output of the

UNIX ls commandto be stored inside $a:

Eg : $a = ‘ls -l‘;

Execution operator (‘)

Don’t confuse the backtick operator with regular quotes (and, conversely, don’t

confuse the latter with the former!)

– The dot (.) operator is used to concatenate two strings

Eg1: $string=‚baabtra‛. ‚mentoring parner‛

Eg2: $string1=‚baabtra‛;

$string2=‚mentoring parner‛;

$string3=$string1.$string2;

String operators (.)

– Used to determine whether a variable is an instantiated object of a certain class or

an object of a class that inherits from a parent class

Eg: class ParentClass

{ }

class MyClass extends ParentClass

{ }

$a = new MyClass;

var_dump($a instanceof MyClass); // returns true as $a is an object of MyClass

var_dump($a instanceof ParentClass); // returns true as $a is an object of MyClass

which inherits parentClass

Type operators (instanceof )

Error suppression operator (@)

• When prepended to an expression, this operator causes PHP to ignore

almost all error messages that occur while that expression is being evaluated

Eg: $x = @mysql_connect(‘localhost’,’root’,’1234’);

• The code above will prevent the call to mysql_connect() from outputting an

error—provided that the function uses PHP’s own functionality for

reporting errors.

• Sadly, some libraries output their errors directly, bypassing PHP and,

therefore, make it much harder to manage with the error-control operator.

• Bitwise operators allow you to manipulate bits of data. All these operators are

designed to work only on integer numbers—therefore, the interpreter will attempt

to convert their operands to integers before executing them.

Bitwise Operators

& Bitwise AND. The result of the operation will be a value whose bits are set if they are set in

both operands, and unset otherwise.

| Bitwise OR. The result of the operation will be a value whose bits are set if they are set in

either operand (or both), and unset otherwise.

ˆ Bitwise XOR (exclusive OR). The result of the operation will be a value whose bits are set if

they are set in either operand, and unset otherwise.

<< Bitwise left shift. This operation shifts the left-hand operand’s bits to the left by a number of

positions equal to the right operand, inserting unset bits in the shifted positions

>> Bitwise right shift. This operation shifts the left-hand operand’s bits to the right by a number

of positions equal to the right operand, inserting unset bits in the shifted positions.

Questions?

‚A good question deserve a good grade…‛

End of day

Contact Us

Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

Start up VillageEranakulam,Kerala, India.

Email: info@baabtra.com