Javascript - Technion – Israel Institute of Technology

Download Report

Transcript Javascript - Technion – Israel Institute of Technology

Server Side scripting
PHP
1
What is PHP?
• PHP stands for PHP: Hypertext Preprocessor
• PHP is a server-side scripting language, like ASP
• PHP scripts are executed on the server
• PHP supports many databases (MySQL, Informix,
Oracle, Sybase, Solid, PostgreSQL, Generic
ODBC, etc.)
• PHP is an open source software (OSS)
• PHP is free to download and use
2
What is a PHP File?
• PHP files may contain text, HTML tags and
scripts
• PHP files are returned to the browser as plain
HTML (View source would not work here!)
• PHP files have a file extension of ".php",
".php3", or ".phtml"
3
Why PHP?
• PHP runs on different platforms (Windows,
Linux, Unix, etc.)
• PHP is compatible with almost all servers used
today
• PHP is FREE to download from the official PHP
resource: www.php.net
• PHP is easy to learn and runs efficiently on the
server side
4
Hello world example
• PHP code can be placed anywhere in the
document.
• Starts and ends with <?php … ?>
•Each code line in PHP must end with a semicolon
<html>
<body>
<?php echo "Hello World"; ?>
</body>
</html>
5
Comments
<html>
<body>
<?php
//This is a comment
/* This is a comment block */
?>
</body>
</html>
6
Variables in PHP
• All variables in PHP start with a $ sign symbol. Variables may
contain strings, numbers, or arrays.
• To concatenate two or more variables together, use the dot (.)
operator
<html>
<body>
<?php
• The result would
be:
$txt1="Hello World";
$txt2="1234";
echo $txt1 . " " . $txt2 ;
“Hello World 1234”
• Rest of operatorssame as in JS.
?>
</body>
</html>
7
Flow Control- if-else
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
else
echo "Have a nice day!"; ?>
</body>
</html>
8
Flow Control- Switch
<?php
switch ($x) {
case 1:
echo "Number 1";
break;
case 2:
echo "Number 2";
break;
default:
echo "No number between 1 - 2";
} ?>
9
Arrays
• Numeric array - An array with a numeric ID key
• $names = array("Peter","Quagmire");
• $names[0] = "Peter"; $names[1] =
"Quagmire";
• Associative array - An array where each ID key
is associated with a value
• $ages = array("Peter"=>32, "Quagmire"=>30);
• $ages['Peter'] = "32"; $ages['Quagmire'] =
"30";
• Multidimensional array - An array containing
one or more arrays
10
Loops
• while ; do...while; for; foreach
<html>
<body>
<?php
$arr=array("one", "two", "three");
foreach ($arr as $value)
{
echo "Value: " . $value . "<br />";
}
?>
</body>
</html>
11
Functions
• In PHP - there are more than 700 built-in functions available.
• We will now show how to how to create your own function
<html>
<body>
<?php
function add($x,$y)
{
$total = $x + $y;
return $total;
}
echo "1 + 16 = " . add(1,16)
?>
</body>
</html>
12
PHP Form Handling
•Get Vs. Post
•$_POST example:
<form action="welcome.php" method="post">
Enter your name: <input type="text" name="name" />
Enter your age: <input type="text" name="age" />
<input type="submit" />
</form>
Welcome.php
Welcome <?php echo $_POST["name"]; ?>.
<br />
You are <?php echo $_POST["age"]; ?> years old!
13
PHP Sending E-mails
• mail(to,subject,message,headers,parameters)
• mail example:
<?php
$to = "[email protected]";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "[email protected]";
$headers = "From: $from";
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
14
Accessing MySQL DB using PHP
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con) {
die('Could not connect: ' .mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM person");
while($row = mysql_fetch_array($result)) {
echo $row['FirstName'] . " " . $row['LastName'];
echo "<br />";
ID
Fname
Lname
Age
}
1
Haim
Cohen
40
mysql_close($con);
?>
2
Israel
Aharony
46
15
3
Nir
Zuk
35
The End!
These slides are based on:
http://www.w3schools.com/php/default.asp
16