You are on page 1of 1

Chapter 5 PHP Arithmetic operators and pre-defined functions (and String functions)

(Chapter 4) More complex PHP Data handlers Arrays,Hashes and Functions


All languages of any repute and worth now contain all of these program data storage handlers in some form of them.

PHP Arrays

1.Single Dimension.

What is an array?, an array is alike to variable, a reference to memory in can be stored data and
retrieved by calling the named reference but in this form of it the storage name it is associate to a
numeric index(the position in the array). The index number in an array is used to reference a part
of the array that behaves exactly as a variable. Arrays such as this always start with the index 0 (zero).
If i were to store all those previous script html strings in an array it would be declared alike to below
of will appear a little complicated but i have not explained it and will do so following it.

<?php
################# html-content-array.php ######################
$my_page_title="Arrays and Hash Array demonstration";
$my_contentline_a = "below is some test text";
$my_contentline_b = 'HERE IS SOME TEST TEXT IN THE HEAD TAGS';
$my_contentline_c = "This is a third line of test text";
$my_contentline_d = 'WITH AN HTML BREAK LINE $html_page_parts[6] BEFORE THIS.';
$html_page_parts = array(
("<html>"."\n"."<br>"."<head>"."\n"."<br>"."<meta name=\"generator\" content=\"nicephot.xlphp.net php tutorial\">"."<title>"),
('</title></head>'."\n".'<body>'."\n".'<center><p>'."\n"."<br>"."\n"),
("\n"."<p>"."\n"),
("\n"."</p>"."\n"),
("\n"."<h2>"."\n"),
("\n"."</h2>"."\n"),
("\n"."<br>"."\n"),
('</p></center></body></html>')
);
# $html_page_parts[0] is the html tags and start of the page to the first title tag
# $html_page_parts[1] is from the end of the title tag pair to the content formatting tag in the visible page body
# $html_page_parts[2] is a start paragraph tag
# $html_page_parts[3] is an end
# $html_page_parts[4] is a text content header start tag
# $html_page_parts[5] is a text content header end tag
# $html_page_parts[6] is a break line(html new line) tag
# $html_page_parts[7] is a page end tag
$count=0;
while($count<5):
if($count==0){
print($html_page_parts[$count]);
}elseif($count==1){
print($my_page_title.$html_page_parts[$count]);
}elseif($count==2){ print($html_page_parts[$count]);
}elseif($count==3){
print($html_page_parts[2].$my_contentline_a.$html_page_parts[($count+1)].$my_contentline_b.$html_page_parts[($count+2)]);
}elseif($count==4){ print($my_contentline_c.$html_page_parts[($count+2)].$my_contentline_d);
}else{
print($html_page_parts[7]);
}
$count++;
endwhile;
?>

First the $html_page_parts = array(); declares an a array and in the previous shown array it is different to this
one in this text. The one shown has each of its memory indexes assigned (or else known as declared and
assigned). When the array is as shown as in this text (a couple of lines above) it is to be used as a
dynamic array meaning it will use some special functions to assign and declare both indexes and
values to the indexes. You must however (as variables) declare them in the script for them to exist. The only
alike to a variable being created when not declared in the script is above in the demonstration script.
Look at the variable $count (the incremented counter value memorising variable) and the array index call that
appears to this $html_page_parts[($count+2)]. This section [($count+2)] is the array index number associate
holder(the two square brackets) and inside is a pair of parenthesis with $count+2. The $count+2 does not
increment/add 2 to the instance value of $count either permanently or temporarily but it does create the value
of the mathematical equation in the parenthesis, and that then represents that completed value with no
reference name for that fraction of script at that point only in it. Parenthesis are operators and are low
precedence operators(meaning they are not particularly important in assessing first but will be assessed).
Parenthesis operators are used in many different types of programming languages and language parts and
are specifically to commit grouping of actions as is also demonstrated inside the array indexes above to make
the string text in the array one unit not a set of confusing joints.That makes it safer to look at for both the
programmer and the runtime script engines interpreter. The array above is also a single dimension array.

2.Multi Dimension.

A multi-dimensional array in PHP can be declared similar as above using the array() function or implicitly.Since the
later to be described(hash array) is similar in PHP to the single dimension system either can be declared with much
the same syntax of array().

A multi-dimensional array can be either a hash or standard array of singular numeric indexes. The declaration is much
as any array and has various shortcut syntax that can be found in the PHP manual.

