what is php

64
1 What is PHP? • PHP stands for PHP: Hypertext Preprocessor • PHP is a server-side scripting languagePHP scripts are executed on the server. • PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.) • PHP is an open source software (OSS)

Upload: avc

Post on 20-Jan-2015

5.306 views

Category:

Technology


0 download

DESCRIPTION

Introduction of PHP andmysql

TRANSCRIPT

Page 1: What Is Php

1

What is PHP?

• PHP stands for PHP: Hypertext Preprocessor

• PHP is a server-side scripting languagePHP scripts are executed on the server.

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

• PHP is an open source software (OSS)

Page 2: What Is Php

2

Why 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

Page 3: What Is Php

3

Where to Start?

• Install an Apache server on a Windows or Linux machine

• Install PHP on a Windows or Linux machine

• Install MySQL on a Windows or Linux machine

Page 4: What Is Php

4

• Download PHP for free here: http://www.php.net/downloads.php

• Download MySQL for free here: http://www.mysql.com/downloads/index.html

• Download Apache for free here: http://httpd.apache.org/download.cgi

Page 5: What Is Php

5

Basic PHP Syntax

• A PHP scripting block always– starts with <?php and – ends with ?>

• Example<?php

…….….

………..

?>

Page 6: What Is Php

6

What is a PHP File?

• A PHP file normally contains

– HTML tags, just like an HTML file

– some PHP scripting code

Page 7: What Is Php

7

Combining HTML and PHP<html> <head> <title>A PHP script including HTML</title></head><body> <b> "hello world- HTML";<?php echo "hello world-PHP";?> </b> </body> </html>

Page 8: What Is Php

8

Comments in PHP

• In PHP, we use

// or # to make a single-line comment

/* and */ to make a large comment block.

Page 9: What Is Php

9

Example

<html>

<body>

<?php

//This is a single-line comment

/*

This is

a comment

block

*/

?>

</body>

</html>

Page 10: What Is Php

10

Variables in PHP

• Variables are used for storing a values, like text strings, numbers or arrays.

• When a variable is set it can be used over and over again in your script

• All variables in PHP start with a $ sign symbol.

• The correct way of setting a variable in PHP:

$var_name = value;

Eg: $name=“Sunil”;

Page 11: What Is Php

11

Example

<?php

$txt = "Hello World!";

$number = 16;

echo $txt;

echo $number;

?>

Page 12: What Is Php

12

Variable Naming Rules

• A variable name must start with a letter or an underscore "_"

• A variable name can only contain alpha-numeric characters and underscores (a-Z, 0-9, and _ )

• A variable name should not contain spaces. If a variable name is more than one word, it should be separated with underscore ($my_string), or with capitalization ($myString)

Page 13: What Is Php

13

PHP String

• String variables are used for values that contains character strings.

• Example 1:<?php$txt="Hello World";echo $txt;?>

The output of the code above will be:Hello World

Page 14: What Is Php

14

PHP String

• Example 2:<?php

$txt1="Hello World";

$txt2="1234";

echo $txt1 . " " . $txt2;

?>

The output of the code above will be:

Hello World 1234

Page 15: What Is Php

15

PHP String

• Example 2:<?php

echo strlen("Hello world!");

?>

The output of the code above will be:

12

Page 16: What Is Php

16

PHP Operators

• Arithmetic Operators+ Addition

- Subtraction

* Multiplication

/ Division

% Modulus (division remainder)

++ Increment

--Decrement

Page 17: What Is Php

17

PHP Operators

• Comparison Operators

== is equal to 5==8 returns false

!= is not equal5!=8 returns true

> is greater than 5>8 returns false

< is less than 5<8 returns true

>= is greater than or equal to 5>=8 returns false

<= is less than or equal to 5<=8 returns true

Page 18: What Is Php

18

Conditional Statements

If...Else Statement• If you want to execute some code if a

