Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mini – Workshop on PHP Faculty of Engineering in Foreign Languages 1.

Similar presentations


Presentation on theme: "Mini – Workshop on PHP Faculty of Engineering in Foreign Languages 1."— Presentation transcript:

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

2 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. Please feel free to ask any questions. 2

3 What will you learn about PHP? Variables Arrays Control structures Functions Working with databases 3

4 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

5 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

6 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

7 PHP structure The content of a PHP code is enclosed in the following tags: Anything outside these tags will be interpreted as pure HTML; File extension is.php 7

8 Simple example PHP Test Hello, folks! '; ?> 8

9 Simple example PHP Test Hello, folks! '; ?> 9

10 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

11 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;

12 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];

13 Arrays 13 Adding elements to array: $fruits[]=“pear”; array_push($fruits, “apple”); Retrieve element: echo $fruits[1];

14 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.

15 Decisional Control Structures 15 If – else if($a > $b): echo $a." is greater than ".$b; else echo $b." is greater than ".$a;

16 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; }

17 Repetitive Control Structures 17 for for ($no=1; $no "; } foreach $x=array("one","two","three"); foreach ($x as $value){ echo $value. " "; }

18 Repetitive Control Structures 18 while while($i "; $i++; } do-while $i=1; do{ $i++; echo "The number is ". $i. " "; } while ($i

19 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.

20 Examples 20

21 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

22 Some useful functions 22 explode($delimiter, $string) - breaks up $string into an array of subs trings, separating using $delimiter count()

23 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;

24 Classes - Example 24 var; } } $var=new SimpleClass(); $var->displayVar(); ?>

25 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.

26 Cookies - Example 26 <?php $expire=time()+60*60*24*30; setcookie("user", “John Smith", $expire); ?>

27 $_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

28 $_SESSION – more details 28 Usually used for information about the user, shopping carts: <?php session_start(); $_SESSION[‘cart’][‘[product’]=“pen”; session_destroy(); ?>

29 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.

30 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.

31 Steps in working with the database 31 1. Connect to the MySQL database 2. Prepare your 'query' (the question you're asking 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

32 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.

33 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;

34 PHP Successful Examples 34 Websites: www.amazon.com www.facebook.com www.wikipedia.com Frameworks:  Zend  Symfony  Yii

35 Questions 35

36 Thanks you for your attention! Now the real fun begins! 36


Download ppt "Mini – Workshop on PHP Faculty of Engineering in Foreign Languages 1."

Similar presentations


Ads by Google