Showing posts with label statement. Show all posts
Showing posts with label statement. Show all posts

Saturday, June 18, 2016

Using the static Statement to Remember the Value of a Variable Between Function Calls


If you declare a variable within a function in conjunction with the static statement, the variable remains local to the function, and the function "remembers" the value of the variable from execution to execution





<?php
function numberedHeading($txt) {
static $num_of_calls = 0;
$num_of_calls++;
echo "<h1>".$num_of_calls." ". $txt."</h1>"; }
numberedHeading("Mobile Phones");
echo "<p>We build a fine range of mobile phones.</p>";
numberedHeading("Camera");
echo "<p>Also Digital Cameras..</p>";
?>

The numberedHeading() function has become entirely self-contained. When we declare the $num_of_calls variable on line 3, we assign an initial value to it. This assignment is made when the function is first called on line 7. This initial assignment is ignored when the function is called a second time on line 9. Instead, the code remembers the previous value of $num_of_calls. We can now paste the numberedHeading() function into other scripts without worrying about global variables.

Wednesday, June 15, 2016

break statement

The break statement, though, enables you to break out of a code block.
Usually we use this in switch..case block. But we can use in loops as well as in the example below:

<?php
$i = -4;
for ($i ; $i <= 10; $i++ ) {
if ( $i == 0 )
break;
$temp = 1000/$i;
print "1000 divided by $i is... $temp<br>";
}
?>

save it as break.php and open from your browser.
We use break statement if we want to get out of a code block for probably to get rid of logic error such as division by zero or anything else.

The result should be like this:

1000 divided by -4 is... -250
1000 divided by -3 is... -333.33333333333
1000 divided by -2 is... -500
1000 divided by -1 is... -1000

The loop stop when $i equals to 0 (zero) and the loop gets out of the loop block.

Thursday, June 9, 2016

Saving State Between Function Calls with the static Statement

Local variables within functions have a short but happy life they come into being when the function is called and die when execution is finished, as they should. Occasionally, however, you may want to give a function a rudimentary memory.
Lets assume that we want a function to keep track of the number of times it has been called so that numbered headings can be created by a script. We could, of course, use the global statement to do this, as shown in Listing below:
<?php
$num_of_calls = 0;
function numberedHeading($txt) {
global $num_of_calls;
$num_of_calls++;
echo "<h1>".$num_of_calls." ".$txt."</h1>";
}
numberedHeading("Mobile Phones");
echo "<p>We build a fine range of mobile phones.</p>";
numberedHeading("Camera");
echo "<p>Also Digital Cameras.</p>";
?>


Saturday, May 28, 2016

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 dont 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.

Friday, May 20, 2016

Accessing Variables with the global Statement

From within one function, you cannot (by default) access a variable defined in another function or elsewhere in the script. Within a function, if you attempt to use a variable with the same name, you will only set or access a local variable. Lets put this to the test :
<?php
$life = 42;
function meaningOfLife() {
echo "The meaning of life is ".$life;
}
meaningOfLife();
?>
Put these lines into a text file called scopetest2.php and place this file in your web server document root. When you access this script through your web browser, it should look like:




As you might expect, the meaningOfLife() function does not have access to the $life variable in line 2; $life is empty when the function attempts to print it. On the whole, this is a good thing because it saves us from potential clashes between identically named variables, and a function can always demand an argument if it needs information about the outside world. Occasionally, you may want to access an important variable from within a function without passing it in as an argument. This is where the global statement comes into play. Use global to restore order to the universe.
<?php
$life=42;
function meaningOfLife() {
global $life;
echo "The meaning of life is ".$life;
}
meaningOfLife();
?>
Put these lines into a text file called scopetest3.php and place this file in your web server document root. When you access this script through your web browser, it should look like:




By placing the global statement in front of the $life variable when we declare it in the meaningOfLife() function (line 4), it now refers to the $life variable declared outside the function (line 2).
You will need to use the global statement within every function that needs to access a particular named global variable. Be careful, though; if you manipulate the contents of the variable within the function, the value of the variable will be changed for the script as a whole.
You can declare more than one variable at a time with the global statement by simply separating each of the variables you want to access with commas:
global $var1, $var2, $var3;


Watch Out!
Usually, an argument is a copy of whatever value is passed by the calling code; changing it in a function has no effect beyond the function block. Changing a global variable within a function, on the other hand, changes the original and not a copy. Use the global statement carefully.


Sunday, May 15, 2016

while statement

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

its 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 statements 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.

Tuesday, May 10, 2016

do while statement

do...while statement is a little like while statement turned on its head. The essential difference between the two is that the code block is executed before condition test and not after it.

do {
// code to be executed
//increment or decrement
} while (expression of condition) ; // only if condition is true, the code block is repeated

Remember that do...while statement always ends with a semicolon.

This type of statement will be useful when we want the code block to be executed at least once, even if in while statement evaluates to false. The code block below is executed a minimum of one time:

<?php
$i = 1;
do {
echo "The number is: $i<br />";
$i++;
} while (($i > 10) && ($i < 20));
?>

The do...while statement tests whether the variable $i contains a value that is greater than 10 and less than 20. In line 2, we initialized $1 to 1, so this expression returns false. Nonetheless, the code block is executed at least one time before the condition is evaluated, so the statement will print a single line to the browser.

The number is: 1

If we change the value of $i in line 2 to something like 10 and then run the script, the loop will display
The number is: 10
The number is: 11
The number is: 12
The number is: 13
The number is: 14
The number is: 15
The number is: 16
The number is: 17
The number is: 18
The number is: 19

Monday, May 9, 2016

The continue statement

The continue statement ends the execution of th current iteration but doesnt end the loop as a whole. Instead, the next iteration begins immediately. Using the previous example as in the break statement, replacing the break statement with the continue statement, we can get rid of a divide-by-zero error without ending the loop completely. See the example below:

<?php
$i = -4;
for ($i ; $i <= 10; $i++ ) {
if ( $i == 0 )
continue;
$temp = 1000/$i;
print "1000 divided by $i is... $temp<br>";
}
?>

save it as continue.php and open it through your browser. The result should be:

1000 divided by -4 is... -250
1000 divided by -3 is... -333.33333333333
1000 divided by -2 is... -500
1000 divided by -1 is... -1000
1000 divided by 1 is... 1000
1000 divided by 2 is... 500
1000 divided by 3 is... 333.33333333333
1000 divided by 4 is... 250
1000 divided by 5 is... 200
1000 divided by 6 is... 166.66666666667
1000 divided by 7 is... 142.85714285714
1000 divided by 8 is... 125
1000 divided by 9 is... 111.11111111111
1000 divided by 10 is... 100

As the result says, we know that the iteration stop temporarily if $i equals to zero and then continue the loop until finish