PHP Functions: Scope Variable, Passing Parameter, Default, Parameters By reference, Returns

PHP functions

PHP Functions:

PHP Function has, like many languages, the ability to group portions of code in the form of functions (or procedures). If the definition of functions has no impact on the operation of a program, and therefore if it is possible to build its development like a series of instructions placed end to end, it provides visibility and source code size reduction. Maintenance is then only easier and faster. The PHP Functions  allowing structuring the code, it is strongly advised to use them and to create independent files, which will group together functions of the same type. These libraries can easily be reused in other developments and reduce thus the development time (and therefore the production cost). There are therefore three advantages to writing code as PHP Functions :

  • Development time is considerably reduced. In fact, you don’t have to retype identical code sequences in various parts of the program. Of the same way, the bugs are not duplicated, which saves considerable time during the focus.
  • Better readability of the source code. The simple act of grouping the code into functions and functions in separate files allows developers to navigate very easily. In addition, giving your functions meaningful names is complementary with the addition of comments, and necessarily facilitates the readability and understanding of the program. This is all the more important for the sustainability of a code. Thus, maintenance and modifications of the program are facilitated even for a developer using his own code some time after writing it.
  • Teamwork is improved. A development can thus be divided into several sub-parts, each developer then having the task of developing one or more PHP Functions .

If the use of PHP Functions  is a definite advantage, a preliminary design step remains necessary in order to clearly define the functions that will have to be developed and to target their respective roles. Most importantly, the preliminary step is to set the entry rules / output of each function, this so that each developer knows in advance the parameters it should send to the function and what it should return after processing performed. If the PHP Functions  are known from the start, then everyone can start to understand them. use in its code even before putting said functions into production.

Syntax of PHP Functions:

The function is a subroutine isolated from the rest of the code and usable by a simple call from any part of the program. The syntax for declaring a simple function is as follows:

This code displays nothing until it is called like this:

PHP functions

The previous case is a good example of a function declaration and use of it at inside the main program. We can see the benefit of the definition of the function in case the task of it is repetitive and should generate a lot of source code.

Note: Choosing the name of a function To make your code easier to read, don’t hesitate to give your functions a evocative name. As with variables, reading your program is facilitated if these elements have explicit names. If the program functions are called function1(), function2() and function3(), maintenance does not will be that more difficult. Do not give your PHP Functions  a name that is too cryptic or that can have several meanings. Avoid function names that are too short for them to be not used more than once in your program. For example, for a function that must count a number of people present on a web page, do not call it not just counter(), but rather onlinePersonCounter().

Scope variables:

As we said, a function is nothing more than a piece of code isolated from the program main. It is therefore no surprise that you will learn that it is possible to define variables within its functions. On the other hand, what must be remembered is that the variables thus defined are not accessible from the rest of the program (they have a local scope), like the demonstrate the following example:

PHP functions

Conversely, a variable defined outside the function (in the main program) is not accessible from the function.

PHP functions

To use global variables in the function, you must use the “global” keyword at inside this function, as the following example shows:

PHP functions

As you can see, the changes made to these global variables within the function reverberate outside the function. There is another way to use global variables from within a function. This method is to use the predefined array $GLOBALS directly. The previous example would then give:

PHP functions

The scope of a variable therefore depends on where it is initialized. The scope of a variable will not be the same if it is defined in a function or at the start of your program. We can consider three levels of definition.

The different levels of the definition of variables


The variables are specific to each function. As soon as the execution of this function, the variable is destroyed and the memory space that contained the value is released.


Global variables are defined for the entire execution time of the code of the page.


Static variables are specific to each function, but they are not destroyed at the end of the performance of this function. If the function is called back multiple times, the value of the variable is kept and changed each time.

We therefore have to deal with the case of static variables. These variables are initialized at first call of the function and are not reset the following times. To create a static variable, you must use the static keyword followed by the name of the variable.

Example of use :

This will indeed display:

Fawad khan Is that you?

Fawad khan Is that you?It’s you?

Fawad khan Is that you?It’s you? It’s you?


Initialization of a static variable

It is not possible to initialize a static variable with the result of a function. However, you can work around this problem by assigning this variable a value that it is supposed to never reach, and call the function if ever the variable takes this value.

Passing Parameters or Arguments in Php Functions:

