mini – workshop on php - 27.04.2013 -

36
Mini – Workshop on PHP - 27.04.2013 - Faculty of Engineering in Foreign Languages 1

Upload: lance-miranda

Post on 03-Jan-2016

37 views

Category:

Documents


0 download

DESCRIPTION

Mini – Workshop on PHP - 27.04.2013 -. Faculty of Engineering in Foreign Languages. Welcome!. I am Victor Voicu , an older colleague of yours; currently studying Computer Science at FILS, in the 4 th year and teaching PHP and today I will present you some features of PHP. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Mini – Workshop on PHP - 27.04.2013 -

Mini – Workshop on PHP

- 27.04.2013 -

Faculty of Engineering in Foreign Languages

1

Page 2: Mini – Workshop on PHP - 27.04.2013 -

Welcome!

• I am Victor Voicu, an older colleague of

yours; currently studying Computer

Science at FILS, in the 4th year and

teaching PHP and today I will present

you some features of PHP.

• Please feel free to ask any questions.2

Page 3: Mini – Workshop on PHP - 27.04.2013 -

What will you learn about PHP?

• Variables

• Arrays

• Control structures

• Functions

• Working with databases

3

Page 4: Mini – Workshop on PHP - 27.04.2013 -

What is PHP?

• A widely – used open source general

purpose scripting language;

• Hypertext Preprocessor;

• It works hand in hand with HTML;

• It a powerful tool in creating websites.

4

Page 5: Mini – Workshop on PHP - 27.04.2013 -

Why PHP?

• Because it is simple

• Because it is easy to use

• Because you do not need many things to

create a site in PHP… only logic

5

Page 6: Mini – Workshop on PHP - 27.04.2013 -

Server side or client side

• PHP runs on a server, not on the client side

(your own computer);

• The webpages displayed to the client are

dynamic (in contrast with pure HTML);

• On the server side, PHP code may perform

different operations: queries on a database,

storage of data submitted by users; etc. 6

Page 7: Mini – Workshop on PHP - 27.04.2013 -

PHP structure

• The content of a PHP code is enclosed in

the following tags: <?php php_code ?>

• Anything outside these tags will be

interpreted as pure HTML;

• File extension is .php

7

Page 8: Mini – Workshop on PHP - 27.04.2013 -

Simple example

<html>

<head>

<title>PHP Test</title>

</head>

<body>

<?php echo '<p>Hello, folks!</p>'; ?>

</body>

</html> 8

Page 9: Mini – Workshop on PHP - 27.04.2013 -

Simple example

<html>

<head>

<title>PHP Test</title>

</head>

<body>

<?php echo '<p>Hello, folks!</p>'; ?>

</body>

</html> 9

Page 10: Mini – Workshop on PHP - 27.04.2013 -

PHP Syntax

• Similar with Java/C/C++;

• It has loose type property like Python;

(you do not need to specify a variable

type)

• Ends instructions with ;

• It is case sensitive.10

Page 11: Mini – Workshop on PHP - 27.04.2013 -

Variables

11

• Declaration: $var• Assigning a value: $var=1;• In php a variable can take any value

(loose type): $var=1;$var=3.54;$var=true;$var=“Hello”• The last assigned value is the one that

remains• Calling a variable is done by writing its

name: echo $var;

Page 12: Mini – Workshop on PHP - 27.04.2013 -

Arrays

12

• Declaration: $var=array();• Assigning a value (can be done by

using with or without using a key ):– Without: $var[]=1;//automatically the

key is 0– With: $var[‘hello’]=“Hello”;

