Control Structures
•Control structures in php are same as many of programming languages, its syntax is most similar to c/c++ and perl.
•Nevertheless, PHP has one special control structure, distinguishes it from other programming languages.
•If Statement:
•if(condition) statement;
•If … else Statement
•if(condition) statement;
•else statement;
•If … elseif … else statement
•if(condition) statement;
•elseif(condition) statement;
•else statement;
•If Statement used to execute some statements if condition became true; for example if($x < 3) echo “Try again”;
•At this example if $x has value less than 3 then the condition is true, so Try again will be echo-ed on the web page. Otherwise it will not.
•If… else Statement, used to execute some action if condition became true. Or do something else if condition is false; for example if($x <=3) echo “Try Again”; else echo “You don‟t have permission”;
•At this example if $x has value less than or equal to 3 then Try Again will be echo-ed. else You don‟t have permission will be echo-ed.
•As a result one of the two blocks will be executed depending on the condition.
•If … elseif … else Statement used to execute some statements if condition is meeting some rules, if not you might do some actions to deal with another condition, or do general case.
Example:
<?php
$x = 1;
If($x <3) echo { “Try again”; $x++; }
elseif($x == 3) echo { “Last chance”; $x++; }
else echo “You don‟t have permissions”;
?>
Notes:
•In the previous script, suppose that $x has a value of two then first block will be executed, which means Try again will be echo-ed, and $x will be incremented by 1.
•If $x has a value of 3 then second block will be executed. Or last block will be executed if $x greater than 3.
•Switch Statement:
Used to execute some statements depending on one case or more.
Syntax:
switch(x)
{
case label1: executed when x == label 1;
break;
case label2: executed when x==label2;
break;
default: executed when x did not match any label;
}
Notes:
•Switch statement sometimes called multiple selection statement, that‟s because you might execute multiple blocks depending on your case.
•Unlike if statement. Domain of switch statement is first appearance of break keyword. i.e your code will be executed until first break stops it.
•Default case does not need break label, because, often it is the last thing to be executed whether no labels matched your selection.
Enhancement for Page views Script
<?php
session_start();
if(isset($_SESSION[„views‟]))
$_SESSION[„views‟]++;
else
{
$_SESSION[„views‟] = 1;
}
?>
<html>
<body>
<p> you have visited this page <?php echo $_SESSION[„views‟]; ?> times </p>
</body>
</html>
Notes:
•At previous script, we called a new built-in function called isset().
•This function returns true or false depending on the given parameter. Its job is determining the given parameter has value or not. If given parameter has value and is not null, then it will return true; else it will return false.
•In our application we started a session by calling session_start(); this function call depends not on session value. It is only sets the server to store a session.
•Next, we checked for a session called views, using function isset(). This function will tell us that session views has been set before or not.
•Depending on the returned value of function isset, we‟ll take our action, or in other words, do the purpose of the script which is telling the visitor how many pages he had visited from the time he opened the site.
•As we explained before, sessions are available to all pages in one application. Which means that any page you want to track user hits on it simply include this script to it. We‟ll illustrate how to include scripts in your script, next.
•Suppose that function isset returned true, this will mean that, there is an exist session called views, this means that it is not the first page user had viewed at this visit.
•So the suitable action to be performed is incrementing number of views by one, $_SESSION[„views‟]++;
•If isset returned false, this means that session views is not exist, so we had to initialize it. By starting the counter from one. $_SESSION[„views‟] = 1;
•Remember that sessions have UID, and each visitor has his own session.
•Last thing is printing value of session views.
To download this lesson as a PDF file click here: PHP Tutorial - Lesson 2: Introduction to PHP - Part II
Best Wishes,
Anas Jaghoub
Comments
Post a Comment