Intro to Functions

What if, to print prime numbers(upto 50) 5 times in a program, we write the same logic 5 times. Hard, right? Here comes the need of functions. So, with the use of functions we can access(call) the same logic as many times as we need in our whole program.

Let me first code some in-built PHP functions-

In the above code, date() and phpinfo() are the two functions. The parentheses in the function tell PHP that you are referring to a function, otherwise it would be a constant. Like this, there are hundreds of in-built functions.


Defining a Function

The general syntax of function is-

The first line of the syntax indicates the following:

  1. A definition starts with the word function.
  2. A name follows, which must start with a letter or underscore, followed by any number of letters, numbers, or underscores.
  3. The parentheses are required.
  4. One or more parameters, separated by commas.

Some important points to note are-

function names are case insensitive i.e.print() or PrInt() or pRint() all are same.

The opening curly brace starts the statements that will execute when you call the function.

a matching curly brace must close it. These statements may include one or more return statements, which force the function to cease execution and return to the calling code.

If a value is attached to the return statement, the calling code can retrieve it, as we’ll see next.


Returning a value

Let’s understand this with a very simple example. We’ll write a program that converts a person’s name in lowercase and then capitalize first letter of each part of the name. Don’t worry we have built-in functions for these tasks-

strtolower()- takes a string and returns the string as lowercase.

ucfirst()- it sets first character of a string to upper case and returns the converted string.

So, above code will output "Kane williamson" (First letter of name is capital, rest are in lowercase).

Now, let’s define a function that takes three names and makes each one lowercase, with an initial capital letter. The code for this is below-

I have combined both the functions in one expression- ucfirst(strtolower($n1)) .


Returning an array

Let’s re-code above code –

Here, I have used an in-built function array() , it takes elements as parameters and returns an array. I have stored the return value from the function fix_names() in the variable $names, which is now an array name and then I have printed the elements of array using their index number, separated by space.


Passing arguments by reference

Look at this code carefully, CONCENTRATE…

The concept of reference is same as in C or C++. I will not go deep in it. Reference is an address of a variable, so when we change the value of a particular variable through its address, initial value is changed.


Returning Global variables

Following example explains this-

The global keyword followed by the variable name gives every part of your code full access to it. The global keyword followed by the variable name gives every part of your code full access to it.


THAT’S IT

One thought on “PHP- Functions

Leave a comment