topic 2 : php

33
Topic 2 : PHP Er. Pradip Kharbuja 1

Upload: pradip-kharbuja

Post on 18-Jan-2015

1.997 views

Category:

Education


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Topic 2 : PHP

Topic 2 : PHPEr. Pradip Kharbuja

1

Page 2: Topic 2 : PHP

PHP• PHP : PHP Hypertext Prepocessor

• Open Source Software

Free to download and use

• PHP is a server side scripting language.

You request a page on the internet.

The server interprets the PHP.

It returns the results of that interpretation to you as an HTML page.

2

Page 3: Topic 2 : PHP

Starting PHP• A web-server with PHP installed. eg. XAMPP, WAMP

• Some kind of programming environment.

Notepad++, Dreamweaver, Eclipse, Netbeans

• An internet browser to interact with the application.

3

Page 4: Topic 2 : PHP

A PHP Script<html>

<head>

<title>My First PHP Script</title>

</head>

<body>

<?php echo "<p>Hello World!</p>"; ?>

</body>

</html>

4

Page 5: Topic 2 : PHP

My First PHP Script• PHP works like standard HTML, except you can set

sections of the page to be interpreted by the server.

• PHP sections are marked by blocks.

<?php starts a block of PHP

?> ends a block of PHP

All of your PHP code goes in this block.

• The echo function is used to output some text to the browser.

This script will display the text “Hello World” in a browser.

5

Page 6: Topic 2 : PHP

The Produced HTML<html>

<head>

<title>My First PHP Script</title>

</head>

<body>

<p>Hello World!</p>

</body>

</html>

6

Page 7: Topic 2 : PHP

Variables• starts with a $ symbol.

• We do not provide the type of a variable just a variable name.

• eg.<?php

$text = "Hello World";

echo $text;

?>

<?php

$name = "John";

echo "Hello $name";

echo "Hello {$name}";

?>

7

Page 8: Topic 2 : PHP

Operators• Arithmetic, Assignment, Comparison, Logical, Conditional

Operators similar to JavaScript

• String Operators

. .=<?php

$first_name = "Bill";

$last_name = "Gates";

echo $first_name . " " . $last_name;

$full_name = $first_name;

$full_name .= " " . $last_name;

ehco $full_name;

?>8

Page 9: Topic 2 : PHP

Conditional Operators1. if statement

2. if...else statement

3. if...else if....else statement

4. switch statement

Task

Write different programs to demonstrate above statements.

9

Page 10: Topic 2 : PHP

Loop Statements1. for loop

2. while loop

3. do....while loop

4. foreach loop

Task

Write different programs to demonstrate above statements.

Page 11: Topic 2 : PHP

Array1. Numeric Array

2. Associative Array

3. Multidimesional Array

11

Page 12: Topic 2 : PHP

Numeric Array• An array with a numeric ID key

• Example #1 : ID key is automatically assigned.

$names = array("Peter", "Joe", "John");

• Example #2 : ID key is assigned manually.

$names = array();

$names[0] = "Peter";

$names[1] = "Joe";

$names[2] = "John";

12

Page 13: Topic 2 : PHP

Numeric Array [Contd.]<?php

$names[0] = "Peter";

$names[1] = "Joe";

$names[2] = "John";

echo $names[1] . " and " . $names[2] . " are " . $names[0] . "'s neighbours.";

?>

Output :

Joe and John are Peter's neighbours.

13

Page 14: Topic 2 : PHP

Associative Array• When storing data, numeric array is not always the best way to do.

• Example #1

$ages = array("Peter" => 32,

"Joe" => 30,

"Bill" => 34);

• Example #2

$ages["Peter"] = 32;

$ages["Joe"] = "30";

$ages["Bill"] = 34;

14

Page 15: Topic 2 : PHP

Multidimensional Array• each element in main array can also be an array

• and each element in sub array can be an array too & so on.

$courses = array(

"level4" => array("Database", "Java", "OSD"),

"level5" => array("PHP", "Agile", "Network"),

"level6" => array("C#", "Advanced Database")

);

15

Page 16: Topic 2 : PHP

foreach loop• foreach statement is used to loop through arrays

Syntax:

foreach(array as value)

{

//code to be executed

}

16

Page 17: Topic 2 : PHP

foreach loop example<?php

$subjects = array("Dynamic Website", "Database", "Java");

echo "I studied following subject : ";

foreach($subjects as $subject)

{

echo "<br/>{$subject}";

}

?>

17

Page 18: Topic 2 : PHP

Functions• similar to JavaScript

<?php

function add($x, $y){

$total = $x + $y;

return $total;

}

echo "1 + 16 = " . add(1, 16);

