> Online tutorial : php array
Showing posts with label php array. Show all posts
Showing posts with label php array. Show all posts

echo and print

The echo() and print() function are used to output the given argument.it can output all types single and multiple outputs can be made with these command
Syntax
int print(string $arg)
void echo (string $arg1[,string $ ,,])
Similarities
They both language constructs (not function)so they can be used without parentheses
Example


<?php
echo "welcome to india":
print "welcome to india";

?>

Output:

PHP directory

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

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()