The while statement look like this:
while (condition/s) {
// do something
//increment or decrement to limit iteration
}
the real example is below:
<?php
$counter = 1;
while ($counter <= 10) {
echo "$counter * 10 = ".($counter * 10)."<br/>";
$counter++;
}
?>
save it as while.php and open your browser. The result should be:
1 * 10 = 10
2 * 10 = 20
3 * 10 = 30
4 * 10 = 40
5 * 10 = 50
6 * 10 = 60
7 * 10 = 70
8 * 10 = 80
9 * 10 = 90
10 * 10 = 100
it's the same result as if we use "for" statement.
In this example, we initialize the variable $i, with a value of 1. The while statement tests the $i variable, so that as long as the condition(s) is true, in this case the value of $i is less than or equal to 10, the loop will continue to run. Within the while statement's code block, the value of $i is multiplied by 10 and the result is printed to the browser. In line 5, the value of $i is incremented by 1. This step is extremely important, for if you did not increment the value of the $i variable, the while expression would never resolve to false and the loop would never end.
Looping for - Meeting 10
Basically "PHP" has 3 kinds of loop, namely, "for" statement, "while" statement, and "do ... while" statement. Below we learn on "for" statement.
The regular expression of "for" statement is like this:
for (initialization; condition(s) or bound(s); increment or decrement) {
// code to be executed
}
The expressions within the parentheses of the "for" statement are separated by semicolons. Usually, the first expression initializes an $i (as a counter) variable, the second expression is the test condition for the loop, and the third expression increments or decrements the counter depending on the condition given. Below we go directly to the example:
<?php
for ($i=1; $i<=10; $i++) {
echo "$i * 10 = ".($i * 10)."<br />";
}
?>
save it as loopfor1.php and open via your browser. The result should be like this:
1 * 10 = 10
2 * 10 = 20
3 * 10 = 30
4 * 10 = 40
5 * 10 = 50
6 * 10 = 60
7 * 10 = 70
8 * 10 = 80
9 * 10 = 90
10 * 10 = 100
One thing to watch in "for" statement is that "don't forget to write the condition(s) expression" to stop looping so that the program will not loop infinitely.
The regular expression of "for" statement is like this:
for (initialization; condition(s) or bound(s); increment or decrement) {
// code to be executed
}
The expressions within the parentheses of the "for" statement are separated by semicolons. Usually, the first expression initializes an $i (as a counter) variable, the second expression is the test condition for the loop, and the third expression increments or decrements the counter depending on the condition given. Below we go directly to the example:
<?php
for ($i=1; $i<=10; $i++) {
echo "$i * 10 = ".($i * 10)."<br />";
}
?>
save it as loopfor1.php and open via your browser. The result should be like this:
1 * 10 = 10
2 * 10 = 20
3 * 10 = 30
4 * 10 = 40
5 * 10 = 50
6 * 10 = 60
7 * 10 = 70
8 * 10 = 80
9 * 10 = 90
10 * 10 = 100
One thing to watch in "for" statement is that "don't forget to write the condition(s) expression" to stop looping so that the program will not loop infinitely.
"Switch" Statement - Meeting 9
"switch" statement is the alternative way to the multiple "if..elseif" conditions and can be shorter way to code than "if..elseif". The regular scripts applying "switch" staement is like this:
switch (expression) {
case result1:
// execute this if expression results in result1
break;
case result2:
// execute this if expression results in result2
break;
default:
// execute this if no break statement has been encountered hitherto
}
a switch statement will inspect expression by expression in a list of expression, and fire only one statement which is a match.
It is important to include a break statement at the end of any code that will be executed as part of a case statement. Without a break statement, the program flow will continue to the next case statement and ultimately to the default statement. In most cases, this will result in unexpected behavior, likely incorrect!
try a script below and save it as eight.php
<?php
$greeting = "morning"; //you may change to "afternoon" or "evening"
switch ($greeting) {
case "morning":
echo "Good morning";
break;
case "afternoon":
echo "Good afternoon";
break;
case "evening":
echo "Good evening";
break;
default:
echo "I don't know what to say but good $mood anyway.";
break;
}
?>
we initialize a variable $greeting with a value "morning". "switch" will evaluate each "case" and if find a match, it will be executed. In this case the result is "Good morning". Yet if we change the value of the variable $greeting to "afternoon" or "evening" the result will change accordingly.
switch (expression) {
case result1:
// execute this if expression results in result1
break;
case result2:
// execute this if expression results in result2
break;
default:
// execute this if no break statement has been encountered hitherto
}
a switch statement will inspect expression by expression in a list of expression, and fire only one statement which is a match.
It is important to include a break statement at the end of any code that will be executed as part of a case statement. Without a break statement, the program flow will continue to the next case statement and ultimately to the default statement. In most cases, this will result in unexpected behavior, likely incorrect!
try a script below and save it as eight.php
<?php
$greeting = "morning"; //you may change to "afternoon" or "evening"
switch ($greeting) {
case "morning":
echo "Good morning";
break;
case "afternoon":
echo "Good afternoon";
break;
case "evening":
echo "Good evening";
break;
default:
echo "I don't know what to say but good $mood anyway.";
break;
}
?>
we initialize a variable $greeting with a value "morning". "switch" will evaluate each "case" and if find a match, it will be executed. In this case the result is "Good morning". Yet if we change the value of the variable $greeting to "afternoon" or "evening" the result will change accordingly.
If ... elseif - Meeting 8
We still learn more on "if" condition. In this lesson we learn on multiple conditions "if ... elseif" . We have more than two choices but still only one is fired. The script will inspect, find and execute only the "true" condition.
We still use six.php (in meeting 6 as the first file to call) to be opened from your browser, and save this script below as seven.php (rename first the previous seven.php to another filename):
<html>
<head>
<title>Conditions - cont</title>
</head>
<body>
<h3>Thank you for entering your data</h3><br>
<h4>The mark's result is:</h4>
<?php
$usrname = $_POST['usrname'];
$mark = $_POST['mark'];
if (($mark<=100)&& ($mark>90)){
print("<h3>
$usrname got <font color=#000000><b>\"A\"</b></font>
</h3>");
}
elseif (($mark<=90)&& ($mark>75)){
print("<h3>
$usrname got <font color=#0000ff><b>\"B\"</b></font>
</h3>");
}
elseif (($mark<=75)&& ($mark>60)){
print("<h3>
$usrname got <font color=#bb0000><b>\"C\"</b></font>
</h3>");
}
elseif (($mark<=60)&& ($mark>40)){
print("<h3>
$usrname got <font color=#ff0000><b>\"D\"</b></font>
</h3>");
}
elseif (($mark<=40)&& ($mark>=0)){
print("<h3>
$usrname was <font color=#ff0000>
<b><u>\"Failed\"</u></b></font>
</h3>");
}
else {
print("<h3>
<font color=##ff0000><b>\"Invalid Data\"</b></font>
</h3>");
}
?>
</body>
</html>
in the file above we also apply a little bit more complicated conditions with the hope that we can make a good progress on conditions. Try to call six.php from your browser and try some various inputs to submit and see the results.
We still use six.php (in meeting 6 as the first file to call) to be opened from your browser, and save this script below as seven.php (rename first the previous seven.php to another filename):
<html>
<head>
<title>Conditions - cont</title>
</head>
<body>
<h3>Thank you for entering your data</h3><br>
<h4>The mark's result is:</h4>
<?php
$usrname = $_POST['usrname'];
$mark = $_POST['mark'];
if (($mark<=100)&& ($mark>90)){
print("<h3>
$usrname got <font color=#000000><b>\"A\"</b></font>
</h3>");
}
elseif (($mark<=90)&& ($mark>75)){
print("<h3>
$usrname got <font color=#0000ff><b>\"B\"</b></font>
</h3>");
}
elseif (($mark<=75)&& ($mark>60)){
print("<h3>
$usrname got <font color=#bb0000><b>\"C\"</b></font>
</h3>");
}
elseif (($mark<=60)&& ($mark>40)){
print("<h3>
$usrname got <font color=#ff0000><b>\"D\"</b></font>
</h3>");
}
elseif (($mark<=40)&& ($mark>=0)){
print("<h3>
$usrname was <font color=#ff0000>
<b><u>\"Failed\"</u></b></font>
</h3>");
}
else {
print("<h3>
<font color=##ff0000><b>\"Invalid Data\"</b></font>
</h3>");
}
?>
</body>
</html>
in the file above we also apply a little bit more complicated conditions with the hope that we can make a good progress on conditions. Try to call six.php from your browser and try some various inputs to submit and see the results.
If Else - Meeting 7
As a further lesson of "if" condition, now we learn on "if ... else " contion. In this lesson there are two choices and only one is fired. If the condition is "true" then the first statement is fired and if "false" the second statement is fired.
This is similar to the previous lesson but different. Pay attention and see the difference from the previous.
<html>
<head>
<title>Conditions - cont</title>
</head>
<body>
<h3>Thank you for entering your data</h3><br>
<h4>The mark's result is:</h4>
<?php
$usrname = $_POST['usrname'];
$mark = $_POST['mark'];
if ($mark >= 60){
print("<h3> $usrname is <font color=blue><b>\"Passed\"</b></font>
</h3>");
}
else {
print("<h3>
$usrname is <font color=red><b>\"Not Passed\"</b></font>
</h3>");
}
?>
</body>
</html>
save it as seven.php (this might overwrite the previous file unless you renamed it before save this file) and we still use six.php (in the previous lesson) file to call the form so that we can save time bu not recoding the first/origin file.
The login is, if condition is "true" then the statement in the first { } bracket is executed, if "false" the statement in the second { } bracket is executed.
This is similar to the previous lesson but different. Pay attention and see the difference from the previous.
<html>
<head>
<title>Conditions - cont</title>
</head>
<body>
<h3>Thank you for entering your data</h3><br>
<h4>The mark's result is:</h4>
<?php
$usrname = $_POST['usrname'];
$mark = $_POST['mark'];
if ($mark >= 60){
print("<h3> $usrname is <font color=blue><b>\"Passed\"</b></font>
</h3>");
}
else {
print("<h3>
$usrname is <font color=red><b>\"Not Passed\"</b></font>
</h3>");
}
?>
</body>
</html>
save it as seven.php (this might overwrite the previous file unless you renamed it before save this file) and we still use six.php (in the previous lesson) file to call the form so that we can save time bu not recoding the first/origin file.
The login is, if condition is "true" then the statement in the first { } bracket is executed, if "false" the statement in the second { } bracket is executed.
If Condition - Meeting 6
Now we learn on "IF Ststement/Condition". In this example we still apply 2 files (pages) so that we can remember the previous lesson. Please pay attention to the single "if condition" below:
<html>
<head>
<title>Conditions</title>
</head>
<body>
<h3>IF Condition</h3>
<br>
Fill in a name and a exam's mark[0-100] below:
<form action="seven.php" method="post">
<pre>
Nama : <input type="text" name="usrname" size="30" maxlength="30"><br><br>
Nilai : <input type="text" name="mark" size="3" maxlength="3"><br><br>
<input type="submit" value="Send Soon!">
</pre>
</form>
</body>
</html>
save the above file as six.php
Then create a script as a new file and save it as seven.php like below:
<html>
<head>
<title>Seven</title>
</head>
<body>
<h3>Thank you for entering your data</h3><br>
<h4>The mark's result is:</h4>
<br>
<?php
$usrname = $_POST['usrname'];
$mark = $_POST['mark'];
$result = "Not Passed";
if ($mark >= 60){
$result = "Passed";
}
print("<h3>
$usrname is <font color=red><b>$result</b></font>
</h3>");
?>
</body>
</html>
Then open your browser and point to the six.php and see the result. It should show that the mark greater than or equal to 60 is "passed", if not it s "not passed".
The logic is, if the condition is "true" then the statement inside the { } bracket is executed if "false" it is not executed.
<html>
<head>
<title>Conditions</title>
</head>
<body>
<h3>IF Condition</h3>
<br>
Fill in a name and a exam's mark[0-100] below:
<form action="seven.php" method="post">
<pre>
Nama : <input type="text" name="usrname" size="30" maxlength="30"><br><br>
Nilai : <input type="text" name="mark" size="3" maxlength="3"><br><br>
<input type="submit" value="Send Soon!">
</pre>
</form>
</body>
</html>
save the above file as six.php
Then create a script as a new file and save it as seven.php like below:
<html>
<head>
<title>Seven</title>
</head>
<body>
<h3>Thank you for entering your data</h3><br>
<h4>The mark's result is:</h4>
<br>
<?php
$usrname = $_POST['usrname'];
$mark = $_POST['mark'];
$result = "Not Passed";
if ($mark >= 60){
$result = "Passed";
}
print("<h3>
$usrname is <font color=red><b>$result</b></font>
</h3>");
?>
</body>
</html>
Then open your browser and point to the six.php and see the result. It should show that the mark greater than or equal to 60 is "passed", if not it s "not passed".
The logic is, if the condition is "true" then the statement inside the { } bracket is executed if "false" it is not executed.
Catching Parameters - Meeting 5
In this lesson, we are learning on catching parameters from another page. So we will have 2 files as examples, the first is containing a form in which will be submitted to another page. The second is the file which will catch the first page/file.
Here is the first script:
<html><head><title>Fourth</title></head>
<body>
<center><h3>Registration</h3></center>
<hr>
Please fill your username and password in this registration form below
<form action="five.php" method="post">
<pre>
Nama : <input type="text" name="usrname" size="30" maxlength="30"><br><br>
Password : <input type="password" name="passwd" size="8" maxlength="8"><br><br>
<input type="submit" value="Send Soon!">
</pre>
</form>
</body></html>
In the script above we only apply html codes as we ignore some security aspects for this basic lesson. The most important thing is the parameter action="name.php" method="post" inside form tag, name="usrname" and name="passwd" because those parameters will be catchedby the next file/page.
Save the file as four.php
Next, here is the second script:
<html><head><title>Fifth</title></head>
<body>
<center><h3>Form Destination</h3></center>
<hr>
<h4>Thank you for submitting your registration form</h4>
We have completely received your registration form
<?php
$username = $_POST['usrname'];
$passwd = $_POST['passwd'];
print("Your username is <b>$usrname</b><br>");
print("Your password is <i>$passwd</i><br>");
?>
</body></html>
The file above must be saved as five.php. Why? It s because a page/file where the first file was addressed (see action="five.php" in the previous script). Then we catch the other two parameters with a command $_POST['...']. We are using "POST" because in the first file we used POST method. Basically we may use POST or GET method.
Here is the first script:
<html><head><title>Fourth</title></head>
<body>
<center><h3>Registration</h3></center>
<hr>
Please fill your username and password in this registration form below
<form action="five.php" method="post">
<pre>
Nama : <input type="text" name="usrname" size="30" maxlength="30"><br><br>
Password : <input type="password" name="passwd" size="8" maxlength="8"><br><br>
<input type="submit" value="Send Soon!">
</pre>
</form>
</body></html>
In the script above we only apply html codes as we ignore some security aspects for this basic lesson. The most important thing is the parameter action="name.php" method="post" inside form tag, name="usrname" and name="passwd" because those parameters will be catchedby the next file/page.
Save the file as four.php
Next, here is the second script:
<html><head><title>Fifth</title></head>
<body>
<center><h3>Form Destination</h3></center>
<hr>
<h4>Thank you for submitting your registration form</h4>
We have completely received your registration form
<?php
$username = $_POST['usrname'];
$passwd = $_POST['passwd'];
print("Your username is <b>$usrname</b><br>");
print("Your password is <i>$passwd</i><br>");
?>
</body></html>
The file above must be saved as five.php. Why? It s because a page/file where the first file was addressed (see action="five.php" in the previous script). Then we catch the other two parameters with a command $_POST['...']. We are using "POST" because in the first file we used POST method. Basically we may use POST or GET method.
Data type conversion - Meeting 4
Now, we are going to learn on data type's conversion in PHP. Though in PHP we may undeclare the data-type, but we sometimes need to convert the data-type to get the exact results and avoid misleading results. Below is a script which show 3 kinds of how to convert a data-type in PHP. Please read carefully and uncomment one by one of each method of conversions to understand the functions.
<h3>Data type conversion</h3>
<hr><h4>Calculating A Circle</h4>
<?php
$rad = "10.5 cm";
define("phi",3.14);
//php has 3 ways on converting data types. to try it one by one you can uncomment each command below
/*settype($rad,"integer");*/ /*$rad = intval($rad);*/ /*$rad=(integer)$rad;*/
/*settype($rad,"double");*/ /*$rad = doubleval($rad);*/ /*$rad=(double)$rad;*/
/*settype($rad,"string);*/ /*$rad = strval($rad);*/ /*$rad=(string)$rad;*/
printf("Circle radius : %s <br>",$rad);
printf("Circle's area : %s cm<sup>2</sup><br>",($rad*$rad*phi));
printf("Circle's circumference : %s cm<br>", (2*phi*$rad));
?>
The result should be like this:

as usual, any questions can be posted via comments and so do the answers. If needed iI will create a new label providing "questions and answers". See you next meeting.
<h3>Data type conversion</h3>
<hr><h4>Calculating A Circle</h4>
<?php
$rad = "10.5 cm";
define("phi",3.14);
//php has 3 ways on converting data types. to try it one by one you can uncomment each command below
/*settype($rad,"integer");*/ /*$rad = intval($rad);*/ /*$rad=(integer)$rad;*/
/*settype($rad,"double");*/ /*$rad = doubleval($rad);*/ /*$rad=(double)$rad;*/
/*settype($rad,"string);*/ /*$rad = strval($rad);*/ /*$rad=(string)$rad;*/
printf("Circle radius : %s <br>",$rad);
printf("Circle's area : %s cm<sup>2</sup><br>",($rad*$rad*phi));
printf("Circle's circumference : %s cm<br>", (2*phi*$rad));
?>
The result should be like this:
as usual, any questions can be posted via comments and so do the answers. If needed iI will create a new label providing "questions and answers". See you next meeting.
Subscribe to:
Posts (Atom)