Presentation is loading. Please wait.

Presentation is loading. Please wait.

Number and String Operations

Similar presentations


Presentation on theme: "Number and String Operations"— Presentation transcript:

1 Number and String Operations
BIS1523 – Lecture 6

2 Mathematical Operators
The following mathematical operators may be used in PHP to perform calculations: + : addition - : subtraction * : multiplication / : division **: exponentiation These may be used with numeric literals, or variables Example: 5 * 3 10 + 6 $sum / $count

3 Assigning to Variables
Normally, we will store the results of calculations in variables, so that we can print them out later. $sum = ; $average = $sum / 3; You can also print out mathematical expressions directly, as long as you do not use double quotes print ; print $sum / 3;

4 Precedence Mathematical expressions are not necessarily performed “left to right”. For example the expression: $tempC – 32 * 5 / 9 In “left to right” order, it would do the subtraction first, then the multiplication, then the division. Programming languages, however, have a ‘precedence’ that follows a specific order of resolution for math: Exponentiation first Multiplication & Division next Addition and Subtraction last So in the above example, 32 * 5 / 9 would be done first, then subtracted from the value of $tempC

5 Parenthesis Rather than try and remember the precedence on longer mathematical formulas, we can use parenthesis to specify what order we want our calculations to occur Inner most parenthesis are executed first. So in our previous example, if we wanted to execute the subtraction first, we enclose it in parenthesis ($tempC – 32) * 5 / 9 We can nest parenthesis as much as needed: 3 * (5 + 2 * (15 – 3)) The result of the above is: is done first, the inner most parenthesis (result is 12) Then 2 * 12 is executed, since within a set of parenthesis multiplication goes before addition Then Then 3 * 29

6 Program Flow In our standard HTML/PHP programming design, we will normally take the following steps: Read in the inputs from HTML into variables (INPUT) Perform the calculations needed to calculate our outputs (PROCESS) Print out our results Example program with one input: The form calls “results.php”, the input is called “temp-in”

7 Temperature Converter
Output for the previous example: The Code:

8 Incrementing & Decrementing
Some additional mathematical operators exist to perform common tasks. Two of these are incrementing (adding 1), and decrementing (subtracting 1) from a number The shortcut operators are ++ and – To add 1 to the variable “$count”: $count++; Likewise, to subtract 1: $count--;

9 Arithmetic Assignments
There is also a special operator used to compute sums or totals. We often want to take the value of a variable, add some value to it, and store the result in the same place. The += operator does this. $total += $someamount; The above command would take the value of total, add the value of the variable $someamount, and store the result in $total. So if for example $total was 10, and $someamount was 5, the end result would be $total would have the value 15. (The value of $someamount is unchanged) The assignments -=, *= and /= also exist, but are used less often.

10 Formatting Numbers We can use the number_format function to limit how many decimal places are printed for numbers. There are several ways to use this function for output (some covered later). We are going to start by using it to just round a variable off to a specified number of decimal places. number_format takes 2 parameters, the first is a variable (or number), and the second is how many decimal places to use. The above command would take the value of total, round it to 2 decimal places, then store the result back in $total (changing its value.)

11 Comments Sometimes it is helpful to be able to document our code so when we look at it later, we can be reminded of what, why, or how we did something. The mechanism for this internal documentation is called “Comments”, and can be done one of two ways in PHP A pair of double slashes tells PHP to ignore the rest of this line. So if I do something like: $count++; // This is where I add 1 to count. The typing past the double slashes is ignored. I can write whatever I wish there, as a note to myself later.

12 Multi Line Comments For comments spanning multiple lines, I can use the /* and */ opening and closing tags. /* marks the start of a comment, and */ marks the end, everything between will be ignored, even over multiple lines. Similarly I could do:

13 Printing much HTML with one Print
The PRINT command ignores line breaks within the double quotes. This means that you can output a lot of HTML in one print command, rather than having a print command for each line. In a previous example, we had output done like this: This could also be written as:

14 Calculator Example Let’s step through a full program from start to finish, using all the tools we have discussed so far. We start with a list of information we need to read in from the user, and create an HTML form that allows the user to enter it.

15 The Input Form Enclose the entire input section in a <form>. The action attribute points to the program to run when form is submitted. Have an <input> for each thing we need to read in. Include some text to let the user know what they are typing in. Each input will also have a name attribute that will be used by our PHP to get the values Include a submit button

16 Results Our PHP program should print out a list of results.
Some of these values are just repeating what the user typed in, but total cost, and monthly payment are calculated We will follow our normal formula for the program: Input Process Output

17 Input For each input the user entered, read them into a variable.
It’s okay to use the same names as the inputs. Variables in PHP always start with $’s.

18 Processing Next we do all of our calculations, as well as applying formatting We wait and apply formatting after all of our calculations are complete, so we don’t introduce rounding errors. We also didn’t format the inputs, assuming that the user didn’t enter them in some bad format.

19 Output Using the print command, we format the output using HTML breaks to print it on separate lines. PHP says variables start with dollar signs and then have a letter. So if there are 2 dollar signs in a row, the first one must just be a dollar sign for printing.

20 Concatenation You can use the period operator to concatenate 2 strings together. You will notice that this would be the same as the command: The first example is more commonly seen in coding today, but either way works. One good use for concatenation is to format numbers as we print them out. You can’t use number_format() inside the double quotes, but you could use it as part of a concatenation

21 Concatenation & Formatting Example
In a previous example, we used number_format() to change the value of variables before printing them out. Using concatenation we can format them as we are printing. In this case, we close the double quotes, concatenate the formatted number, then re-open the double quotes and continue with the output. By not changing the original variable, we haven’t introduced any rounding errors.

22 Printing with Several Prints
If concatenation is to messy to read, you can still always just use multiple print commands to print with number_format.

23 Multiple PHP Sections PHP allows you to have any number of PHP sections, intermingled within your HTML. Each of them should be enclosed within the PHP tags <? … ?> Remember though: Inside PHP tags, only PHP commands, no HTML without it being specifically printed. Outside PHP tags, don’t use PHP commands, as they’ll just appear on the screen as content. When we have a lot of HTML, rather than print it, we can use multiple PHP sections to reduce the amount of stuff we need to put inside the “print” commands.

24 Example For example if we were printing something like this:
We could, instead, just have most of the HTML outside of the PHP tags, and just use a small section of PHP to print the variable $quantity. Everywhere there is a variable, we just start PHP, print it, and end PHP. Remember, white space doesn’t matter. So the PHP tags can be on lines by themselves, or not…..your goal is to make it as readable as possible.

25 Example Continued Using the same process, a larger section would go from: To: Notice: when printing just a variable, double quotes aren’t needed.

26 Example Continued In the full program, notice we have HTML at the top, then we begin our input & process PHP. Then before the output, we close the PHP tags, and fill in the HTML we want for output, opening groups of PHP tags for each variable we want to print.


Download ppt "Number and String Operations"

Similar presentations


Ads by Google