<?php
################# multi-dim-array.php #################
$ttle="Multi dimensional PHP array by indexes</title>";
$page_set=array(
array(
"<html><head>"."\n".'<title>'.$ttle.'</head><bo'.'dy><center>',
'<h2>',
'</h2>',
'<p>',
'</center></body></html>'
),
array(
'First line of content in multi dimensional array $page_set[1][0]',
'Second line of content $page_set[1][1]',
'Last two lines of content $page_set[1][2]<br>The internal array $page_set[0][0 to 4] are the HTML tags'
)
);
for($out=0;$out< 3;$out++){
if($out==0){
print($page_set[0][0].$page_set[0][1].$page_set[1][0].$page_set[0][2]);
}elseif($out==1){
print($page_set[0][3].$page_set[1][1]);
}elseif($out==2){
print($page_set[0][3].$page_set[1][2].$page_set[0][4]);
}else{}
}
?>

PHP Hash Arrays

Hash Arrays are not quite the same as a normal array system above but similar enough to convert a normal array to a
hash array.Later. The real difference is that both the indexing referral system and some requirements are compulsory
for a hash array to operate.First the array does not simply store a singular value in its index but two and they are not
referred to by numeric indexing and treated always as a pair. The pair of values are referred to in programming as
a key and value pair. For a value to be present a key is compulsorily present,when a value is added then the array length+1
is added as the key and the value is associated to it automatically in PHP, Also PHP key data type for a hash must be either
integer or string. A key assigned as a float will be demoted to an integer.
Unlike PERL , PHP does not use the value undefined but usually returns a boolean value or integer and also has many
pre-defined functions where code hacking and rules are the method to produce the result in PERL. e.g. array_pad() ,
array_walk() , unset() . To get the length of an array in PHP the count() function is used.
Two more things about hashes now, hashes do not have a proper numeric index order until sorted and secondly are
usually iterated in a special loop called a foreach(). Foreach loop system was not mentioned on the program flow control
page and neither was the while() loop but both are as much common as the for() loop in scripting use.
note: when printing an array out print_r() can do a rough unformatted printout of an array.

<?php
############## hasharrayset.php ####################
$setpair = array("a is equal to" => 0,"b is equal to" => 1,"c is equal to" => 2,"d is equal to" => 3,"e is equal to" => 4);
function hshdo($rdo){
foreach($rdo as $ke => $xnm){
print("The key: ".$ke." :The value: ".$xnm."<br>"."\n");
}
}
#
foreach($setpair as $nm){
print("value is:".$nm."<br>"."\n");
}
print('<p>next with the keys<p>');
foreach($setpair as $key => $nm){
print("The key: ".$key." :The value: ".$nm."<br>"."\n");
}
$redo=array_flip($setpair);
print('<p>swapped key for value order array_flip()<p>');
hshdo($redo);
$redo=array_reverse($redo,false);
print('<p>swapped position for value order array_flip()<p>');
hshdo($redo);
print('<p>print out previous with print_r() the bare data output function for arrays<p>');
print_r($redo);
?>

Functions(user defined) and Arguments

User defined functions are all but uninteresting since some loonier actions such as recursion call
re-inside a function can occur and calling non existent functions. But for the basic purpose here
it is best to show the simpler and more useful versions of user defined functions and there call
systems/semantics.
First it is possible to return an array from a function. Secondly it is wise to use a return statement
for complex data(e.g arrays) and numeric values.Third, arguments can be any of the variable/reference
that exist to store data in the script program. A function is defined by the function keyword and
followed by its name, its name is used later to activate it at calling time in program sequence.
Variables required to be passed to the function must be placed in the parenthesis that immediately
follows the function name.Last, functions also have a pair of curly braces around the code, a left
facing to start and a right facing to end and the last line is for the return statement if a return
statement is required.

<?php
######################## functions.php ##########################
/* declare the global script variables */
$conter=1;
$dec=10;
/* incrementation - decrementation function */
function contUP($decrease){
$point=($decrease-1);
$dec=$point;
return $dec;
}
/* start html tags */
function prnStart(){
print('<html><head><title>Functions in PHP</title></head><body><center>');
}
/* end html tags */
function prnEnd(){
print('</center></body></html>');
}
#
prnStart();
#
while($conter<11){
$dec++;
$dec-=($conter++);
print('<p> $dec '.$dec.' :func: '.contUP($dec). ' $conter: '.$conter.' / $dec '.$dec.'</p>'."\n\n");
}
#
prnEnd();
#
?>

You might also like