php and mysql

Post on 18-Nov-2014

444 Views

Category:

Education

6 Downloads

Preview:

Click to see full reader

DESCRIPTION

These are some basics you need to start with PHP, Any queries, get in touch!!

TRANSCRIPT

By Sanketkumar Biswas

1

Client Server Arch.

2

Server Side Scripting

Generates dynamic web pages.

Practically invisible to end - user.

3

Web Application

An application that runs within a web browser.

And can be accessed over a network.

4

What is PHP ?

It is a server side scripting language used for web development.

Created by Rasmus Lerdorf in 1995

5

Why PHP ?

Open Source

Very good set of built in functions

Very good database support

Great support community

Many frameworks

6

Getting started with PHP

<?php

echo "Hello World";

?>

7

Including files

include('/filepath/filename/')

include_once('/filepath/filename/')

require('/filepath/filename/')

require_once('/filepath/filename/')

8

PHP Syntax

PHP is programmer centric.

HTML and PHP are not same.

PHP is white space insensitive.

PHP is sometimes case sensitive.

Termination by semicolon (;)

Braces define blocks.

9

Programmer Centric

Focuses on convenience for programmer.

Does not focus much on correctness.

10

HTML Vs. PHP

HTML is client side.

PHP is server side.

11

White space Insensitive

<?php

$val = 2 + 2; //single spaces

$val = 2 + 2; //spaces and tabs

$val =

2

+ 2; //multiple lines

?>

12

Case Sensitive

<?php

$val = 20;

echo "variable value is $val";

echo "variable value is $VaL";

?>

13

Case Sensitive

<?php

HELLO();

function hello() {

EcHo "Hello!!";

}

?>

14

Termination

<?php

echo "Hello World";

?>

15

Braces define block

<?php

if (1 == 1)

echo "Hello World";

if (1 == 1) {

echo "Hello ";

echo "World";

}

?>

16

Commenting

<?php

// this is a single line comment.

# this is also a single line comment.

/* this is a

multi line

comment. */

?>

17

Variables

Variables are denoted with a leading ($) sign.

Value of variable is the most recent assignment.

Variable is assigned using (=) operator, to its left is the variable and to right is the expression to be evaluated.

18

Variables can, but do not need to be declared before assignment.

Variables have no type other than the type of its current value.

Variables should always start with an alphabet.

Numerical values for a variable can be INT, HEX, OCT, FLOAT and e.

19

In variables for text we can use (\) as escape character.

Boolean values is not case sensitive, TRUE, TRuE, true, trUE, etc.

Prefix for HEX is "0x" and for OCT is "0".

Range of INT varies with respect to processor bits, i.e. different for 32 and 64 bit processor.

20

$x = 'Hello';

$x = "Hello";

$x = <<<var

Hello

var;

Single quotes display as it is, whereas double quotes and heredoc do interpretation.

21

Creating a variable

$x = 3;

Changing value

$x = $x + 5;

Changing value as well as type

$x = $x + 0.5;

22

Global Variables:

$x = "HELLO";

function test() {

global $x;

return $x;

}

echo test();

Super Global Variables: $_POST, $_GET, etc.

23

Outputting

echo "Hello World \n Welcome to PHP";

echo "Hello World <br> Welcome to PHP";

echo "$var1 \n $var2";

24

Arrays

Creating arrays

$array = array(1, -2, 'cat', TRUE);

$array[] = 2.6;

$array[5] = 012;

$array[] = 0xA;

$array[7] = 5e2;

25

Creating arrays with index

$array = array(

0 => 1,

1 => -2,

2 => 'cat',

3 => TRUE

);

26

Creating arrays with string index

$array = array(

'INT' => 1,

'NINT' => -2,

'STRING' => 'cat',

'BOOLEAN' => TRUE

);

27

Creating multi dimensionsl arrays

$array = array(

'INT' => array(

'POS' => 1,

'NEG' => -2

)

);

28

Deleting arrays

unset($array[2]);

Unset is not the same as,

$array[2] = "";

29

Iterating arrays

$array = array(

'INT' => 1, 'NINT' => -2, 'STRING' => 'cat'

);

foreach($array as $element) {

echo $element."<br>";

}

30

Operators

Arithmetic > Comparison > Logical

Arithmetic: +, -, *, /, %

Comparison: ==, ===, !=, !==, ......

Logical: and, or, !, .......

break: used to stop loops.

continue: used to stop iteration.

31

Arithmetic Operators

32

Comparison Operators

33

Logical Operators

34

Constants

Defining a constant

define("CONSTANT_NAME","VALUE");

35

Type Casting

$variable = (TYPE) VALUE;

$x = 5.5;

$y = (INT) $x; // $y = 5

36

Functions

Declaration:

function NAME(ARG1, ARG2=DEFAULT);

Eg:

$x = "Hello";

function app($x,$s="World") {

return $x . $s;

}

$a = app($x); // HelloWorld

$b = app($y,"India"); // HelloIndia

37

Some Important Functions

strlen(): gives length of string.

strcmp(x,y): case sensitive string compare.

strcasecmp(x,y): case in sensitive string compare.

strncmp(x,y,n): checks first n characters.

38

strtoupper(x): converts to uppercase.

strtolower(x): converts to lowercase.

trim(x): removes white space.

substr(x,m,n): creates substring of x from m to n characters.

ord(x): gives int value for character.

39

count(x): gives no. Of elements in array.

isset(x): checks if has value.

sort(x): sorts array wrt values.

ksort(x): sorts array wrt keys.

array_merge(x,y): merge arrays to a new one.

40

array_slice(x,m,n): creates a sub array of x from m key to the next n keys.

41

Branching

if... else...

if ($a > $b) {

$c = $a - $b;

echo $c;

} else {

$c = $b - $a;

echo $c;

}

42

switch

switch($a) {

case 1: echo "one"; break;

case 2: echo "two"; break;

case 3: echo "three"; break;

default: echo "no number"; break;

}

43

Looping

while

$count = 1;

while ($count <= 10) {

echo "Count is $count \n";

$count++;

}

44

do - while

$count = 45;

do {

echo "Count is $count \n";

$count++;

} while ($count <= 10);

45

for

$limit = 5;

for($count=0; $count<$limit; $count++)

{

echo "Count is $count \n";

}

46

PHP and Forms

HTTP is a stateless protocol

Data is sent using

GET

POST

47

To collect data for PHP processing we use super global variables.

$_GET

$_POST

48

PHP and MySQL

Initialise a database connection

mysql_connect('host','username','pass');

mysql_select_db('dbname');

Firing a query

mysql_query('Query');

49

<?php

mysql_connect('localhost','root','');

mysql_select_db('demo');

$res = mysql_query('SELECT * FROM table');

while ($row = mysql_fetch_assoc($res))

{ echo $row['name']; }

?>

50

Session

PHP Session variable is super global.

It has values of a single user across many pages in an application.

It is located server side.

51

Starting Session

session_start();

Session Variable

$_SESSION['username']

Destroying Session

session_destroy();

52

Cookie

Cookie is also super global variable.

It is used to store information about user and its visit on user's computer.

It is located client side.

53

Setting cookie

setcookie('name','rohit',time()+(3600));

Cookie variable

$_COOKIE['name']

54

Thank You

Follow me on:

Facebook: http://fb.com/sanketkumarbiswas/

Twitter: @sankeybiswas

LinkedIn: http://linkedin.com/in/sanketkumarbiswas/

Web: http://www.sankeybiswas.com/

Cell: +91 9820 477 377

55

top related