Você está na página 1de 36

PHP + MySQL + XML

Alexander O’Connor
<Alex.Oconnor@scss.tcd.ie>
•  Brief introduction to PHP
•  Variables
• 
• 
Flow Control
Basic Forms
1
•  Arrays
•  Functions & imports

2
•  Connecting to Databases
•  Sessions
•  Security
•  SimpleXML
•  File I/O 3
Background
LAMP
Linux (or Other OS)
Apache Mysql PHP
Basic Syntax
<!doctype html>
<head>
</head>
<body>

<?php
echo ‘Hello World!’;
?>

</body>
</html>
<!doctype html>
<head>
</head>
<body>

<?php
echo ‘<p>Hello World!</p>’;
?>

</body>
</html>
<!doctype html>
<head>
</head>
<body>
<?php
//This is a comment
echo ‘<p>Hello World!</p>’;
/* so is this */
?>
</body>
</html>
$ indicates a variable

$name is a variable called name

<?php = assigns the value ‘Sam’ to $name

$name = ‘Sam’;
We can use a variable in place of a
echo $name; value such as a string.

In this case, we print out ‘Sam’


?>
PHP variables have implicit type

Types include:

<?php •  Booleans
•  Integers

$name = 99; • 
• 
Floating point numbers
Strings
•  Arrays
echo $name; • 
• 
Objects
Resources
•  NULL
?>
PHP will try to ‘juggle’ types to get the
right one in context.
Operators
$a + $b Add the values of $a and $b
$a * $b Multiply the values of $a and $b
$a - $b Subtract the value of $b from $a
$a / $b Divide the value of $a by $b
$a % $b Divide $a by $b and return the remainder
<?php $a = $b
(modulo)
Assign the value of $b to $a

$number = 99;
$double_number = $number + 2;
?>
<?php
$name = ‘Jim’;
echo ‘Hello,’.$name;
?> . Is the concatenation operator, it adds
strings together.

Could also write echo “Hello, $name”


with double quotes (interpolation).
Flow Control
Operator Meaning
$a == $b Equality (note double =)
$a != $b Inequality (NOT equal)
$a < $b Less than
<?php $a >= $b Greater than or Equal to
$a = ‘5’; $a === $b Equal and Same type
$a !== $b Not equal / different type
$b = 3;
if($a > $b){
print ‘a is bigger’;
}
?>
<?php
$a = ‘5’;
$b = 3;
$c = -1;
if($a == $b){
print ‘will not print’;
} else if ($c < $b){
print ‘will print’;
} else{
print ‘if none of the others are
true’;
}
?>
<?php

while($number <= 5){
print $number . ‘<br />’;
$number += 1;
}
?>
<?php
switch ($name){
case ‘adam’:
print ‘hello adam’;
break;
case ‘eve’ :
print ‘hello eve’;
break;
default:
print ‘hello unknown’;
break;
}
?>
html forms
html Data from form PHP
form script

User
<html>
<body>
<form action="handler.php" method="post">
<input type="text" name=”username" id=”username" />
<label for=”username">Name</label><br />
<input type="checkbox" name="check" id="check" />
<label for="check">Check?</label><br />
<input type="submit" value="Say Hi" />
</form>
</body>
</html>
html5 Only!
<html>
<body>
<form action="handler.php" method="post">
<input type="email" name="mail" id="mail"
placeholder=”first.last@example.com" />
<label for="mail">email address</label><br />
<input type="number” min="0" max="20" step="2”
name="num" />
<input type="submit" value="HTML5!" />
</form>
</body>
</html>
html forms
text Plain text
password Obfuscated text (****)
textarea Editable text box
checkbox On/off box
radio Radio Buttons (pick one of a list)
select Drop-down list
file File upload
hidden Add hidden data

html5 (partial)
autofocus Highlight this first
required Browser checks for value
email Browser checks for email
number Browser checks for number
url Browser checks for web address
<html>
<body>
<?php
if($_GET[‘username’]){
print ‘<p>Hello, ’.$_GET[‘username’].’</p>’;
}else{
print ‘No name specified’;
}
?>
</body>
</html>
<html>
<body>
<?php
if($_POST[‘username’]){
print ‘<p>Hello, ’.$_POST[‘username’].’</p>’;
}else{
print ‘No name specified’;
}
?>
</body>
</html>
Arrays
$name Alex

username check

$_GET Alex true


<?php
$array = array(); A B

$array[‘A’] = 1;
$array 1 2
$array[‘B’] = 2;

?>


<?php
0 1
$array = array();

array_push($array,‘zero’); $array 1 2
$array[] = 2;
?>


1 2 3
<?php
$myarray = array( a b c

‘1’ => ‘a’,


‘2’ => ‘b’,
‘3’ => ‘c’);
foreach($myarray as $key => $value){
print $key.’,’.$value;
}
?>
<?php
$value = array_pop($myarray);
?>

array_push Push a new value onto the end of


the array
array_pop Pop the last value off the array
natsort Sort the array by values, in a
‘natural order’
shuffle Randomize the array
array_unique Remove duplicates
reverse Reverse the order of the array
print_r($array) Pretty-print the array
Summary
PHP Variables Flow control

• Inline with html • Store values • if () { }


• <?php ?> • $variable_name • elseif {}
•  statement ; • $x = 9; • else {}
•  // comment • Type Juggling • while () {}
• /* comment */ •  “$interpolation ” • switch (value) {
•  ‘con’.‘cat’.‘enate’ • case val :
•  break;
• }

Forms Arrays

• <input> • $array[‘key’]
• text • array functions
• password • sort
• html5 input • reverse
• number • shuffle
• email • array_push
• <submit> • array_pop
• $_GET[] or $_POST[] • foreach ($array as $key => $val)
•  Brief introduction to PHP
•  Variables
• 
• 
Flow Control
Basic Forms
1
•  Arrays
•  Functions & imports

2
•  Connecting to Databases
•  Sessions
•  Security
•  SimpleXML
•  File I/O 3
Picture Credits
•  Wheels of Industry no .1
http://www.sxc.hu/photo/447579
•  Boxes and Bike http://www.sxc.hu/photo/78885
•  Keyboard Spy http://www.sxc.hu/photo/754170
•  Rasmus Lerdorf
http://en.wikipedia.org/wiki/Rasmus_Lerdorf
•  Sleep tight http://www.sxc.hu/photo/682369
•  Form http://www.sxc.hu/photo/478224
•  Rusted Chain http://www.sxc.hu/photo/319183
Useful Links
•  Language Manual
–  http://www.php.net/manual/en/
•  PHP Tutorials
–  http://devzone.zend.com/article/627
–  http://phptutorial.info/
–  http://www.tizag.com/phpT/
•  Frameworks
–  Codeigniter http://codeigniter.com/
–  List of Libraries
http://komunitasweb.com/2009/03/20-
great-php-library-you-need-to-know/
Interesting Links
•  http://thenextweb.com/insider/
2011/08/06/20-years-ago-today-the-world-
wide-web-opened-to-the-public/

Você também pode gostar