PHP has many built-in functions. For example, strtoupper(), this function is to convert string to uppercase, another example is abs(), which is to return a value to be absolute.
But we can also define our own function using statement:
function name_of_function($argument1, $argument2)
{
//function code here
}
The name of the function follows the function statement and precedes a set of parentheses. If your function requires arguments, you must place comma-separated variable names within the parentheses. These variables will be filled by the values passed to your function. Even if your function doesn't require arguments, you must nevertheless supply the parentheses.
By the way, The naming rules for functions are similar to the naming rules for variables, "The Building Blocks of PHP." Names cannot include spaces, and they must begin with a letter or an underscore. As with variables, your function names should be meaningful as well as consistent in style. The capitalization of function names is one such stylistic touch you can add to your code; using mixed case in names, such as myFunction() or handleSomeDifficultTask(), makes your code much easier to read.
see the example below:
<?php
function bold($word){
print("<b>$word</b>");
}
function italic($word) {
print("<i>$word</i>");
}
function underline($word) {
print("<u>$word</u>");
}
bold("Good Morning...");echo("<br/>");
italic("Good Afternoon...");echo("<br/>");
underline("Good Evening...");echo("<br/>");
?>
save it as function.php and open from your browser, the result should be like;
Good Morning...
Good Afternoon...
Good Evening...
all the functions above expect a string as a paramater, so we place the variable name $word between the parentheses when we declare the function. Whatever is passed to all functions will be stored in this $word variable. Within the body of the functions,we print the $word variable, appending a <b>...</b> element to "bold function", <i>...</i> to "italic function" and <u>...</u> to "underline function".
No comments:
Post a Comment