echo "20 + 9 = " . add(20, 9);

?>

18

Page 19: Topic 2 : PHP

HTML Forms & User Input- need HTML to get user input using get / post method

<form action = "result.php" method = "get">

<p>What is your name?</p>

<input type = "text" name = "full_name">

<p>What is your question?</p>

<input type = "text" name = "question">

<input type = "submit" value = "Ask">

<input type = "reset" value = "Clear values">

</form>

19

Page 20: Topic 2 : PHP

result.php<body>

<?php

$full_name = $_GET['full_name'];

$pass_word = $_GET['pass_word'];

$question = $_GET['question'];

?>

<p><?php echo "<b>{$full_name}</b> asked : "; ?></p>

<p><?php echo $question; ?></p>

</body>

20

Page 21: Topic 2 : PHP

HTML Forms & User Input- getting user input using post method

<form action = "result_post.php" method = "post">

<p>What is your name?</p>

<input type = "text" name = "full_name">

<p>What is your question?</p>

<input type = "text" name = "question">

<input type = "submit" value = "Ask">

<input type = "reset" value = "Clear values">

</form>

21

Page 22: Topic 2 : PHP

result_post.php<body>

<?php

$full_name = $_POST['full_name'];

$pass_word = $_POST['pass_word'];

$question = $_POST['question'];

?>

<p><?php echo "<b>{$full_name}</b> asked : "; ?></p>

<p><?php echo $question; ?></p>

</body>

22

Page 23: Topic 2 : PHP

Why use PHP?• PHP runs on different platforms (Windows, Linux, Unix, etc.)

• PHP is compatible with almost all servers used today (Apache, IIS, etc.)

• PHP is FREE to download from the official PHP resource: www.php.net

• PHP is easy to learn and runs efficiently on the server side

• PHP supports many databases (MySQL, Informix, Oracle, Sybase, PostgreSQL, Generic ODBC, etc.)

23

Page 24: Topic 2 : PHP

Why not use PHP?• It is hard to find good ‘example’ programs.

• Unlike JavaScript, PHP requires server to execute.

• the names of built-in functions are inconsistent.

array_rand( ) - Pick random entries out of an array

shuffle( ) - shuffles array

24

Page 25: Topic 2 : PHP

Some More about PHP• PHP does not distinguish between variables of one type, and

variables of another in code.

They are all just ‘variables’.

• In technical terms, it is loosely typed.

This means you have to be careful.

• PHP is famous for type juggling. The automatic type conversions that PHP performs

25

Page 26: Topic 2 : PHP

Type Juggling.

1. $foo = "0";

2. $foo++;

3. $foo = $foo + 1.3;

4. $foo= array( );

5. $foo= "car";

6. $foo[0] = "b"; echo $foo;

// String

// Integer

// Double

// Array

// String

// bar

26

We can find type of variable using gettype( ) function

Page 27: Topic 2 : PHP

Type Casting• Changing the contents of a variable from one type to another.

• This is done through type casting:

$num = 10; //integer

$strnum = (string)$num; //string

1. (int), (integer) - cast to integer

2. (bool), (boolean) - cast to boolean

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

4. (string) - cast to string

5. (array) - cast to array

6. (object) - cast to object

27

Page 28: Topic 2 : PHP

String Comparison in PHP• You might be tempted to use the == operator to compare

strings in PHP.

You will not get the behaviour you want doing that.

The == in PHP is called a loose comparison operator.

• PHP offers strict comparison operators too

===

!==

These should be used for string comparisons

28

Page 29: Topic 2 : PHP

Terminology1. Loosely typed

A programming language that does not require the type of variables to be declared.

2. Type juggling

The automatic type conversions that PHP performs

3. Type casting

Changing the type of a variable from one kind of data to another

29

Page 30: Topic 2 : PHP

Practice Questions• WAP to generate multiplication table of <your_roll_no>.

• Generate a form to get user input, submit to find whether it is prime or not.

• Generate a form to get user input, submit to find the fibonacci series.

• Generate a form to get user input, submit to find the factorial.

30

Page 31: Topic 2 : PHP

Assignment #3• WAP to change background color of html according to day.

[Hint : research about date( ) function]

• Demonstrate different format of PHP Date.

• Demonstrate function with default value.

• Demonstrate a program of local & global variable.

• Demonstrate different PHP Array functions

array_combine( ) array_key_exists( ) array_merge( )

array_rand( ) array_reverse( ) array_unique( )

count( ) in_array( ) shuffle( ) sort( )

31

Last Date to Submit : 20th Sept

Send to [email protected]

Page 32: Topic 2 : PHP

Questions???

32

Page 33: Topic 2 : PHP

End of Topic - 2

33