PHP Arrays and Functions
PHP Arrays and Functions
ARRAY
An array is a special variable, which can hold more
than one value at a time.
In PHP, the array() is used to create an array:
array();
In PHP we have 3 types of arrays:
1. Indexed arrays - Arrays with a numeric index
<?php
$stuff = array("Hi", "There");
echo $stuff[1], "\n";
?>
There
INTEGER INDICES
<?php
$stuff = array();
$stuff[] = "Hello";
$stuff[] = "World";
SI664
DUMPING AN ARRAY
• The function print_r() dumps out PHP data - it is
used mostly for debugging
<?php
$stuff = array("name" =>
"Chuck",
"course"
=> "SI664");
print_r($stuff); Array
?> (
[name] => Chuck
[course] => SI664
)
DUMPING AN ARRAY
• The function print_r() dumps out PHP data - it is
used mostly for debugging
<?php
$stuff = array();
$stuff[2] = "Hello";
$stuff[9] = "World";
print_r($stuff);
?> Array
(
[2] => Chuck
[9] => SI664
)
VAR_DUMP .VS. PRINT_R
<?php
$stuff = array("name" => "Chuck",
"course" => "SI664");
var_dump($stuff);
?>
array(2) {
["name"]=>
string(5) "Chuck"
["course"]=>
string(5) "SI664"
}
http://stackoverflow.com/questions/3406171/php-var-dump-
vs-print-r
LOOPING THROUGH AN ARRAY
<?php
$stuff = array("name" =>
"Chuck",
"course" =>
“Web Design 2");
foreach($stuff as $k => $v
) {
echo "Key=",$k,"
Val=",$v,"\n";
}
?>
Key=name Val=Chuck
Key=course Val= Web Design 2
LOOPING THROUGH AN ARRAY
<?php
$colors= array(“red", “yellow",
“black");
$a_length = count($colors);
or other arrays.
CIS
ARRAY FUNCTIONS
• count($ar) - How many elements in an array
• is_array($ar) - Returns TRUE if a variable is an
array
• isset($ar['key']) - Returns TRUE if key is set in the
array
• sort($ar) - Sorts the array values (loses key)
• ksort($ar) - Sorts the array by key
• asort($ar) - Sorts array by value, keeping key
association
• shuffle($ar) - Shuffles the array into random order
$za = array(); Count: 2
$za["name"] = "Chuck"; $za Is an
$za["course"] = “WD2";
print "Count: " . count($za) . "\n";
array
if ( is_array($za) ) { name is
echo '$za Is an array' . "\n"; set
} addr is
else {
echo '$za Is not an array' .
not set
"\n";
}
in an array
what is the difference between:
empty() ,is_null() ,isset() unset()
FUNCTIONS?
• A function is a piece of code in a larger program. The
function performs a specific task.
• PHP has lots of built-in functions that we use all the time
• We write out own functions when our code reaches a
certain level of complexity.
• The advantages of using functions are:
Reducing duplication of code
Decomposing complex problems into simpler pieces.
Reuse of code
BUILT-IN FUNCTIONS...
$val = 10;
triple($val); Triple = 30
echo "Triple = $val\n";
VARIABLE SCOPE
In general, variable names used inside of function
code, do not mix with the variables outside of the
function. They are walled-off from the rest of the code.
This is done because you want to avoid "unexpected"
side effects if two programmers use the same variable
name in different parts of the code.
We call this "name spacing" the variables. The
function variables are in one "name space" whilst the
main variables are in another "name space“
function try1() {
$val = 100;
LOCAL VARIABLES } Try1=
Local variables are variables that are created within, and
10
$val = 10;
can be accessed only by, a function. try1();
One set of local variables is the list of arguments to a echo "Try1 = $val\n";
function.
function add() {
$GLOBALS['s'] = $GLOBALS['m1'] +
$GLOBALS['m2'];
}
add();
echo $s, $GLOBALS['s'];
?>
output 100 100
<?php
$name= ‘Ahmad’;
Function display()
{
Global $name;
Return ‘my name is ‘.$name;
}
Echo display();
?>
We will write the previous example in other way using
$GLOBALS
<?php
Function display()
{
Return ‘my name is ‘.$GLOBALS [name];
}
$name = ‘Ahmad’;
Echo display();
?>
$_POST
PHP $_POST is widely used to collect form data after
submitting an HTML form with method="post".
$_POST is also widely used to pass variables.
The browser client can sends information to the web
server through POST.
OUTPUT
<?php
Name ali echo '
<form action="welcome.php" method="post">
Password **** Name : <input type="text" name="name"><br>
Password: <input type=“password"
name="password"><br>
submit <input type="submit">
</form> '
?> ;
<?php
echo "wellcome ",$_POST['name'],"<br />";
welcome ali
echo " your password is :",$_POST['password'],"<br />";
your password is :ali2 ?>
PHP $_GET
PHP $_GET can also be used to collect form data after
submitting an HTML form with method="get".
$_GET can also collect data sent in the URL.
OUTPUT
Words
Search
Search
http://localhost/test/get1.php?getdata=ali
NOTES
$_GET is an array of variables passed to the current
script via the URL parameters.