Professional Documents
Culture Documents
Chapter 5
Chapter 5
Outline
2
Working Remotely
Web server already configured with PHP and
MySQL is needed
High speed connection is needed
Use Telnet or SSH to create databases and set
permissions from command line
Need to have software like PuTTY for Telnet/SSH
access on windows PC , Mac has already SSH
Use FTP softwares like FireFTP,FileZilla for file
transfer on webservers
Prerequisites for PHP
6
Program Editors
Plain-text editors
Notepad
For Highlighted code
Notepad ++
Editra
IDE
NetBeans
phpDesigner
What is PHP?
7
HTML content
<?php
PHP code
?>
HTML content
<?php
PHP code
?>
HTML content ... PHP
Single comments
#
//
Multi line comment
/* */
All php commands should end with semicolon
Variable declarations
Formulas/expressions
Statements etc.
Variables
12
$user_name = “kebedexxx";
$age = 16;
$drinking_age = $age + 5;
$this_class_rocks = TRUE; PHP
print "text";
PHP
Hello world! Escape "chars" are the SAME as in Java! You can have line
breaks in a string. A string can use "single-quotes". It's cool!
output
Hello World!
14
<?php
print "Hello, world!";
?> PHP
Hello world!
output
Echo vs print
15
Echo Print
+ - * / % . ++ --
= += -= *= /= %= .=
many operators auto-convert types: 5 + "7" is 12
Logical Operators
17
Precedence (High to low)
18
Examples
19
$a = 7 / 2; # float: 3.5
$b = (int) $a; # int: 3
$c = round($a); # float: 4.0
$d = "123"; # string: "123"
$e = (int) $d; # int: 123 PHP
$favorite_food = "Ethiopian";
print $favorite_food[5].”<br>”;
$favorite_food = $favorite_food . " Shiro";
print $favorite_food;
#output
P
Ethiopian Shiro
zero-based PHP
indexing using bracket notation
there is no char type; each letter is itself a String
string concatenation operator is . (period), not +
5 + "2 turtle doves" === 7
5 . "2 turtle doves" === "52 turtle doves"
can be specified with double quotes or single quotes
String Functions (cont.)
21
# index 01234567890
$name = "Abebe Bikila";
$length = strlen($name);
$cmp = strcmp($name, “Abebe Kitila");
$index = strpos($name, "e");
$first = substr($name, 6, 6);
$name = strtoupper($name);
print $length."<br>";#12
print $cmp."<br>";#-1
print $index."<br>";#2
print $first."<br>";#Bikila
print $name;#ABEBE BIKILA PHP
Interpreted Strings
23
$age = 16;
print "You are " . $age . " years old.\n";
print "You are $age years old.\n"; # You are 16 years old.
PHP
if (condition) {
statements;
} elseif (condition) {
statements;
} else {
statements;
}
PHP
switch(expr){
case val1: statement1; break;
case val2: statement2; break;
case val3: statement3; break;
default: statement;
}
PHP
Example
29
while (condition) {
statements;
} PHP
do {
statements;
} while (condition);
PHP
32 PHP Arrays
<?php
$a=array(………………..);
$b=array(…array(…..) );
foreach(elem in $a)
{
…….
} ?>
PHP arrays
33
formProcess.php
<html>
<body>
Welcome <?php $_POST[“name”];?><br>
Your birthday is:<?php $_POST[“bday”];?>
</body>
</html>
PHP
45 Php Functions
<?php
function function_name(par1,par2,…, parN)
{
-------
}
?>
Functions
46
48
<?php
$host=‘localhost:3306’;
$user=‘root’;
$pass=‘’;
$con=mysql_connect($host,$user,$pass); #create con
$sql=‘create Database A’;
$db=mysql_query($sql,$con); #creates db A
if(!$db)
die(‘couldn’t create database’);
else
echo ‘Database created successfully’;
?>
PHP
…continued
50
<?php
mysql_select_db(‘A’,$con); #selects db A
$sql2=‘select * from XXX’;
$exe=mysql_query($sql2,$con);
#retrieval of data from table xxx
while($r=mysql_fetch_array($exe,MYSQL_ASSOC))
{
echo “A:{$r[‘A’]}<br>”.
“B:{$r[‘B’]}<br>”.
“<br>”;
}
mysql_close($con); //closes con
?> PHP