1 / 36

Mini – Workshop on PHP - 27.04.2013 -

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.

Download Presentation

Mini – Workshop on PHP - 27.04.2013 -

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

  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.

  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

  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.

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

  8. Simple example <html> <head> <title>PHP Test</title> </head> <body> <?php echo '<p>Hello, folks!</p>'; ?>  </body></html>

  9. Simple example <html> <head> <title>PHP Test</title> </head> <body> <?php echo '<p>Hello, folks!</p>'; ?>  </body></html>

  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.

  11. Variables • 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 • 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 • Adding elements to array: $fruits[]=“pear”; array_push($fruits, “apple”); • Retrieve element: echo $fruits[1];

  14. Global arrays • $_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 • If – else if($a > $b): echo $a." is greater than ".$b; else echo $b." is greater than ".$a;

  16. Decisional Control Structures • 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 • 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>";}

  18. Repetitive Control Structures • 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);?>

  19. Functions • 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 • <?php function 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?”);?>

  21. Some useful functions • 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 • explode($delimiter, $string) - breaks up $string into an array of substrings, separating using $delimiter • count()

  23. Classes • 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 • <?phpclass SimpleClass{    // property declaration    public $var = 'a default value';    // method declaration    public function displayVar() {        echo $this->var;}} $var=new SimpleClass(); $var->displayVar();?>

  25. Cookies • Used to store information about the user; • Commonly use form: boolsetcookie($name, $value, $expire) • Should be set at the beginning of your PHP file. • Set on the client’s computer.

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

  27. $_SESSION – more details • 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 • Usually used for information about the user, shopping carts: <?php session_start(); $_SESSION[‘cart’][‘[product’]=“pen”; session_destroy(); ?>

  29. Databases – MySQL(i) • 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) • 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 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 • 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 $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 Websites: • www.amazon.com • www.facebook.com • www.wikipedia.com Frameworks: • Zend • Symfony • Yii

  35. Questions

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

More Related