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

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