Dreevoo.com | Online Learning and Knowledge Sharing
 
Home | Programs | Programming languages | PHP & MySQL | Start learning PHP - part 2
Guest
Click to view your profile
Topics
Programs
Languages
Recipes
Home
Shortcuts
 
 

Start learning PHP - part 2

Often in PHP you want the same block of code to run over and over again and instead of adding several almost equal lines, you can use loops to perform this task much easier.

 
  Author: podtalje | Version: php.net | 12th February 2013 |  
 
 
1.
 

In the previous PHP lesson we learned  how to output text and how to store values in variables. You can find the lesson on the following link Start learning PHP - part 1

Today we will learn how to store data in a variables in a more compact way through arrays and how to use loops to perform one task several times.

 
 
2.
 

Arrays

A variable can hold a single piece of data. On the other hand an array can hold a string of related data.

The example is below:
<?php

$person[0] = "Jimmy"; 
$person[1] = "Denise"; 
$person[2] = "George"; 
$person[3] = "Monica";

$age["Jimmy"] = 45;
$age["Nicole"] = 32;
$age["George"] = 26;
$age["Denise"] = 15;

echo "The names in the array are: <br /> ";
echo $person[0] . ", ";
echo $person[1] . ", ";
echo $person[2] . ", ";
echo $person[3];
?>


Here we can see that variables can be accessed by index or by name.

 
 
3.
 

Foreach

Instead of using echo for every single variable in an array, a much better choice is to use foreach loop statement.

<?php
$person[0] = "Jimmy"; 
$person[1] = "Denise"; 
$person[2] = "George"; 
$person[3] = "Monica";

foreach ($person as $p) {
  echo $p." ";
}
?>


This code will go through all the elements in an array and output the values.

 
 
4.
 

For

The next very useful loop statement that you will use a lot in PHP is for statement.

<?php
for ($i=0; $i<10; $i++) {
  echo "Loop number: $i <br />";
}
?>


This code will execute the echo command ten times.
<br /> tag in HTML means new line.

 
 
5.
 

While

There is also another very common statement that you will use if you don't know the exact number of iterations needed and that is while statement.

<?php
$i=0;
while ($i<10) {

  echo "Loop number: $i <br />"; 
  $i++;
}
?>


The code inside while loop will be executed while the condition is true. In our case while value of varible $i is less then 10.

 
 
6.
 

Do...while

You can also use a similar loop statement by using syntax do...while and the end result should be the same. Although in most of the cases the loop statements mentioned before will be much more useful.

<?php
$i=0;
do {
  echo "Loop number: $i  <br />";
  $i++;
}

while ($i<10);
?>

Loop statements from this lesson are very important in PHP and you will use them a lot. So try it yourself and make a few examples to fully understand how they work.

 
 
 
   
  Please login to post a comment
  Click to open user profile  
Billbong, 12th Feb 2013, 1:36 AM
Keep them coming, thank you!
 
 
  Click to open user profile  
Ascocrist, 23rd Sep 2013, 4:49 AM
Hy there!I hope i'm not late with the question, but i noticed the dots used in section 2. Arrays in the echo commands, also in section 3. Foreach in the end of the echo command. What is the purpose or role of the dots in php?Great tuts, thank you!
 
 
  Click to open user profile  
podtalje, 23rd Sep 2013, 3:02 PM
Don't worry, you are not late. We are always here.

To answer your question, dot in PHP is operator for concatenating strings.

To give you a simple example:
$a = "Hello ";
$b = "world";
$c = $a . $b; // the value of $c variable is now "Hello world"

In the lesson in step 2, the dot operator is used just for better formatting of output text by appending ", " after the names. Otherwise it is not necessary.

I hope you now get the idea on how to use dot operator. If you have any additional questions just let me know and I will do my best to answer you.



 
 
  Click to open user profile  
Ascocrist, 24th Sep 2013, 4:36 AM
Yes, now i understand, thank you!

I've started to learn php yesterday :D so i'm a huge question mark with arms and legs, but thanks to this awesome tut, i actually understand what i'm seeing.
Thanks again

 
 
  Click to open user profile  
Ascocrist, 24th Sep 2013, 4:45 AM
I was looking at the other php tutorials but they are way above my level.
Could you point me to more tutorials, similar to yours?

Thank you!
 
 
  Click to open user profile  
podtalje, 24th Sep 2013, 5:32 AM
You can try this one:
http://www.w3schools.com/php/

I would also suggest that when you learn something new, you always try this in practice by creating a simple example.

If you have any additional questions just let me know.
 
 
  Click to open user profile  
Ascocrist, 24th Sep 2013, 10:57 PM
Thank you!

If i'll come across any problems, i'll surely ask

Thanks again!
 
   
 
 
online learning made for people
Dreevoo.com | CONTRIBUTE | FORUM | INFO