PHP Functions
PHP Built-in FunctionsA function is a self-accommodate block of code that hang a specific task.
PHP has a huge collection of internal or built-in functions that you can call directly within your PHP scripts to perform a specific task, like gettype(), print_r(), var_dump, etc.Get more knowledge about PHP functions with
PHP Training in Chandigarh
PHP User-Defined Functions
In addition to the built-in functions, PHP also grants you to explain your own functions. It is a way to create reusable code bottle that execute particular tasks and can be stored and continue independently from main program. Here are some improvements of using functions:
Functions reduces the recurrence of code within a program — Function allows you to extract commonly used block of code into a single basic. Now you can functions the same task by calling this perform anywhere you want in period your writing without acquire to copy and paste the same block of code again and again.
Functions makes the code much simpler to continue — Since a function created once can be used many times, so any adjustment made central a function naturally achieve at all the apartments without stunning the definite files.
Functions makes it easier to eliminate the errors — When the program is subdivided into functions, if any error occur you know absolutely what function create the glitch and where to find it. Therefore, adapting fault becomes much easier.
Functions can be restate in different application — Because a function is detached from the rest of the writing, it's simple to restate the same behavior in other function just by including the php file containing those functions.
The following section will show you how easily you can define your own function in PHP. For learning practically go for PHP Training in Chandigarh.
Passing Arguments to a Function by Reference
In PHP there are two action you can gap arguments to a function: by value and by reference. By default, function arguments are passed by value so that if the value of the argument within the function is changed, it does not get affected outside of the function. However, to allow a function to modify its arguments, they must be moved by reference.
<?php
/* Defining a function that multiply a number
by itself and return the new value */
function selfMultiply(&$number){
$number *= $number;
return $number;
}
$mynum = 5;
echo $mynum; // Outputs: 5
selfMultiply($mynum);
echo $mynum; // Outputs: 25
?>
Understanding the Variable Scope
However, you can declare the variables anywhere in a PHP script. But, the location of the declaration determines the extent of a variable's visibility within the PHP program i.e. where the variable can be used or accessed. This accessibility is known as variable scope.
By default, variables declared within a function are local and they cannot be viewed or manipulated from outside of that function, as demonstrated in the example below:
<?php
// Defining function
function test(){
$greet = "Hello World!";
echo $greet;
}
test(); // Outputs: Hello World!
echo $greet; // Generate undefined variable error
?>
PHP Training in Chandigarh is the best place for learning PHP Functions ,variables framework ,content management and other by Cbitss Technologies.
0 comments:
Post a Comment