PHP - Part 2 - Functions

PHP - Part 2 - Functions - CNT 4714: Enterprise Computing...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
CNT 4714: PHP – Part 2 Page 1 Dr. Mark Llewellyn © CNT 4714: Enterprise Computing Spring 2011 Introduction to PHP – Part 2 Department of Electrical Engineering and Computer Science University of Central Florida Instructor : Dr. Mark Llewellyn [email protected] HEC 236, 407-823-2790 http://www.cs.ucf.edu/courses/cnt4714/spr2011
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CNT 4714: PHP – Part 2 Page 2 Dr. Mark Llewellyn © Functions In PHP Functions are at the heart of a well-organized script and will make your code easy to read and reuse. Large projects would be unmanageable without functions because the problem of repetitive code would bog down the development process. If you haven’t had any experience using functions, you can think of a function as an input/output machine. The machine takes the raw materials you feed it (the input) and works with them to produce a product (the output). A function accepts values, processes them, and then performs an action (printing to the browser, for example), returns a new value, or both.
Background image of page 2
CNT 4714: PHP – Part 2 Page 3 Dr. Mark Llewellyn © Functions In PHP If you need to bake a cake, you would probably do it yourself, in your own kitchen with your oven. But if you need to bake thousands of cakes, you would probably build or acquire a special cake-baking machine, built for making cakes in massive quantities. Similarly, when deciding whether to create a function for reuse, the most important factor to consider is the extent to which it can save you from writing repetitive code. If the code you are writing will be used more than once, it is probably best to create a function to represent the code.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CNT 4714: PHP – Part 2 Page 4 Dr. Mark Llewellyn © Functions In PHP A function is a self-contained block of code that can be called by your script. When called (or invoked), the function’s code is executed and performs a particular task. You can pass values to a function (called arguments), which then uses the values appropriately – storing them, transforming them, displaying them, whatever the function is designed to do. When finished, a function can also pass a value back to the original code that called it into action. In PHP, functions come in two flavors – those built in to the language, and those that you define yourself.
Background image of page 4
CNT 4714: PHP – Part 2 Page 5 Dr. Mark Llewellyn © Functions In PHP PHP has hundreds of built-in functions. Consider the following example that utilizes the built-in function strtoupper() .
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CNT 4714: PHP – Part 2 Page 6 Dr. Mark Llewellyn © Functions In PHP In the previous example, the function strtoupper() is called and passed a variable whose value is represented by a string. The function goes about its business of changing the contents of the string to uppercase letters. A function call consists of the function name followed by
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/14/2011 for the course CNT 4714 taught by Professor Llewellyn during the Spring '10 term at University of Central Florida.

Page1 / 37

PHP - Part 2 - Functions - CNT 4714: Enterprise Computing...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online