condition is true and another code if a condition is false, use the if....else statement.

• Syntax if (condition) code to be executed if condition is true;else code to be executed if condition is false;

Page 19: What Is Php

19

Conditional StatementsExample 1:

<html>

<body>

<?php

$d=date("D");

if ($d=="Fri")

echo "Have a nice weekend!";

else

echo "Have a nice day!";

?>

</body>

</html>

Page 20: What Is Php

20

Conditional StatementsExample 2:

<html><body><?php$d=date("D");if ($d=="Fri") { echo "Hello!<br />"; echo "Have a nice weekend!"; echo "See you on Monday!"; }?></body></html>

Page 21: What Is Php

21

Conditional Statements• If you want to execute some code if one of

several conditions are true use the elseif statement

• Syntax if (condition)

code to be executed if condition is true;

elseif (condition)

code to be executed if condition is true;

else

code to be executed if condition is false;

Page 22: What Is Php

22

Conditional StatementsExample 3:

<html><body><?php$d=date("D");if ($d=="Fri") echo "Have a nice weekend!"; elseif ($d=="Sun") echo "Have a nice Sunday!"; else echo "Have a nice day!"; ?></body></html>

Page 23: What Is Php

23

PHP Switch Statement• If you want to select one of many blocks of

code to be executed, use the Switch statement.

• The switch statement is used to avoid long blocks of if..elseif..else code.

Page 24: What Is Php

24

PHP Switch Statement• Syntax

switch (expression)

{

case label1:

code to be executed if expression = label1;

break;

case label2:

code to be executed if expression = label2;

break;

default:

code to be executed

if expression is different from both label1 and label2;

}

Page 25: What Is Php

25

PHP Switch StatementExample:

<html><body><?php$x=2;switch ($x){case 1: echo "Number 1"; break;case 2: echo "Number 2"; break;case 3: echo "Number 3"; break;default: echo "No number between 1 and 3";}?></body></html>

Page 26: What Is Php

26

Exercise :

• Assign values to two variables. Use comparison operators to test whether the first value is

– The same as the second– Less than the second– Greater than the second– Less than or equal to the second

• Print the result of each test to the browser.• Change the values assigned to your test

variables and run the script again.

Page 27: What Is Php

27

PHP Arrays

• What is an array?– When working with PHP, sooner or later, you

might want to create many similar variables.– Instead of having many similar variables, you

can store the data as elements in an array.– Each element in the array has its own ID so

that it can be easily accessed.

Page 28: What Is Php

28

PHP Arrays : Example 1<?php

$names[0] = "Peter";

$names[1] = "Quagmire";

$names[2] = "Joe";

echo $names[1] . " and " . $names[2] .

" are ". $names[0] . "'s neighbors";

?>

The code above will output:

Quagmire and Joe are Peter's neighbors

Page 29: What Is Php

29

PHP Arrays : Example 2<?php

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

echo $names[1] . " and " . $names[2] .

" are ". $names[0] . "'s neighbors";

?>

The code above will output:

Quagmire and Joe are Peter's neighbors

Page 30: What Is Php

30

Multidimensional Arrays

• In a multidimensional array, – each element in the main array can also be

an array. – each element in the sub-array can be an

array, and so on

Page 31: What Is Php

31

Multidimensional Arrays

• In a multidimensional array, – each element in the main array can also be

an array. – each element in the sub-array can be an

array, and so on

Page 32: What Is Php

32

PHP Looping

• Looping statements in PHP are used to execute the same block of code a specified number of times.

Page 33: What Is Php

33

PHP Looping

• Looping statements in PHP are used to execute the same block of code a specified number of times.

• In PHP we have the following looping statements:– while - loops – do...while - loops– for - loops – foreach - loops

Page 34: What Is Php

34

The while Statement

• The while statement will execute a block of code if and as long as a condition is true.

• Syntaxwhile (condition)

{statement 1;statement 2;

}

