Skip to main content

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


Arrays
What is a variable?

A variable is a storage area that holds only one value.

What is an array?

A special variable that holds multiple values.

One of the most powerful properties in php is Arrays.

PHP arrays have a property of being dynamic, that it is not limited with a size for it, you can add elements to it as needed.

Arrays in php also loosely typed.

There are three kinds of arrays in php, numeric, associative, and multidimensional. Each kind has its area of usability.

Numeric array: zero-based index.

You can declare numeric array in php in more than one method.

Example:
<?php
$age= array(“30”,”21”,”19”);
$age[3] = “17”;
$age[]= “35”;
echo $age[0].” “.$age[1].” “.$age[2].” “.$age[3].” “.$age[4];
?>

Previous script shows you how powerful it is to an array to be dynamic, so you can expand it as needed.

You can declare array and initialize it with values like $age = array(“value1”,”value2”)

You can expand it, by two methods. if you know the last index value, then next index will be last index+1. $age[3]=“17”;

If you don’t know what was last index you can use this great way $age[] = “value”;


This will add the new value to the bottom of the array.

Access Array values:

It is inconvenient to access each element of the array by entering its index, suppose you are talking about an array of 1000 elements, shall you access it line by line? It is not a programmer idea.

Now we’ll talk about special kind of loops which is foreach loop.

Foreach loop:

Syntax: foreach($array as $value)

{

Your code…

}

This kind of loop usually is used when dealing with arrays. Because you don’t know what is the size of the array, and you might need to access each element of the array.

Example:
<?php
$arr[]=“one”;
$arr[]=“two”;
$arr[]=“3”;
foreach($arr as $value) echo $value.” “;
?>
Notes:

We declared an array called arr. Then we added values to it.

Latest thing, we accessed each element of the array using the foreach loop, which will loop through each element of the array.

Associative array: an array in which each id is associated with a value.

When storing data about specific named values, a numerical array is not always the best choice to do it.


In associative arrays, we can use values as keys and assign values to it.

Example:
<?php
$age = array(“anas”=>20, “ahmad”=>21, “abd”=>22);
$age[‘mohammad’]=19;
echo “anas is “.$age[‘anas’].” years old”;
?>
Multidimensional array: each element in the array could be an array. And each element in the sub array could also be an array.
Example:
<?php
$student = array(“anas”=>array(“0700901055”,”computer science”) , “ahmad”=>array(“0700902034”,”computer information system”, “Third year”) );
echo “anas information”.$student[‘anas’][‘0’].” “.$student[‘anas’][‘1’];
?>

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

Best Wishes,
Anas Jaghoub

Comments

Popular posts from this blog

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

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

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

سلسلة تعلم برمجة تطبيقات الاندرويد - إعداد بيئة العمل - الحلقة رقم 1

في هذه الحلقة سنقوم بتجهيز بيئة العمل، وإعداد كافة الأدوات والبيئة اللازمة للبدء في برمجة تطبيقات الاندرويد، لنتمكن في الحلقات القادمة من العمل والتطوير باستخدام الأدوات التي سنقوم بإعدادها في هذه الحلقة. 1. للبدء في تطوير أي برنامج على الاندرويد لا بد من توفر حزمة التطوير الأساسية وهي Android SDK . هذه الحزمة هي حجر الأساس لعمل أي تطبيق على الاندرويد حيث تحتوي على مجموعة كبيرة من الوظائف الاساسية والبرامج التي تمكننا من التحدث مع نظام التشغيل الاندرويد، وطلب تنفيذ العمليات والحسابات إلى غيره من الوظائف بحسب طبيعة كل تطبيق ووظائفه. وتمكننا أيضا من إنشاء أجهزة افتراضية Virtual Machines تساعدنا على تجربة التطبيقات حتى بدون وجود جهاز اندرويد  2. حزمة الجافا JDK والتي تقوم بترجمة النص البرمجي لحزمة الاندرويد وكذلك النص البرمجي للتطبيق وتحويله إلى نسخة تنفيذية قابلة للعمل على أي جهاز اندرويد 3. بيئة العمل IDE وفي هذه السلسلة سنقوم باستخدام Netbeans وذلك لسهولة استخدامه وسهولة التطبيق عليه. كما يمكن أيضا استخدام بيئة العمل Eclipse . ولغايات تبسيط هذه الحلقة و...