unknown time to read, 1.78K views since 2016.10.09

Functions

As functions in PHP7 got some core changes, in this article we would working with php 5 functions.

Functions is asort of a subprogram. Functions used in languages to exclude situations of copy-paste code. So when you havea part of code which is pretty the same in two or more parts of your program you probably should move your codeto a function and instead of using the whole code, just call the function on that place.

Also, functions helps to separate blocks of code which is used for different tasks.

declaring a function

All functions in php has same declaration:


// this function prints "hello world"
function someName($someParameter) {
    // function body
    // here you can do some actions

    echo "hello world";
}

Each function has its own variable visibility scope - this means that inside function body you can't access variables outside this function. On the other hand you can't access variables which are declared in function body from outside that function.

After you had declared the function you can use it everywhere you want:


someName(); // hello world

Each function should have unique name - two functions in one scope named equally will produce fatal error and broke your app.

Overcoming visibility scope

parameters

To pass some data to function's scope there are function parameters. In php, function parameters could have default value and be not required in that case.

In most cases you should declare all function parameters at function declaration, but also you can handle dynamically parameters to functions. Dynamic parameters are out ofthe scope of this article, so we woun't toucn them at all.

Lets define our first parameter at function:


function sayHello($toName) {
    echo 'hello, '.$toName;
}

// now we can pass parameters to our function

sayHello('John'); // hello, John
sayHello('Bob'); // hello, Bob

We've just provided different data to function's scope by setting $toName parameter to John first and then to Bob in second function call.

global variables

You can use variables that are defined in global scope (simply on the script, not within some function) without passing them as parameters. To access global variable in function scope you should declare such variables as global in function body:


// this is global variable
$count = 1;

function increaeCount(){
    global $count; // list of variables (separated hy comma) from global scope which can be accessed in this function 
    $count++;
}

increaseCount(); // $count = 2
increaseCount(); // $count = 3

echo $count; // 3

getting data from functions

To get some data from function you can use two ways:

returning some data from function

Each function can return a value in result. You can imagine it like each function call is substituted with return value at the end of function execution. Example:

$x2 = pow(2); // assign return value of pow(power) function to $x2 variable

to return some data from function you should use return keyword:

function makeHello($name){
    return 'hello, '.$name;
}

Now function makeHello returns string with greetings instead of printing it as in previous function sayHello. return statement breaks function execution and return a value - code after statement wouldn't be executed. Function could return only one value. You can return whatever you want from a function.

change data outside function's scope

By using global variables you can make changes outside function data. It's not always a good decision, because it's not obvious to track dependencies between your functions. Functions which cause changes in variables that are not provided as parameters called functions with side effects.

Conclusion

Using functions is a first step to write more structurized and beautiful code. Always divide your code to functions. Each function should have one responsibility. Don't write functions that can do a lot of things depending of its parameters.

Read next article How superglobal variables can help you in course Basic PHP