1 CS428 Web Engineering Lecture 20 Control Structures, Loops and Pointers File Uploading Function...
-
Upload
whitney-owen -
Category
Documents
-
view
215 -
download
0
Transcript of 1 CS428 Web Engineering Lecture 20 Control Structures, Loops and Pointers File Uploading Function...
1
CS428 Web EngineeringLecture 20
Control Structures, Loops and PointersFile Uploading Function
(PHP - III)
• Format:
if (expression)
statement;
• Example:
if ($a > $b)
echo “a is larger than b”;
Logical Expression: If Statement
EXAMPLE• <?php
$a = 4;
$b = 3;
if ($a > $b){
echo “a is larger than b”;
}
?>
• Boolean Operators: >, >=, <, <=, ==, !=
EXAMPLE• <?php
$a = 3;
$b = 4;
if ($a > $b) {
echo “a is larger than b”;
}
if ($a < $b){
echo “a is not larger than b”;
}
?>
• <?php
$a = 3;
$b = 4;
if ($a > $b) {
echo “a is larger than b”;
}
else {
echo “a is not larger than b”;
}
?>
EXAMPLE• How to deal with different cases:• <?php
$a = 4;
$b = 4;
if ($a > $b) {
echo “a is larger than b”;
} elseif ($a == $b) {
echo “a equals to b”;
} else {
echo “a is smaller than b”;
}
?>
LOGICAL OPERATORS (AND)• <?php
$a = 5;
$b = 4;
?>
<?php
$c = 20;
$d = 1;
if (($a > $b) && ($c > $d)){
echo “a is larger than b AND ”;
echo “c is larger than d ”;
}
?>
LOGICAL OPERATORS (OR)• <?php
$a = 5;
$b = 4;
?>
<?php
$c = 1;
$d = 20;
if (($a > $b) || ($c > $d)){
echo “a is larger than b OR ”;
echo “c is larger than d ”;
}
?>
EXAMPLE• If variable a is not set, then set equals to 100.• <?php
$a = 5;
$b = 4;
?>
<?php
if (!isset($a)){
$a = 100;
}
echo $a;
?>
EXAMPLE• If variable a is not set, then set equals to 100.• <?php
$a = 5;
$b = 4;
?>
<?php
unset($a);
if (!isset($a)){
$a = 100;
}
echo $a;
?>
• If a is integer, then set its type equals to string.• <?php
$a = 5;
$b = 4;
?>
<?php
if (is_int($a)) {
settype($a, “string”);
}
echo gettype($a);
?>
Logical Expressions: Switch• <?php $a = 3; ?>• <?php
switch ($a){
case 0:
echo “a equals 0”;
break;
case 1:
echo “a equals 1”;
break;
case 2:
echo “a equals 2”;
break;
default:
echo “a is not 0, 1 or 2”;
break;
}
?>
LOOPS• Loops allow us to execute the code until
the condition is satisfied. There are three main kinds of loops:
• while Loops• for Loops• foreach Loops
WHILE LOOPS• The format of while loops is as under:
while (expression)
statement;
• The expression would be Boolean statement,
EXAMPLE• <?php
$count = 0;
while ($count <= 10) {
echo $count . “, ”;
$count++;
}
echo “<br /> Count: {$count}”;
?>
EXAMPLE• <?php
$count = 0;
while ($count <= 10) {
if ($count == 5) {
echo “FIVE”;
} else {
echo $count . “, ”;
}
$count++;
}
echo “<br /> Count: {$count}”;
?>
FOR LOOP• The format of for loops is as under:
for (expr1, expr2, expr3)
statement;
• The expression would be boolean statement,
• Expression1 could be initializing statement.• Expression2 could be condition/test.• Expression3 could be increment/decrement.
$_POST• We use post method in a form. When we
submit the button of form, all the data of form is send to $_POST array.
<form method=“post” action=“process.php”>
• We get this data on any other page from this super global array $_POST.
EXAMPLE• Myform.php
<body>
<form method=“post” action=“dataform.php”>
<input type=“text” name=“txtname” />
<input type=“submit” value=“Say Hello” name=“sbtbtn” />
</form>
</body>
• dataform.php
<?php
If($_POST)
{
$Name = $_POST[‘txtname’];
$Name =$_REQUEST[‘txtname’];
echo “Hello” . $Name;
}
?>
$_GET• The data that we send through URL also known as
query string is stored in this super global array $_GET.
http://www.example.com/search.php?keyword=pakistan
• Query string started in URL after ? Sign.
• In a URL there may be more than one query string.
http://www.example.com/search.php?keyword=pakistan&city=isb
EXAMPLE• link.php
<body>
<a href=“get.php?site=ITDunya”>Sending QueryString</a>
</body>
• get.php
<?php
$siteName = $_GET[‘site’];
echo “Welcome to ” . $siteName;
?>
$_FILES• When we upload a file or an image to server, it
stores in $_FILES array.• Example:• User uploads a file from his computer c:\docs\
project.zip • Its size is 20,000 bytes• PHP store in a temp folder, and gives it a
random name.• $_FILES[userfile][name]• $_FILES[userfile][size]• $_FILES[userfile][type]
EXAMPLE• Upload_form.php
<form action="upload.php" method="post" enctype="multipart/form-data"> Select image to upload: <input type="file" name="fileToUpload" id="fileToUpload"> <input type="submit" value="Upload Image" name="submit"></form>
• Upload.php
<?php
move_uploaded_file($_FILES[' fileToUpload ']['tmp_name'], 'uploads/'.$new_file_name);
$message = ‘Your file was accepted.';
?>
FOREACH LOOP • The format of foreach loops is as under:
foreach ($array as $value)
statement;
• The boolean test foreach loop is, do we have items still left in array. Foreach loop will go through every single element of an array.
• Foreach loop only works with array. • Foreach loop works, it stores elements in
array in a temporary variable
EXAMPLE• <?php
$agesArray = array(4, 8, 15, 17, 23, 42);
?>
<?php
// using each value
foreach ($agesArray as $age) {
echo $age . “, ”;
}
?>
• Syntax for associative array• Foreach loop can take each array as a key
value pair.
foreach ($array as $key => $value)statement;
EXAMPLE• <?php
$ages = array(4, 8, 15, 16, 23, 42);
?>• <?php
// using each key => value pair
// key for 4 is 0, key for 8 is 1 and so on…
foreach ($ages as $position => $age) {
echo $position . “: ” . $age . “<br/>”;
}
?>
• <?php
$prices = array(“Brand new computer”=>2000, “1 month in lynda.com training center”=>25, “Learning PHP”=> “priceless”);
foreach ($prices as $key => $value) {
if (is_int($value)) {
echo $key . “: $” . $value . “<br/>”;
} else {
echo $key . “: ” . $value . “<br/>”;
}
}
?>
LOOPS: Continue• If we didn’t want to print let say 5• <?php
for ($count=0; $count <=10; $count++) {
if ($count == 5) {
continue;
}
echo $count . “, ”;
}
?>• Output: 1, 2, 3, 4, 6, 7, 8, 9, 10,
• Lets say we are looking through, a list of students in a database. We can perform lots of complex action on it.
• But the very first thing we check, whether or not the student is fresh man, s/w engineer jr. or senior.
• If we turns out, the student is fresh man, we don’t want anything else.
• Then we can have this check, are they fresh man, continue
LOOPS: Break• If we didn’t want to print before 5• <?php
for ($count=0; $count <=10; $count++) {
if ($count == 5) {
break;
}
echo $count . “, ”;
}
?>• Output: 1, 2, 3, 4,
• If we don’t want comma (,) at the end of last number.
• <?php
for ($count = 0; $count <= 10; $count++) {
echo $count;
if ($count == 10) {
break;
}
echo “, ”;
}
?>• Output: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
POINTERS• Computer maintains the pointer, that’s
points to the current value of an array. By default its always the first value.
• When we looping through array, computer moves that pointer along the array to get each value.
EXAMPLE• <?php
$ages = array(4, 8, 15, 16, 23, 42);
?>
<?php
echo “1: ” . current($ages) . “<br />”;
// that will give us, the current place, the pointer is pointing.
?>• Output: 1: 4
• <?php
$ages = array(4, 8, 15, 16, 23, 42);
?>
<?php
echo “1: ” . current($ages) . “<br />”;
next($ages);
echo “2: ” . current($ages) . “<br />”;
reset($ages);
echo “3: ” . current($ages) . “<br />”;
?>• Output: 1: 4
2: 8
3: 15