15.04.2018 Views

programming-for-dummies

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Creating Functions 505<br />

If you don’t know how many times you need to repeat commands, use a<br />

WHILE loop, which looks like this:<br />

Book V<br />

Chapter 4<br />

while (condition) {<br />

Command;<br />

}<br />

If the condition is True, the loop runs at least once. If this condition is<br />

False, the loop doesn’t run.<br />

PHP<br />

Somewhere inside a WHILE loop, you must have a command that can change<br />

the condition from True to False; otherwise the loop will never end and<br />

your program will appear to hang or freeze.<br />

Creating Functions<br />

To break up <strong>programming</strong> problems, you can create subprograms that solve<br />

a specific task. Such subprograms are functions. The <strong>for</strong>mat of a typical function<br />

looks like this:<br />

function functionname (Parameter list)<br />

{<br />

Commands;<br />

return $value;<br />

}<br />

The two parts of a PHP function are<br />

Parameter list: Defines any data that the function needs to work. If the<br />

function doesn’t need to accept any values, the parameter list can be<br />

empty.<br />

Return: Defines a value to return.<br />

If a function doesn’t return a value or accept any parameters, it might look<br />

like this:<br />

function myfunction ()<br />

{<br />

Command;<br />

}

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!