- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming»
- Programming Languages
PHP Tutorial: A Beginners Guide in Different PHP Loop, Aray and Function
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
}
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?
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.
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" );
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
}
Other PHP Sample Codes and Tutorials
- How to become a PHP Developer: Learn and Earn
- How to run PHP Code or PHP Scripts and See the Result on Your Browser
- HTML Basics: Learn The Basics of HTML and Create Your Own Webpage on Your Browser
- PHP Code Example on Associative Array and Multidimensional Array
- PHP Sample Codes On Function:How to make Function in PHP
- PHP code Example for if statement
- PHP Code Example for if-else Statement
- PHP Code Example on if else-if Statement
- PHP Code Example on Switch Statement
- PHP Code Example on While Loop
- PHP Code Example on Numeric Array
- PHP Code Example on Do-while Loop
- PHP Code Example on Foreach Loop and For Loop