You will often need to request parameters for your functions. For that, it is enough to complete the function declaration line with a list of variable names. The variables thus defined are only accessible inside the function; they are therefore variables of local visibility.

PHP functions

Default settings:

You can give your function default settings. So if you don’t come home no value when calling this function in your code, a default value is used in order to perform the function. To set a default value, you simply need to specify this value using the sign “=” when declaring the function. Like this :

This function may or may not receive a value as a parameter. If it’s not the case, then this takes, by default, the value “hello”. So there are two ways to call this function in the main program.

PHP functions

The fact that the PHP Functions  require a priori a fixed number of arguments may ask you one day a problem. But there are two ways to deal with it:

  • Using an array, if your function accepts only one argument but this one is array type, you can put as many arguments as you want.
  • By using the functions made available by PHP (which makes it akin to using an array created by PHP).

Passing parameters or Arguments by reference in Php Functions:

When we pass a variable as a parameter to a function, the transmitted variable has only one local scope, and the variable manipulated within the function is in fact only a copy of the variable transmitted by the calling program. Thus, the execution of the function does not entail no modification on the transmitted variable. When you want the function to modify the value of the variable passed as a parameter, this must be passed not by value but by reference. When a variable is passed as a reference, the new local variable thus created points to the same memory area, but with another name than the parameter. Thus, we have an alias of the variable that is created in the function, and any changes to it will be reflected in the original. However, it is not the caller who decides whether the variable should be passed by value or by reference, but the function. Thus, to use a reference, it suffices simply to indicate the function parameter with an “&” in front of the variable name.

PHP functions

In the previous example, we can see that the changes made to the local variable $reference affect the global variable $phrase.

Return a value in Php Functions:

You will undoubtedly want to retrieve the content of a variable following the processing of a function. To return a variable and thus terminate the execution of the function, the PHP language uses the return keyword. When this instruction is encountered, the function evaluates the value of the following variable and returns it to the main program.

Example of use :

If it is possible to place more than one return statement inside a function, the first return executed terminates the execution of the function.

PHP functions

The return statement can only return one value. If you want to return several values, two possibilities are available to you:

  • Return an array of values ​​or an object with multiple attributes.
  • Use several parameters passed by reference (which will therefore serve as more return value as input value); possibly, the function can also return a value via return.

It is not really possible to say a priori which is the right method. It’s a bit of a case by case. The first then requires analyzing the content of a table (and the use of an object is not always justified). The second method is not entirely satisfactory, since we mixture of input parameters and output parameters; conversely, it allows to reserve the return value for a boolean indicating whether the operation went well or not. Here is an example using a table showing how to get out of this problem, which you will no doubt come across in your career as a developer.

Here, the function returns an array of values. It is therefore possible to process the table and output the different data, as if the function had returned us several variables. The result of the small program above therefore gives us:

PHP functions

Built in PHP Functions :

Built in PHP Functions  will be functions that exist in PHP installation package. These built in PHP Functions  are what make PHP an exceptionally effective and profitable scripting language. The built in PHP Functions  can be grouped into numerous classes. The following is the rundown of the classifications.

String PHP Functions :

These are PHP Functions  that control string information, allude to the article on strings for execution instances of string PHP Functions .

Numeric PHP Functions :

Numeric PHP Functions  are function that return numeric outcomes.

Numeric php function can be utilized to format numbers, return constants, perform numerical calculations and so forth.

Common numeric PHP Functions :

Function Description Example Output
is_number Acknowledges an argument and returns true if its numeric and false if it’s most certainly not <?php



echo “true”;




echo “false”;





if(is_numeric (444444))


echo “true”;




echo “false”;



number_format This php function is Used to formats a numeric value using separators. <?php

echo number_format(123456);


rand This php function is Used to generate a random number. <?php

echo rand();


Random number
round This php function is used to take(round off) the nearest whole number with decimal point. <?php

echo round(2.233);


sqrt This php function is to take the square root of a given number <?php

echo sqrt(144);


cos This php function is to find the cosine value of a given number <?php

echo cos(50);


sin This php function is to find the sine value of a given number <?php

echo sin(70);


tan This php function is to find the tangent value of a given number <?php

echo tan(20);


pi This php function is used to find the value of PI which is constant <?php

echo pi();




Date PHP Functions:

The date php function is utilized to format Unix date and time to human-readable format.

Php Functions Related Article:

what is PHP? and write your first hello world program

Related Articles

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button