Escolar Documentos
Profissional Documentos
Cultura Documentos
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 = ‘Sam’;
We can use a variable in place of a
echo $name;
value such as a string.
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.
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
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/