> December 2012 ~ Online tutorial

foreach statement in php

foreach Statement:

The foreach statement is used to loop through arrays.
For every loop, the value of the current array element is assigned to $value (and the array pointer is moved by one) so on the next loop, you'll be looking at the next element.
Syntax
foreach (array as value)
{
code to be executed;
}

Example

<html>
<body>
<?php
$arr=array("one", "two", "three");
foreach ($arr as $value)
{
echo "Value: " . $value . "
";
}
?>
</body>
</html>

Output;

one
two 
three

for Statement in php

for Statement
The for statement is used when you know how many times you want to execute a statement or a list of statements.
Syntax
for (initialization; condition; increment)
{
code to be executed;
}

Note:

The for statement has three parameters. The first parameter initializes variables, the second parameter holds the condition, and the third parameter contains the increments required to implement the loop. If more than one variable is included in the initialization or the increment parameter, they should be separated by commas. The condition must evaluate to true or false.
Example

<html>
<body>
<?php
for ($i=1; $i<=5; $i++)
 {
 echo "Hello World!";
}
?>

≤html>
Output
Hello World
Hello World
Hello World
Hello World
Hello World

do...while Statement in php

The do...while Statement
The do...while statement will execute a block of code at least once - it then will repeat the loop as long as a condition is true.
Syntax

do
{
code to be executed;
}
while (condition);



Example
The following example will increment the value of i at least once, and it will continue incrementing
the variable i as long as it has a value of less than 5:



<html>
<body>
<?php
$i=0;
do
{
$i++;
echo "The number is " . $i . " ";
}
while ($i<5)
</body>
</html>

while statement in php

The while Statement
The while statement will execute a block of code if and as long as a condition is true.
Syntax
while (condition)
code to be executed;
Example
The following example demonstrates a loop that will continue to run as long as the variable i is
less than, or equal to 5. i will increase by 1 each time the loop runs:

<html>
<body>
<?php
$i=1;
while($i<=5)
{
echo "The number is " . $i . "
$i++;
}
?>

Looping in php

PHP Looping:

Looping statements in PHP are used to execute the same block of code a specified
number of times.
Looping Very often when you write code, you want the same block of code to run a number of times. You
can use looping statements in your code to perform this.

In PHP we have the following looping statements:

• while - loops through a block of code if and as long as a specified condition is true

• do...while - loops through a block of code once, and then repeats the loop as long as a
special condition is true

• for - loops through a block of code a specified number of times

• foreach - loops through a block of code for each element in an array

Multidimensional Arrays in php

Multidimensional Arrays
In a multidimensional array, each element in the main array can also be an array. And each
element in the sub-array can be an array, and so on.
Example
In this example we create a multidimensional array, with automatically assigned ID keys:

$families = array
(
"Griffin"=>array
(
"Peter",
"Lois",
"Megan"
),
"Quagmire"=>array
(
"Glenn"
),
"Brown"=>array
(
"Cleveland",
"Loretta",
"Junior"
)
);

The array above would look like this if written to the output:


Array
(
[Griffin] => Array
(
[0] => Peter
[1] => Lois
[2] => Megan
)
[Quagmire] => Array
(
[0] => Glenn
)
[Brown] => Array
(
[0] => Cleveland
[1] => Loretta
[2] => Junior
)
)

Associative Array in php

Associative Arrays
An associative array, each ID key is associated with a value.
When storing data about specific named values, a numerical array is not always the best way to
do it.
With associative arrays we can use the values as keys and assign values to them.
Example 1
In this example we use an array to assign ages to the different persons:
$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);
Example 2
This example is the same as example 1, but shows a different way of creating the array:
>

$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";

The ID keys can be used in a script:

<
?php
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
echo "Peter is " . $ages['Peter'] . " years old.";
?>
The code above will output:
Peter is 32 years old.

Numeric array in php

Numeric Arrays
A numeric array stores each element with a numeric ID key.
There are different ways to create a numeric array.
Example 1
In this example the ID key is automatically assigned: PDF by Hans Home Collection

$names = array("Peter","Quagmire","Joe");

Example 2
In this example we assign the ID key manually:



$names[0] = "Peter";
$names[1] = "Quagmire";
$names[2] = "Joe";

The ID keys can be used in a script:


<?php
$names[0] = "Peter";
$names[1] = "Quagmire";
$names[2] = "Joe";
echo $names[1] . " and " . $names[2] .
" are ". $names[0] . "'s neighbors";
?>
The code above will output:
Quagmire and Joe are Peter's neighbors

