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

AABU GTUG @ J.U.S.T (Event)

AABU GTUG @ J.U.S.T (Event) On April 26, 2011 we have headed to Jordanian University for Science and technology (J.U.S.T), to run one of our events there. The event focused on introducing the GTUGS (Google’s Technologies Users Groups) and introducing Google’s technologies to students, such as Android and App Engine, to Chrome Extensions and HTML5 demos. Attendees were about 200 attendee, in addition to Mr. Mohammad Abu Musa the manager of Genesis Company for E-learning. Their was a lot of activities during the event, beginning with introducing the AABU GTUG, and assuring for cooperate working with students to increase the awareness of students about emergent technologies, and opening new GTUG at J.U.S.T, we announced the start of this group. We have presented a lot of sessions here is the Timetable: Subject presenter Time Hello! Inas Shoman 12:00-12:05 HTML5 Mohammad Jaradat, Abd Al Rahman Shoman 12:05-12:20 Android Inas Shoman, Mohamad Mahaqbeh, Mamoon Hwamdeh 12:20-12:35 Latest...

AABU GTUG Opening Event

On February 17, 2011 We’ve headed to Al Al Bayt University, to run one of the biggest event established there, for opening the AABU GTUG (Google’s Technologies Users Group). The event focused on introducing the GTUGS (Google’s Technologies Users Groups) and introducing Google’s technologies to students, such as Android and App Engine, to Chrome Extensions and HTML5 demos. Attendees exceeded 250 attendee, in addition to Vice president of Al Al Bayt University Dr. Hashem Al Masaeed, Deanship of College of I.T at Al Al Bayt University Prof. Ismail Ababneh, and a quite number of professors and teachers at the college of I.T at AABU, in addition to Yarmuk fm and Al Ro’aya  tv. AABU GTUG as a Google’s Technologies Users Group interested in Google’s technologies and tries as possible to increase students awareness about Google’s technologies, and introduce it to them. Their was a lot of activities during the event, beginning with the key note from me Anas Jaghoub, that included introducin...

سلسلة تعلم برمجة تطبيقات الاندرويد – إنشاء أول تطبيق للاندرويد – الحلقة رقم 2

تحدثنا في الحلقة السابقة عن إعداد بيئة العمل، وأهم الأدوات والحزم اللازمة للبدء في تطوير تطبيقات لنظام التشغيل الاندرويد. سنتحدث في هذه الحلقة عن كيفية إنشاء تطبيق للاندرويد، وما هي مكونات تطبيق الاندرويد والبنية الهيكلية ﻷي تطبيق على الاندرويد. إنشاء تطبيق باستخدام بيئة العمل Netbeans : 1. اضغط على قائمة File 2. اختر New Project. 3. اختر نوع المشروع Android 4. اضغط زر Next. 5. اضبط إعدادات المشروع كالتالي: Project Name: اسم المشروع، يفضل أن يكون اسم المشروع هو نفس اسم التطبيق وذلك لتمييزه عن باقي المشاريع الأخرى Package Name: اسم حزمة المشروع، وهنا يجب اتباع قواعد تسمية الحزم في لغة Java إذ يجب أن تتكون من مقطعين على الأقل ويفصل بين كل مقطع بنقطة، ولا يوجد فراغات. Target Platform: رقم نسخة نظام التشغيل الاندرويد التي يدعمها هذا التطبيق، وهنا لا بد من اختيار أقل رقم يتوافق معه هذا التطبيق لمزيد من التفاصيل شاهد الصورة التالية: 6. اضغط على زر Finish وهكذا نكون قد أنشأنا بمساعدة بيئة العمل Netbeans أول تطبيق للأندرويد حيث تم تصميم هيكل التطبيق، واستدعاء كافة ال...