• Calling an element of an array is done by using its key (or its index:– echo $var[‘hello’]; echo $var[0];

Page 13: Mini – Workshop on PHP - 27.04.2013 -

Arrays

13

• Adding elements to array:$fruits[]=“pear”;array_push($fruits, “apple”);

• Retrieve element:echo $fruits[1];

Page 14: Mini – Workshop on PHP - 27.04.2013 -

Global arrays

14

• $_GET[] – used to take information sent with get

method (links, forms) , data is visible.

• $_POST[] – used to take information sent with post

method (usually from forms), data cannot be seen

(useful for login, registration, etc);

• $_FILE[] – used for file upload

• $_SESSION[] – used for storing info related to user’s

session

• $_SERVER[]- used to take info from server and client.

Page 15: Mini – Workshop on PHP - 27.04.2013 -

Decisional Control Structures

15

• If – else

if($a > $b):

echo $a." is greater than ".$b;

else

echo $b." is greater than ".$a;

Page 16: Mini – Workshop on PHP - 27.04.2013 -

Decisional Control Structures

16

• Switch – Case:

switch ($number) {

case 0:

echo “number equals 0";

break;

case 1:

echo “number equals 1";

break;

default:

echo “number is unknown";

break;

}

Page 17: Mini – Workshop on PHP - 27.04.2013 -

Repetitive Control Structures

17

• for

for ($no=1; $no<=5; $no++){

echo "The number has the value " . $no . "<br>";

}

• foreach

$x=array("one","two","three");

foreach ($x as $value){

echo $value . "<br>";

}

Page 18: Mini – Workshop on PHP - 27.04.2013 -

Repetitive Control Structures

18

• while

while($i<=5){

echo "The number is " . $i . "<br>";

$i++;

}

• do-while

$i=1;

do{

$i++;

echo "The number is " . $i . "<br>";

}

while ($i<=5);

?>

Page 19: Mini – Workshop on PHP - 27.04.2013 -

Functions

19

• Group your code so it can be reusable for

other variables (values);

• Can have or not a return value;

• To return a value, use the return

statement.

Page 20: Mini – Workshop on PHP - 27.04.2013 -

Examples

20

• <?phpfunction divide($x,$y){

$result=$x/$y;return $result;

}echo “32/ 16 = " . divide(32,16);

?>• <?php

function concat($string1,$string2){return $string1.string2;

}echo concat(“Are you still”,” awake?”);

?>

Page 21: Mini – Workshop on PHP - 27.04.2013 -

Some useful functions

21

• echo – print in the webpage• isset()- checks if a variable is set (it has been

given a value)– isset($var)

• var_dump() – prints the value and the type of a variable

• in_array() – searches if the value exists in an array

in_array(‘”pear”,$fruits)• require_once/include_once – includes a file

in the current page only once

Page 22: Mini – Workshop on PHP - 27.04.2013 -

Some useful functions

22

• explode($delimiter, $string) - breaks up $string into an array of substrings, separating using $delimiter

• count()

Page 23: Mini – Workshop on PHP - 27.04.2013 -

Classes

23

• Are the basic structure in Object Oriented Programming;

• Start with the class keyword;• :: Scoping resolution operator – used to

access a super class and call it's functions;

• → operator used to access functions/fields of classes

• Use new keyword for new instances of a class;

Page 24: Mini – Workshop on PHP - 27.04.2013 -

Classes - Example

24

• <?phpclass SimpleClass{ // property declaration public $var = 'a default value'; // method declaration public function displayVar() { echo $this->var; }}

$var=new SimpleClass(); $var->displayVar();?>

Page 25: Mini – Workshop on PHP - 27.04.2013 -

Cookies

25

• Used to store information about the

user;

• Commonly use form:

bool setcookie($name, $value, $expire)

• Should be set at the beginning of your

PHP file.

• Set on the client’s computer.

Page 26: Mini – Workshop on PHP - 27.04.2013 -

Cookies - Example

26

<?php

$expire=time()+60*60*24*30;

setcookie("user", “John Smith",

$expire);

?>

Page 27: Mini – Workshop on PHP - 27.04.2013 -

$_SESSION – more details

27

• Stores information about the user’s

session;

• More trustworthy than cookies

(controlled at the server side)

• Data stored in this array can be

retrieved in different pages

Page 28: Mini – Workshop on PHP - 27.04.2013 -

$_SESSION – more details

28

• Usually used for information about the

user, shopping carts:

<?php

session_start();

$_SESSION[‘cart’][‘[product’]=“pen”;

session_destroy();

?>

Page 29: Mini – Workshop on PHP - 27.04.2013 -

Databases – MySQL(i)

29

• MySQL is a free database commonly used on

websites to store information

• PHP supports accessing MySQL databases

• You can store information about users,

preferences in MySQL databases

• Information can be retrieved and displayed

in the webpage.

Page 30: Mini – Workshop on PHP - 27.04.2013 -

Databases – MySQL(i)

30

• In order to work with a database, first

you must connect to the server

containing the database.

• Data from the database is stored in

tables.

Page 31: Mini – Workshop on PHP - 27.04.2013 -

Steps in working with the database

31

1. Connect to the MySQL database

2. Prepare your 'query' (the question you're askin

g the database)

3. Actually execute your query

4. Process the results (answer from the database)

(Repeat steps 2 to 4 as necessary)

5. Close your connection to the database

Page 32: Mini – Workshop on PHP - 27.04.2013 -

Database functions

32

• mysqli_connect() – establishes the connection to the database.

• mysqli_query() – executes a query on one or several tables and returns a result.

• mysqli_fetch_array() – returns a row from the result set.

Page 33: Mini – Workshop on PHP - 27.04.2013 -

Database example

33

$connection=mysqli_connect(“localhost”,”root”,””,”db_example”)$query=“SELECT * FROM users”;$resultSet=mysqli_query($query,$connection);$users=array();while($row=mysqli_fetch_assoc($resultSet))

$users[]=$row;

Page 34: Mini – Workshop on PHP - 27.04.2013 -

PHP Successful Examples

34

Websites:• www.amazon.com• www.facebook.com• www.wikipedia.comFrameworks: Zend Symfony Yii

Page 35: Mini – Workshop on PHP - 27.04.2013 -

Questions

35

Page 36: Mini – Workshop on PHP - 27.04.2013 -

Thanks you for your attention!

Now the real fun begins!

36