You are on page 1of 1

Chapter 6 PHP File I/O and String Regular Expression engine

(Chapter 5) PHP Arithmetic operators and predefined functions (and String functions)

Arithmetic constants and Properties

One of the major actions of computing is mathematics. Maths is difficult and accuracy is of real world
value in anything now whether it be a simple add and subtract site shopping cart monetary total or a
more valid problem of unit conversion of some type of measurement of bickies whether large quantities
of money in an exchange rate ration of standard economic foreign units.
So nothing now changes for this programming language(PHP) as any of its predecessors e.g. PERL C/C++ Shell.
All of these have three R's of computing maths,R predefined functions for trigonometry, R Coefficient constants,
R standard fractional computer architecture typing e.g. Integer , Float.
In computing a constant is variable to which a value cannot be re-assigned but either only initialised or as
below, pre-set by the language script engine by both semantic syntax and reference name.
The variable M_PI below is not changeable and is the value of PI, the coefficient for circle geometry.
M_PI 3.14159265358979323846 Pi
another
is the Euler constant that comes from fractional maths of numerator/denominator system.
M_EULER 0.57721566490153286061
and again as would be there are logarithms.

Arithmetic operators

Arithmetic operators are along the same again for most languages
the star * is the multiplication operator
$nine=3*3;
the forward slash / operator is the division operator
$four=12/3; the plus sign + operator is for addition
$ten=5+5;
the minus sign(hyphen) - is the subtraction operator
$seven=10-3;
and the modulus % sign(percent sign) is the remainder of a division
$zero=12/4;
This now comes to operator precedence being of importance and being
difficult to discern some errors in maths until identified e.g.
4+3*7/7 if you were to do this from left to right as you found the
numbers you would get 7 but if you do it by precedence using parenthesis
you would get 7 a completely different way 4+(3*7)/7 because of them
being grouping operators , and a completely different one algebraically
by precedence and associatively with no parenthesis by the PHP.
This is better explained by the PHP Manual's example but that's effectively it.
As i told you before, operator precedence has some part in maths and to
achieve better synchronisis of equation sequences the grouping operator
parenthesis can be used to cause internal declarations of code to be
managed as a single entity and not simply inline or by plain declaration
precedence.

<html>
<head>
<!-- predictate.php -->
<title>PHP Engine, precence , algebraic evaluation logic examples</title>
</head>
<body>
<center>
<?php
print('Inline forced calculation by left to right evaluation of: 4 + 3 x 7 / 7'."\n\n".'<br>');
$bg=4+3;
print("4+3 is: ".$bg.'<br>');
$c=$bg*7;
print("".$bg." x 7 is: ".$c.'<br>');
$fnl=$c/7;
print("".$c." / 7 is: ".$fnl.'<br>');
print("final result is: ".$fnl.'<p><p>');
#
#
#
print('Precedence grouping forced by ( ) parenthesis of 4 + 3 x 7 / 7<br>by this formula 4 + (3 x 7) / 7<br>');
$parenth=4+(3*7)/7;
print("The final result is: ".$parenth.'<p><p>');
#
#
#
print('Inline ungrouped precedence calculated by PHP engine of 4 + 3 x 7 / 7'.'<br>');
$phpinline=4+3*7/7;
print("The final result is: ".$phpinline.'<p><p>');
#
print('Precedence grouping forced by ( ) parenthesis of 4 + 3 x 7 / 7<br>by this formula 4 + (3 x 7 / 7)<br>');
$phpinline_2=4+(3*7/7);
print("The final result is: ".$phpinline_2.'<p><p>');
#
print('Precedence grouping forced by ( ) parenthesis of 4 + 3 x 7 / 7<br>by this formula 4 + 3 x (7 / 7)<br>');
$phpinline_3=4+3*(7/7);
print("The final result is: ".$phpinline_3.'<p><p><p><p>');
#
print('Precedence Inline of 4 + 3 + 9 x 7 / 7<br>');
$phpinline_4=4+3+9*7/7;
print("The final result is: ".$phpinline_4.'<p><p>');
#
print('Precedence grouping forced by ( ) parenthesis of 4 + 3 + 9 x 7 / 7<br>by this formula 4 +( 3 + 9 x 7) / 7<br>');
$phpinline_4=4+(3+9*7)/7;
print("The final result is a floating point number(decimal fraction): ".$phpinline_4.'<p><p>');
#
?>
</center>
</body>
</html>

Pre-Defined Math functions in PHP

Standard expectable programming language trigonometric functions exist in PHP.


One of the less usual is rand() used to generate random numbers.rand takes a
minimum and a maximum number for its arguments and outputs thatever comes of it.
hypot() returns the length of the hypotenuse.
ceil() for rounding up.

String functions/operations

At present and now is the final time/moment gone to leave this to, i will explain some basic standard string handling functions.
These are extremely important because much of anything www and web servers have and alike to the http and ftp networking protocol
is text whether data or commands. Earlier in this tutorial you found text and maths are unavoidable at some point because of how
computers operate and how that operation needs to be interfaced to most possibly and at some point a human user. Strings are made
of individual characters and without regard to byte size of singular or pairs to make a string it has a length. consider if as you
learned before that windows has the new line 4 byte character and we put with it to make a reference to a string one more character,
that would make 3 characters and a length of three would it?. Answer NO.Only two. Two, because new line is for purpose one
character but better as one symbolic representation,meaning it only represents a new line and nothing else. In most scripting
languages it is not the operational method of data typing to be extremely definitive of the data unlike C/C++ because a string while
it is an array of characters does not actually change its data type to a character except during exceptive and deliberate programmer
controlled chosen situations and is never actually a character data type.One single character is always a string not a char.When
handling strings it is often required to know the length strlen() of it because they are often operated on in a for() loop from
start to finnish,moreover they are often required to have two(a pair) not one set of positions in the string being related and worked
upon in the one iteration sequence of the loop(a start and an end of another relevant string). The position of strings absolute start
is always zero and it end is always its length minus -1(one).
These type of functions are always found when string
handling. strlen() , substr() ltrim() rtrim().

<?php
############### stringfuncs.php ######################
$desc=("Below each character is a substring printed on a new line.<p>"."\n\n");
$text="The shorter piece of string is a substring";
$lenstr=strlen($text); # get thenumber of chars to stop the for loop
print($desc);
for($i=0;$i<$lenstr;$i++){
$sbs=substr($text,$i,1); ## count along the string and remove each char then print it
print($sbs."<br>"."\n");
}
## a simple type of regular expression system is occuring with str_replace() function
$changestr='<p>'.str_replace("Below","Above",$desc);
print($changestr);
?>

You might also like