Skip to main content

PHP Tutorial - Lesson 2: Introduction to PHP - Part II

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

Popular posts from this blog

PHP Tutorial–Guest Book–System Definition and Requirements

In this lesson I’m going to illustrate the system definition and requirements for the Guest Book application. It is an important step in developing any application, since this step gives us an orientation about the system and its functionality, what is expected from the system to do. actually the system definition comes from keywords from customers and end users, usually try to make it clear for what they need in the application, and what they expect the system to do.  As developers it is our role to define the requirements for the system to be developed. In our example the system definition for the Guest Book is: A system that allows visitors of the site to post their comments and feedbacks about the site, with the possibility for managing comments and maintain it easily and user friendly. On the other hand, the system requirements are: a web server, since it is clear that the developed system is going to run on the Internet, so it is a web-based application not windows-based.

تعلم تطوير تطبيقات للموبايل باستخدام الأندرويد

مرحبا أصدقائي يسعدني أن أبدأ معكم سلسلة حلقات في تعلم تطوير تطبيقات للموبايل باستخدام الأندرويد ، وسأسعى جاهدا معكم في أن تكون هذه السلسلة من أوائل السلسلات في اللغة العربية لتعليم برمجة تطبيقات الأندرويد من البداية وحتى الاحتراف. وسأحاول قدر المستطاع الشرح بلغة عربية بسيطة ومفهومة. حيث ستكون غدا إن شاء الله الحلقة الأولى من تعلم تطوير تطبيقات الموبايل باستخدام الأندرويد. أنس الجاغوب Twitter: @anasjaghoub Facebook: anasjaghoub

Web Development Best Practices (PHP Best Practices)

Websites development is one of the most challenging projects ever to develop, according to the large number of components you deal with, integrate, and manage in your application. For example, most web projects Deal with databases as their data source, and I ntegrate with external services called web services and APIs. As well the application M anages its own objects and components from views to models to controllers to plugins and so on. A skilled web developer should have a great/deep knowledge in most of components used in his system, and as well should ha ve intensive focus on how each component work and how can be used, what can be done, and what cannot. On the other hand, the huge amount of requirements, components to manage, and time frame given to you to accomplish your mission, and number of people in your team, all these factors together requires from you to organize your tasks, and priorities your requirements, do deep analysis and planning in order to have a hi