Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions BIS1523 – Lecture 17.

Similar presentations


Presentation on theme: "Functions BIS1523 – Lecture 17."— Presentation transcript:

1 Functions BIS1523 – Lecture 17

2 Inline vs Block Elements
There are two basic element types in HTML, Inline and Block. A block element is an element that takes up the full width available, and has a line break before and after it. Example: <h1> An inline element only takes up as much width as necessary, and does not force line breaks. Example: <a>

3 Display In CSS, we can change the way elements are displayed (inline vs block), by using the display attribute. For example, if we add CSS and set the <h1> tags to be inline instead of block: They are displayed like this:

4 Inline-block A third type of display is inline-block. Normally inline elements aren’t given a specific width, they take on whatever width their contents need. An inline-block, however, can be sized to a specific width, while still appearing inline (with content to the left and right, and no line breaks before and after). We used this formatting in our sales-program last class period, to create columns of output. We also used the SPAN element to give the sections of text a class.

5 Span We could also use Spans to do other types of formatting in PHP.
Basically, we just mark the text with the SPAN tag and give it a class (or name), and then have matching CSS. For example, let’s say we wanted to mark prices in our sales program from last week with color codes, based on the price of the item. Over 30$ are blue Under 10$ are green First, we create CSS declarations for these types

6 Example Then we add code to add the span tags in our program, based on price

7 Example The HTML generated looks like:
Alternatively we could color code the entire line, by wrapping every column inside that color-coded span.

8 Example The resulting HTML:
Because the “blue” class encloses all of the other spans, they all inherit that “color” attribute, and get blue text.

9 Functions We’ve used several functions in this course:
explode() number_format() We also have the ability to create our own functions in PHP. We can create several different kinds of functions: Simple functions that have no parameters, and that do not return any values Functions that have parameters, but do not return any values Functions that return values (with or without parameters)

10 Simple Functions The syntax for creating a user-defined function is:
For example, we could create a simple function like: You can put any amount of code inside a function.

11 Function You call a function by using its name.
When we call a function, all of the code within that function is executed as if it had been typed right on the calling line. For Example: Each iteration of the loop, it calls the function, and so you would get the word “whatever” printed out each time.

12 Uses for Simple Functions
Occasionally, we have the same lines over and over in our code. In the previous sales example, we had 3 print statements that looked like: Notice the highlighted sections are identical. We could simplify our program slightly, by putting those lines in a function

13 Simple Function Example
This doesn’t quite work, due to something called variable scope

14 Variable Scope Normally, variables are only visible within the section of code they are contained in. When we create a function, we are creating a new ‘container’, so the variables within its scope don’t exist anywhere else. In the previous example, the variables used in our function, $i and $products exist outside the function, but not inside. The resulting output is incorrect:

15 Global command We can tell a function to look outside itself for variable values with the global command. In our example, we want to access $i and $products from outside the function, so we do this: Once we do that, our function works correctly.

16 Functions with Parameters
Another way we can access values in functions is to pass them as parameters. A parameter is a value we send to a function. The explode() and number_format() functions both used parameters. When we did number_format($somevar, 2), we sent it 2 values, the value of the variable, as well as the number 2. The function used those values to format the value correctly. We can add parameters to our functions as well by including them in the declaration:

17 Functions with Parameters
The, when we called our function, we would include the values we wanted to pass to it. The variable names don’t have to be the same, we declared the function as:

18 Further Simplifying Example
We can simplify our code even further by including the color as a parameter. If you examine our existing code, you can see the only difference in the prints is the word green or blue.

19 Simplified Code

20 Return Values Functions without return values are normally used as a stand-alone command. They are called by putting the function name on a line by itself. A function that returns a value, however, is used wherever that value could be used. Our number_format function, for example, is not used on a line by itself, but rather as part of a print command

21 Return Values To have a function return a value, we use the “return” Command. For example, we could write a function that, given a $price, returns the appropriate word for the color that line should be in our code:

22 Simplifing Code Again Notice there’s really not much difference in our calls to print_qty_and_product, other than the color name. Instead, we could replace that with calls to our color function, and our final loop code looks like:

23 Final Code

24 More Function Examples
More function examples can be found at:


Download ppt "Functions BIS1523 – Lecture 17."

Similar presentations


Ads by Google