PHP Functions
A function is just a name we give to a block of code that can be executed whenever we need it. This might not seem like that big of an idea, but believe me, when you understand and use functions you will be able to save a ton of time and write code that is much more readable!
Simple PHP Function
Note: Your function name can start with a letter or underscore "_", but not a number! A simple PHP function that writes Price of the article
Above code output is given below
Passing parameters inside PHP Functions
Another useful thing about functions is that you can send them information that the function can then use. Look at the below example,
The above example will describe clearly that how to use parameters inside php functions. we are using the same function in two different places with different parameters in our program. Here the function work is bind the three parameters and print onthe screen. The aim of the function is to simplify the developers work. Note:Parameters is nothing but an arguments.
Return values from PHP Functions
Sometimes you may want to call a function and have a value returned back to the main coding area. This is done using the return command.
The output of the above program is:
Leaving the return command out, the result would be blank. In the above example, the variable $total calls the function entering two arguments. The function takes the numbers and finds the sum. The sum value is "returned" back to the calling variable $total.
A function is just a name we give to a block of code that can be executed whenever we need it. This might not seem like that big of an idea, but believe me, when you understand and use functions you will be able to save a ton of time and write code that is much more readable!
Simple PHP Function
function functionName() { execution code; }
Note: Your function name can start with a letter or underscore "_", but not a number! A simple PHP function that writes Price of the article
<html> <body> <?php function getPrice() { echo "TWO HUNDRED and TWENTY "; } echo "Article price is"; getPrice(); ?> </body> </html>
Above code output is given below
Article price is TWO HUNDRED and TWENTY
Passing parameters inside PHP Functions
Another useful thing about functions is that you can send them information that the function can then use. Look at the below example,
<?php function print_it_out($first_part,$second_part,$third_part){ echo "$first_part $second_part $third_part"; } $part1 = "Welcome"; $part2 = "to the"; $part3 = "u3schools"; print_it_out($part1,$part2,$part3); $part4 = "This"; $part5 = "is"; $part6 = "online web tutorial"; print_it_out($part4,$part5,$part6); ?>
The above example will describe clearly that how to use parameters inside php functions. we are using the same function in two different places with different parameters in our program. Here the function work is bind the three parameters and print onthe screen. The aim of the function is to simplify the developers work. Note:Parameters is nothing but an arguments.
Return values from PHP Functions
Sometimes you may want to call a function and have a value returned back to the main coding area. This is done using the return command.
<?php function addit($first_number,$second_number){ $total_sum = $first_number + $second_number; return $total_sum; } $first_number = "3"; $second_number ="2"; $total = addit($first_number,$second_number); echo "$total"; ?>
The output of the above program is:
5
Leaving the return command out, the result would be blank. In the above example, the variable $total calls the function entering two arguments. The function takes the numbers and finds the sum. The sum value is "returned" back to the calling variable $total.
0 comments:
Post a Comment