ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming»
  • Programming Languages

PHP Tutorial: A Beginners Guide in Different PHP Loop, Aray and Function

Updated on April 9, 2013
Source: Drupal.org
Source: Drupal.org

This PHP Tutorial for beginners is a continuation of Steps on how to program on PHP: A beginners guide on PHP programming Part 1 and Part 2. If you did not read it yet, please read it first to facilitate better understanding on this hub.




Steps on how to program on PHP: A beginners guide on PHP programming Part 3

Tackles about:

-PHP Loops and Sample Scripts

-PHP Arrays and Sample Scripts

-PHP Functions and Sample Scripts


PHP Loops (while, do-while loops, for loop and foreach loop)

There are also four kinds of Loops in PHP programming. The while loop, do-while loop, for loop and for each loop.

While Loop is used if the php programmer want to execute a block of code over and over while the condition on the while loop is true.

Syntax:

While(condition)

{

..code here

}

See PHP Code Example on While Loop

Do-while loop is used if the programmer wants to execute the codes once and repeat it again once for every true condition specified in its while condition.

Syntax:

Do

{

Codes to here

}while(condition);

See PHP Code Sample on Do-While Loop

For loop is used if the programmer wants to repeat the block of codes for specified number of times.

Syntax:

for (initialized variable; condition; increment)

{

…code here

}

See PHP Code Example on For Loop

For each loop is used if the programmer want to execute codes that would loop through a block of arrays.

Syntax:

Foreach($array as $value)

{

…code here

}

See PHP Code Example on Foreach Loop


Are You Interested with PHP Programming?

See results

Learn PHP Arrays

What is an Array? Array is used to declare or hold one or more values. If a variable can hold only one value an array can hold more. To use array in PHP, you will use the array() built in function. There are 3 kinds of Arrays in PHP, the Numeric Array, Associative Array and the known Multidimensional Array.

Numeric Array

This is an array which stores values in a numeric index.

Example:

$computers = array("Apple", "Toshiba", "Lenovo", "Acer", "HP");

All the values on the array is stored on a certain index respectively. The first value would be on the index 0. Meaning, the value "Apple" is on the index 0 or equivalent to

$computer[0] = "Apple";

"Toshiba" is on the index 1 or equivalent to

$compter[1]="Toshiba"; and so on.

See PHP code Sample on Numeric Array

Associative Array

Associative array is used if you want to declare a value with associated value. For example:

$computer = array("Apple" => "$500", "Toshiba" => "$350", "Acer" => "$200", "HP" => "$200" );

See PHP Sample Code On Associative Array

Multidimensional Array

Multidimensional array is used to contain one or more arrays inside its body. For example:

$computer = array("Apple" => array("$500", "$750", "$1000"),

"Toshiba" => array("$300", "$350", "$500"));

See Sample Code for Multidimensional Array


Functions in PHP

what is a function and how to make functions in PHP? To answer that, functions is a block code that will just be executed on the server if the function name is called. To make function on php, you must include the word "function" in every function you make.

Syntax:

function myFunctionName(with or without parameters here)

{

...code here

}

See an Example Code for Function

Comments

    0 of 8192 characters used
    Post Comment

    • srsshafiq profile image

      Muhammad Shafiq 2 years ago from Pakistan

      Very informative Php tutorials. I have also make a Php tutorials you see it on official website itvideotutorials.

    • aisha91 profile image
      Author

      Rasna Aisha 5 years ago from Manila, Philippines

      Thanks Dwachira, you are right, I say that this is one of the programming languages I want to be expert of. Nah, Dream on. (smile)

      Thanks for rating it and voting it up.

      Keep well.

    • dwachira profile image

      [ Danson Wachira ] 5 years ago from Nairobi, Kenya

      I find PHP becoming the Web programming language of choice, although programmers complain about the different variations of similar syntax in doing the same thing in PHP, overall PHP is a popular programming language. Useful article, voted up.