PHP and MySQL

55
By Sanketkumar Biswas 1

description

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

Transcript of PHP and MySQL

Page 1: PHP and MySQL

By Sanketkumar Biswas

1

Page 2: PHP and MySQL

Client Server Arch.

2

Page 3: PHP and MySQL

Server Side Scripting

Generates dynamic web pages.

Practically invisible to end - user.

3

Page 4: PHP and MySQL

Web Application

An application that runs within a web browser.

And can be accessed over a network.

4

Page 5: PHP and MySQL

What is PHP ?

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

Created by Rasmus Lerdorf in 1995

5

Page 6: PHP and MySQL

Why PHP ?

Open Source

Very good set of built in functions

Very good database support

Great support community

Many frameworks

6

Page 7: PHP and MySQL

Getting started with PHP

<?php

echo "Hello World";

?>

7

Page 8: PHP and MySQL

Including files

include('/filepath/filename/')

include_once('/filepath/filename/')

require('/filepath/filename/')

require_once('/filepath/filename/')

8

Page 9: PHP and MySQL

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

Page 10: PHP and MySQL

Programmer Centric

Focuses on convenience for programmer.

Does not focus much on correctness.

10

Page 11: PHP and MySQL

HTML Vs. PHP

HTML is client side.

PHP is server side.

11

Page 12: PHP and MySQL

White space Insensitive

<?php

$val = 2 + 2; //single spaces

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

$val =

2

+ 2; //multiple lines

?>

12

Page 13: PHP and MySQL

Case Sensitive

<?php

$val = 20;

echo "variable value is $val";

echo "variable value is $VaL";

?>

13

Page 14: PHP and MySQL

Case Sensitive

<?php

HELLO();

function hello() {

EcHo "Hello!!";

}

?>

14

Page 15: PHP and MySQL

Termination

<?php

echo "Hello World";

?>

15

Page 16: PHP and MySQL

Braces define block

<?php

if (1 == 1)

echo "Hello World";

if (1 == 1) {

echo "Hello ";

echo "World";

}

?>

16

Page 17: PHP and MySQL

Commenting

<?php

// this is a single line comment.

# this is also a single line comment.

/* this is a

multi line

comment. */

?>

17

Page 18: PHP and MySQL

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

Page 19: PHP and MySQL

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

Page 20: PHP and MySQL

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

Page 21: PHP and MySQL

$x = 'Hello';

$x = "Hello";

$x = <<<var

Hello

var;

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

21

Page 22: PHP and MySQL

Creating a variable

$x = 3;

Changing value

$x = $x + 5;

Changing value as well as type

$x = $x + 0.5;

22

Page 23: PHP and MySQL

Global Variables:

$x = "HELLO";

function test() {

global $x;

return $x;

}

echo test();

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

23

Page 24: PHP and MySQL

Outputting

echo "Hello World \n Welcome to PHP";

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

echo "$var1 \n $var2";

24

Page 25: PHP and MySQL

Arrays

Creating arrays

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

$array[] = 2.6;

$array[5] = 012;

$array[] = 0xA;

$array[7] = 5e2;

25

Page 26: PHP and MySQL

Creating arrays with index

$array = array(

0 => 1,

1 => -2,

2 => 'cat',

3 => TRUE

);

26

Page 27: PHP and MySQL

Creating arrays with string index

$array = array(

'INT' => 1,

'NINT' => -2,

'STRING' => 'cat',

'BOOLEAN' => TRUE

);

27

Page 28: PHP and MySQL

Creating multi dimensionsl arrays

$array = array(

'INT' => array(

'POS' => 1,

'NEG' => -2

)

);

28

Page 29: PHP and MySQL

Deleting arrays

unset($array[2]);

Unset is not the same as,

$array[2] = "";

29

Page 30: PHP and MySQL

Iterating arrays

$array = array(

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

);

foreach($array as $element) {

echo $element."<br>";

}

30

Page 31: PHP and MySQL

Operators

Arithmetic > Comparison > Logical

Arithmetic: +, -, *, /, %

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

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

break: used to stop loops.

continue: used to stop iteration.

31

Page 32: PHP and MySQL

Arithmetic Operators

32

Page 33: PHP and MySQL

Comparison Operators

33

Page 34: PHP and MySQL

Logical Operators

34

Page 35: PHP and MySQL

Constants

Defining a constant

define("CONSTANT_NAME","VALUE");

35

Page 36: PHP and MySQL

Type Casting

$variable = (TYPE) VALUE;

$x = 5.5;

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

36

Page 37: PHP and MySQL

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

Page 38: PHP and MySQL

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

Page 39: PHP and MySQL

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

Page 40: PHP and MySQL

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

Page 41: PHP and MySQL

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

41

Page 42: PHP and MySQL

Branching

if... else...

if ($a > $b) {

$c = $a - $b;

echo $c;

} else {

$c = $b - $a;

echo $c;

}

42

Page 43: PHP and MySQL

switch

switch($a) {

case 1: echo "one"; break;

case 2: echo "two"; break;

case 3: echo "three"; break;

default: echo "no number"; break;

}

43

Page 44: PHP and MySQL

Looping

while

$count = 1;

while ($count <= 10) {

echo "Count is $count \n";

$count++;

}

44

Page 45: PHP and MySQL

do - while

$count = 45;

do {

echo "Count is $count \n";

$count++;

} while ($count <= 10);

45

Page 46: PHP and MySQL

for

$limit = 5;

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

{

echo "Count is $count \n";

}

46

Page 47: PHP and MySQL

PHP and Forms

HTTP is a stateless protocol

Data is sent using

GET

POST

47

Page 48: PHP and MySQL

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

$_GET

$_POST

48

Page 49: PHP and MySQL

PHP and MySQL

Initialise a database connection

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

mysql_select_db('dbname');

Firing a query

mysql_query('Query');

49

Page 50: PHP and MySQL

<?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

Page 51: PHP and MySQL

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

Page 52: PHP and MySQL

Starting Session

session_start();

Session Variable

$_SESSION['username']

Destroying Session

session_destroy();

52

Page 53: PHP and MySQL

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

Page 54: PHP and MySQL

Setting cookie

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

Cookie variable

$_COOKIE['name']

54

Page 55: PHP and MySQL

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