8. If else statement
Set $a, $b manually!
<?php
if ($a > $b) {
echo "a is bigger than b";
} elseif ($a == $b) {
echo "a is equal to b";
} else {
echo "a is smaller than b";
}
?>
9. While statement
<?php
/* example 1 */
$i = 1;
while ($i <= 10) {
echo $i++; /* the printed value would be
$i before the increment
(post-increment) */
}
?>
10. While statement (2)
/* example 2 */
<?php
$i = 1;
while ($i <= 10) {
echo $i;
$i++;
}
?>
11. List statement
<?php
$info = array('coffee', 'brown', 'caffeine');
// Listing all the variables
list($drink, $color, $power) = $info;
echo "$drink is $color and $power makes it special.n";
// Listing some of them
list($drink, , $power) = $info;
echo "$drink has $power.n";
// Or let's skip to only the third one
list( , , $power) = $info;
echo "I need $power!n";
// list() doesn't work with strings
list($bar) = "abcde";
var_dump($bar); // NULL
?>
13. foreach statement
<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
$value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
unset($value); // break the reference with the
last element
?>
14. Exercise
Repeat a paragraph according to an input value.
For example: $p=3
This is a paragraph
This is a paragraph
This is a paragraph
Print the square value if read a even integer
Or Print the cubic value if read a odd integer
Or print as it is if read a string.
15. Create DB (phpmyadmin)
DB name: roubel
DB user: rb_user1
DB psw: myuser1
Host: localhost
Create table: orders
(4 columns: orderid(PK), oil, spark, tyres)
16. Connect to database
// Create connection
$con=mysqli_connect("localhost","rb_user1"
,"myuser1","roubel");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " .
mysqli_connect_error();
} else { echo "Connection was OK!n";}
17. Database query
Exercise: Insert variables instead of values
mysql_select_db($dbname, $con) or die
($dbname . " Database not found." . $dbuser);
18. Handle database result functions
//randomly select one row
$query = "SELECT * FROM `order` WHERE 1";
$result = mysqli_query($con, $query);
$num_results = mysqli_num_rows($result);
echo $num_results;
$result = mysqli_query($con, $query) or
die(mysqli_error($con));
19. Show array values
$info = mysqli_fetch_all( $result );
//$info = mysqli_fetch_array( $result ); //check it
echo '<br />';
foreach ($info[0] as $value) { echo "Value:
$value<br />n"; };
foreach ($info[1] as $value) { echo "Value:
$value<br />n"; };
21. Use list to fetch data
list(,$oilqty, $tyreqty, $sparksqty) = $info2;
echo '<br>';
echo '<br>oil from order 1: '.$oilqty;
echo '<br>tyres from order 1: '.$tyreqty;
echo '<br>sparks from order 1: '.$sparksqty;
23. Reuse code using functions
Create functions.php
function askDB($query)
{
.. Code .
return $info;
}
Include functions.php within php area
include functions.php';
Call fuction
$var = askDB($query);
24. Session handling
For all session pages!
session_start();
if (isset($_SESSION['mydog']))
{..code
}
else
{error printout..
}
25. Printf sprintf (formatted printout)
<?php
$num = 5;
$location = 'tree';
$format = 'There are %d monkeys in the %s';
echo sprintf($format, $num, $location);
?>