Page 35: What Is Php

35

The while StatementExample

... $i=1;while($i<=5) { echo "The number is " . $i . "<br />"; $i++; }...

Page 36: What Is Php

36

do...while Statement

• The do...while statement will execute a block of code at least once - it then will repeat the loop as long as a condition is true.

• Syntaxdo

{

code to be executed;

}

while (condition);

Page 37: What Is Php

37

do...while Statement

• Example..$i=0;do { $i++; echo "The number is " . $i . "<br />"; }while ($i<5);..

Page 38: What Is Php

38

The for Statement

• The for statement is used when you know how many times you want to execute a statement or a list of statements.

• Syntaxfor (initialization; condition; increment)

{

code to be executed;

}

Page 39: What Is Php

39

The for Statement

• Example.

.

for ($i=1; $i<=5; $i++)

{

echo "Hello World!<br />";

}

.

.

Page 40: What Is Php

40

The foreach Statement

• The foreach statement is used to loop through arrays.

• For every loop, the value of the current array element is assigned to $value (and the array pointer is moved by one) - so on the next loop, you'll be looking at the next element.

• Syntaxforeach (array as value){ code to be executed;}

Page 41: What Is Php

41

The foreach Statement

• Example

The following example demonstrates a loop that will print the values of the given array:

<html><body><?php$arr=array("one", "two", "three");foreach ($arr as $value){ echo "Value: " . $value . "<br />";}?></body></html>

Page 42: What Is Php

42

PHP Functions

• A function is a block of code that can be executed whenever we need it.

• Creating PHP functions:– All functions start with the word "function()"– Name the function - It should be possible to understand what the

function does by its name. The name can start with a letter or underscore (not a number)

– Add a "{" - The function code starts after the opening curly brace

– Insert the function code– Add a "}" - The function is finished by a closing curly brace

Page 43: What Is Php

43

Example 1

A simple function that writes my name when it is called:

