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
-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.
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.
Codes to here
For loop is used if the programmer wants to repeat the block of codes for specified number of times.
for (initialized variable; condition; increment)
For each loop is used if the programmer want to execute codes that would loop through a block of arrays.
Foreach($array as $value)
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.
This is an array which stores values in a numeric index.
$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 = "Apple";
"Toshiba" is on the index 1 or equivalent to
$compter="Toshiba"; and so on.
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 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"));
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.
function myFunctionName(with or without parameters 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