Mini – Workshop on PHP - Maria

Download Report

Transcript Mini – Workshop on PHP - Maria

Faculty of Engineering in Foreign Languages
Mini – Workshop on PHP
- 27.04.2013 -
1
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
What will you learn about PHP?
• Variables
• Arrays
• Control structures
• Functions
• Working with databases
3
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
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
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
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
Simple example
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo '<p>Hello, folks!</p>'; ?>
</body>
</html>
8
Simple example
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo '<p>Hello, folks!</p>'; ?>
</body>
</html>
9
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
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;
11
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];
12
Arrays
• Adding elements to array:
$fruits[]=“pear”;
array_push($fruits, “apple”);
• Retrieve element:
echo $fruits[1];
13
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.
14
Decisional Control Structures
• If – else
if($a > $b):
echo $a." is greater than ".$b;
else
echo $b." is greater than ".$a;
15
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;
}
16
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>";
}
17
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);
?>
18
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.
19
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?”);
?>
20
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
21
Some useful functions
• explode($delimiter, $string) breaks up $string into an array of subs
trings, separating using $delimiter
• count()
22
Classes
• Are the basic structure in Object
Oriented Programming;
• Start with the class keyword;
• :: Scoping resolution operator –
used to access a superclass and call it's functions;
• → operator used to access functions/
fields of classes
• Use new keyword for new instances
of a class;
23
Classes - Example
• <?php
class SimpleClass{
// property declaration
public $var = 'a default value';
// method declaration
public function displayVar() {
echo $this->var;
}
}
$var=new SimpleClass();
$var->displayVar();
?>
24
Cookies
• 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.
25
Cookies - Example
<?php
$expire=time()+60*60*24*30;
setcookie("user", “John Smith",
$expire);
?>
26
$_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
27
$_SESSION – more details
• Usually used for information about the
user, shopping carts:
<?php
session_start();
$_SESSION[‘cart’][‘[product’]=“pen”;
session_destroy();
?>
28
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.
29
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.
30
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
31
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.
32
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;
33
PHP Successful Examples
Websites:
• www.amazon.com
• www.facebook.com
• www.wikipedia.com
Frameworks:
 Zend
 Symfony
 Yii
34
Questions
35
Thanks you for your attention!
Now the real fun begins!
36