Skip to main content

PHP Tutorial - Lesson 3: Introduction to PHP - Part I


Loops

          As we explained before, PHP syntax is similar to C/C++ and PERL.
          Loops at PHP, Not at all, are same as any loop in C/C++. But also, has a special type of loop will be illustrated when using arrays.

While Loop
          Sometimes you need a block of instructions to be repeated, as long as some condition is true.
          Here is an example
<?php
$x = 1;
while($x<10)
{
echo $x.”  “;
$x++;
}
?>
          Previous script is a simple application, that prints numbers from 1 to 9. as a programmer, you should be familiar with this kind of loop.

Do… While Loop
          In some cases you might want some instructions to be executed at least once, and be repeated as long as condition is true, best choice of loop here to be chosen is do … while loop.
<?php
$i=1;
do {
$i++;
echo $i.” “;
} while($i <10);
?>
          Previous script prints numbers from 2 to 9.

For Loop
          Often, for loop is used when you know how many times shall the loop run.
          Syntax:
          for(init,condition,increment)
          Init: is the code to be executed at beginning of the loop, it is executed once. Init could be any php statement, and might be more than one statement, but should be separated by commas ,
          Condition: the case that makes loop run.
          Increment: is the code to be executed at each time the loop run, as init it might be more than one statement.

Advices
          As a programmer, it is your responsible to determine, what is the best kind of loops to be chosen for your case.
          If you know exactly your script how many times should be run. Then the best choice of loop here will be for loop.
          If you don’t know how many times the script should be run, or it depends on user inputs, then best choice here while or do… while loop.
          If your script should be executed at least one time, then do … while is the best choice.
          If your script should be executed at most one time, then there is no need for loop!
          Latest type of loops that is special, or is one of the powerful properties of php, is foreach loop.
          This kind of loop is the best choice when you deal with arrays.
          Arrays will be discussed next Lesson.

To download this Lesson as PDF file click here: PHP Tutorial - Lesson 3: Introduction to PHP - Part I

Best Wishes,
Anas Jaghoub

Comments

Popular posts from this blog

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

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

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

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

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