php array

PHP Arrays
An array stores multiple values in one single variable.What is an Array?
A variable is a storage area holding a number or text. The problem is, a variable will hold only one value.
An array is a special variable, which can store multiple values in one single variable.

If you have a list of items (a list of car names, for example), storing the cars in single variables could
look like this:

$cars1="Saab";
$cars2="Volvo";
$cars3="BMW";
However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300?
The best solution here is to use an array!An array can hold all your variable values under a single name. And you can access the values by referring to the array name.

Type of Array
Numeric Array
Multidimensional Arrays
Associative array
Function in Array
list()
array_sum()
array_unique()
asort()
array_values()
array_push()
count()
in_array()
ksort()

Strpos() function in php

strpos() function
The strpos() function is used to search for character within a string.
If a match is found, this function will return the position of the first match.
If no match is found, it will return FALSE.
Let's see if we can find the string "world" in our string:

< ?php
echo strpos("Hello world!","world");
? >

The output of the code above will be:

6

The position of the string "world" in our string is position 6. The reason that it is 6 (and not 7), is that

strlen() function in php

strlen() function
The strlen() function is used to return the length of a string.
Let's find the length of a string:

<?php
echo strlen("Hello world!");
? >

The output of the code above will be:
12
The length of a string is often used in loops or other functions, when it is important to know when the
string ends. (i.e. in a loop, we would want to stop the loop after the last character in the string).

Concatenation operator in php

The Concatenation Operator
There is only one string operator in PHP.
The concatenation operator (.) is used to put two string values together.
To concatenate two string variables together, use the concatenation operator:

Example

?php
$txt1="Hello World!";
$txt2="What a nice day!";
echo $txt1 . " " . $txt2;
?

The output of the code above will be:
Hello World! What a nice day! 

If we look at the code above you see that we used the concatenation operator two times. This is because
we had to insert a third string (a space character), to separate the two strings.

String in php

PHP String Variables
A string variable is used to store and manipulate text.
String Variables in PHP
String variables are used for values that contains characters.
In this chapter we are going to look at the most common functions and operators used to manipulate
strings in PHP.
After we create a string we can manipulate it. A string can be used directly in a function or it can be stored in a variable.
Below, the PHP script assigns the text "Hello World" to a string variable called $txt:




?php
$txt="Hello World";
echo $txt;
?

The output of the code above will be:
Hello World
Now, lets try to use some different functions and operators to manipulate the string.

Function in String
strops()
strlen
Chunksplit()
addcslashes() and backcslahes()
chop()
chr()
implode()
nl2br()
strrev()
strlen()
substr()
trim()

String Operators in php


 String Operators
As we have already seen in the Echo Lesson, the period "." is used to add two strings together, or more
technically, the period is the concatenation operator for strings. 


PHP Code:


$a_string = "Hello";
$another_string = " Billy";
$new_string = $a_string . $another_string;
echo $new_string . "!";

Display:


Hello Billy!  

Comparison Operators in php


Comparison Operators
Comparisons are used to check the relationship between variables and/or values. If you would like to see
a simple example of a comparison operator in action, check out our If Statement Lesson. Comparison operators are used inside conditional statements and evaluate to either true or false. Here are the most important comparison operators of PHP. 

Assume: $x = 4 and $y = 5; 


Operator English ExampleResult
== Equal To $x == $y false
!= Not Equal To $x != $y true
< Less Than $x < $y true
>Greater Than $x > $y false
<= Less Than and Equal To $x <= $y true
>= Greater Than and Equal To $x >= $y false


Assignment Operators in php


Assignment Operators
Assignment operators are used to set a variable equal to a value or set a variable to another variable's
value. Such an assignment of value is done with the "=", or equal character.

Example

•  $my_var = 4; 
•  $another_var = $my_var  

Now both $my_var and $another_var contain the value 4. Assignments can also be used in conjunction
with arithmetic operators.
  

Php operator

Operators in PHP

In all programming languages, operators are used to manipulate or perform operations on variables and
values. You have already seen the string concatenation operator "." in the Echo Lesson and the assignment
operator "=" in pretty much every PHP example so far.
  There are many operators used in PHP, so we have separated them into the following categories to make
it easier to learn them all.
Concatenation operator
Assignment Operators
Arithmetic Operators
String operator