PHP Programing language

adplus-dvertising
PHP FUNCTION
Previous Home Next

A function is using in PHP just a name and it's give to a block of code that can be executed whenever we need it. This is not to seem like that big of an idea, but if you know that How can using Function then you will be able to save a maximum of time and write code that is much more readable.

Example:

you are in a company , and your company motto that you have to displaying at least once on every webpage. If you not handle this situation then you get fired, Well, being the PHP programmer you are, you think to yourself, "then you handle this situation for using a functions."

Note: Although functions are often thought of as an advanced topic for beginning programmers to learn, if you take it slow and stick with it, functions can be just minor speed bump in your programming career. So don't give up if you functions confuse you at first.

Create a First program in PHP using Function

If you want to create a program for function in PHP. Then first need a Company Name  Like as Company Name. It's with this function name that you will be able to call upon your function, so make it easy to type and understand. Then the actual syntax for create a function is pretty self-explanatory, but you can be the judge of that. First, you must tell PHP that you want to create a function. You do this by typing the keyword function followed by your function name and some other stuff.

<?php
function Company Name(){
}
?>

Note: Your function name can start with a letter or underscore "_", but not a number.We want our function to print out the company motto each time it's called, so that sounds like it's a job for the echo command.

<?php
function CompanyName()
{
echo "r4rtechsoft!<br />";
}
?>

How can use Function in PHP

Now that you have completed coding your PHP function, it's time to put it through a test run. Below is a simple PHP script. Let's do two things: add the function code to it and use the function twice.

PHP Code:

<?php
echo "welcome to r4rtechsoft <br />";
echo "it's more valuable site <br />";
echo "it's educational site <br />";
?>

PHP Code with Function

<?php
function CompanyName()
{
echo "r4rtechsoft!<br />";
}
CompanyName();
echo "welcome to r4rtechsoft <br />";
echo "it's more valuable site <br />";
echo "it's educational site<br />";
CompanyName();
?>

The rule of Function using:

  1. Always start your function with the keyword function
  2. Remember that your function's code must be between the "{" and the "}"
  3. Then you are using your function, be sure you spell the function name correctly
  4. Don't give up!
Previous Home Next