{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This 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
Image of page 1

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

View Full Document Right 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.
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.
Image of page 3

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

View Full Document Right 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.
Image of page 4