<html><body><?phpfunction writeMyName() { echo “Nimal"; }writeMyName();?></body></html>

Page 44: What Is Php

44

Example 2 <html><body><?phpfunction writeMyName() { echo "Kai Jim Refsnes"; }echo "Hello world!<br />";echo "My name is ";writeMyName();echo ".<br />That's right, ";writeMyName();echo " is my name.";?></body></html>

Page 45: What Is Php

45

Example 3 • The following example will write different first names,

<html><body><?phpfunction writeMyName($fname) { echo $fname . " .<br />"; }echo "My name is ";writeMyName(“Nimal");echo "My name is ";writeMyName(“Kamal");echo "My name is ";writeMyName(“Amal");?></body></html>

Page 46: What Is Php

46

Example 4 • The following function has two parameters

<html><body><?phpfunction writeMyName($fname,$lname) { echo $fname . " Refsnes" . $lname . "<br />"; }echo "My name is ";writeMyName(“Nimal",“Amarasinghe");echo "My name is ";writeMyName(“Kamal",“Perera");echo "My name is ";writeMyName(“Amal",“Silva");?></body></html>

Page 47: What Is Php

47

PHP Functions - Return values

• Functions can also be used to return values.• Example

<html><body><?phpfunction add($x,$y) { $total = $x + $y; return $total; }echo "1 + 16 = " . add(1,16);?></body></html>

Page 48: What Is Php

48

PHP Forms

• The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input.

Page 49: What Is Php

49

Example• Welcome.htm

<html><body><form action="welcome.php" method="post">Name: <input type="text" name="name" />Age: <input type="text" name="age" /><input type="submit" /></form></body></html>

Page 50: What Is Php

50

Example• welcome.php

<html>

<body>

Welcome <?php echo $_POST["name"]; ?>.<br />

You are <?php echo $_POST["age"]; ?> years old.

</body>

</html>

Page 51: What Is Php

51

PHP $_GET

• The $_GET variable is used to collect values from a form with method="get".

• Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser's address bar)

• It has limits on the amount of information to send (max. 100 characters).

Page 52: What Is Php

52

Example<form action="welcome.php" method="get">

Name: <input type="text" name="name" />

Age: <input type="text" name="age" />

<input type="submit" />

</form>

• Welcome.php

Welcome <?php echo $_GET["name"]; ?>.<br />

You are <?php echo $_GET["age"]; ?> years old!

Page 53: What Is Php

53

Note:

• When using the $_GET variable all variable names and values are displayed in the URL.

• So this method should not be used when sending passwords or other sensitive information!

• The HTTP GET method is not suitable on large variable values; the value cannot exceed 100 characters.

Page 54: What Is Php

54

PHP $_POST

• The $_POST variable is used to collect values from a form with method="post".

• Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.

Page 55: What Is Php

55

Example

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

Enter your name: <input type="text" name="name" />

Enter your age: <input type="text" name="age" />

<input type="submit" />

</form>

• Welcome.php

Welcome <?php echo $_POST["name"]; ?>.<br />

You are <?php echo $_POST["age"]; ?> years old!

Page 56: What Is Php

56

PHP Database

Page 57: What Is Php

57

Connecting to a MySQL Database

• Before you can access and work with data in a database, you must create a connection to the database.

• In PHP, this is done with the mysql_connect() function.• Syntax

mysql_connect(servername,username,password);

– servername Optional. Specifies the server to connect to. Default value is "localhost:3306"

– username Optional. Specifies the username to log in with. Default value is the name of the user that

owns the server process– password Optional. Specifies the password to log in with.

Default is ""

Page 58: What Is Php

58

Example

• In the following example we store the connection in a variable ($con). The "die" part will be executed if the connection fails:

<?php$con = mysql_connect("localhost","peter","abc123");if (!$con) { die('Could not connect: ' . mysql_error()); }// some codemysql_close($con);?>

Page 59: What Is Php

59

Create MySQLDatabase and Tables

• To get PHP to execute a MySQL statement we must use the mysql_query() function.

• This function is used to send a query or command to a MySQL connection.

Page 60: What Is Php

60

Example• In the following example we create a database called "my_db":

<?php$con = mysql_connect("localhost","peter","abc123");if (!$con) { die('Could not connect: ' . mysql_error()); }if (mysql_query("CREATE DATABASE my_db",$con)) { echo "Database created"; }else { echo "Error creating database: " . mysql_error(); }mysql_close($con);?>

Page 61: What Is Php

61

Create a Table• Example

$con = mysql_connect("localhost","peter","abc123");if (!$con) { die('Could not connect: ' . mysql_error()); }// Create table in my_db databasemysql_select_db("my_db", $con);$sql = "CREATE TABLE person (FirstName varchar(15),LastName varchar(15),Age int)";mysql_query($sql,$con);mysql_close($con);

Page 62: What Is Php

62

Insert new records into a database table• Example

<?php$con = mysql_connect("localhost","peter","abc123");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("my_db", $con);mysql_query("INSERT INTO person (FirstName, LastName, Age) VALUES ('Peter', 'Griffin', '35')");mysql_query("INSERT INTO person (FirstName, LastName, Age) VALUES ('Glenn', 'Quagmire', '33')");mysql_close($con);?>

Page 63: What Is Php

63

Insert Data From a Form Into a Database

• Example : insert.html<html><body><form action="insert.php" method="post">Firstname: <input type="text" name="firstname" />Lastname: <input type="text" name="lastname" />Age: <input type="text" name="age" /><input type="submit" /></form></body></html>

Page 64: What Is Php

64

Insert Data From a Form Into a Database• Example : insert.php

<?php$con = mysql_connect("localhost","peter","abc123");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("my_db", $con);$sql="INSERT INTO person (FirstName, LastName, Age)VALUES('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }echo "1 record added";mysql_close($con)?>