Mini – Workshop on PHP - 27.04.2013 -
-
Upload
lance-miranda -
Category
Documents
-
view
37 -
download
0
description
Transcript of Mini – Workshop on PHP - 27.04.2013 -
Mini – Workshop on PHP
- 27.04.2013 -
Faculty of Engineering in Foreign Languages
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
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;
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];
Arrays
13
• Adding elements to array:$fruits[]=“pear”;array_push($fruits, “apple”);
• Retrieve element:echo $fruits[1];
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.
Decisional Control Structures
15
• If – else
if($a > $b):
echo $a." is greater than ".$b;
else
echo $b." is greater than ".$a;
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;
}
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>";
}
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);
?>
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.
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?”);
?>
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
Some useful functions
22
• explode($delimiter, $string) - breaks up $string into an array of substrings, separating using $delimiter
• count()
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;
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();?>
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.
Cookies - Example
26
<?php
$expire=time()+60*60*24*30;
setcookie("user", “John Smith",
$expire);
?>
$_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
$_SESSION – more details
28
• Usually used for information about the
user, shopping carts:
<?php
session_start();
$_SESSION[‘cart’][‘[product’]=“pen”;
session_destroy();
?>
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.
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.
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
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.
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;
PHP Successful Examples
34
Websites:• www.amazon.com• www.facebook.com• www.wikipedia.comFrameworks: Zend Symfony Yii
Questions
35
Thanks you for your attention!
Now the real fun begins!
36