Tutorial Title: Custom Function in PHP
Course: PHP
Instructor: Muhammad Samim
1. Defining Functions in PHP
echo "Powered by Muhammad Samim";
}
//it will not be shown until we don't call
anyName(); //Displays whatever function contains
2. Passing Parameters Functions in PHP
function anyName($number1, $number2){
$sum = $number1 + $number2;
echo $sum;
}
anyName(300,60.55); //These two numbers will be passed through variables $number1 and $number2 and will be added to be stored in variable $sum to be displayed.
3. Returning Values from Functions
$sum = $number1 + $number2;
return $sum;
}
$result = anyName(300,400);
echo $result . "<br>"; //Here we can use the stored value of result
$result = anyName(400,$result); //400 will be added with the old value of $result
echo $result;
4. Global and Local Scope Variables
Course: PHP
Instructor: Muhammad Samim
1. Defining Functions in PHP
- User defined function is a piece of program that can be used calling by its name. It saves a lot of time and makes easier to code.
echo "Powered by Muhammad Samim";
}
//it will not be shown until we don't call
anyName(); //Displays whatever function contains
2. Passing Parameters Functions in PHP
function anyName($number1, $number2){
$sum = $number1 + $number2;
echo $sum;
}
anyName(300,60.55); //These two numbers will be passed through variables $number1 and $number2 and will be added to be stored in variable $sum to be displayed.
3. Returning Values from Functions
- Using keyword return of PHP in function to make it more flexible.
$sum = $number1 + $number2;
return $sum;
}
$result = anyName(300,400);
echo $result . "<br>"; //Here we can use the stored value of result
$result = anyName(400,$result); //400 will be added with the old value of $result
echo $result;
4. Global and Local Scope Variables
- Creating variable outside the function is global variable which can be used inside or outside the function
- Creating variable inside the function is local variable which can not be accessed outside the function.
- To use or access the local variable outside the function we use keyword global with variable inside the function.
$y = "outside";
function anyName(){
$y= "inside";
}
echo $y; //Variable $y value is "outside"
echo "<br>";
anyName();
echo $y; //Variable $y value is still "outside" even after calling function because of local variable which is not accessible from outside.
//Two times "outside" will be displayed because from outside its not accessible in order to access we need to use keyword global with variable $y inside the function.
$y = "outside";
function anyName(){
global $y;
$y= "inside";
}
echo $y; //Variable $y value is "outside"
echo "<br>";
anyName(); //
echo $y; //Now variable $y value is changed to "inside" after calling function as already inside the function local variable declared as global variable in order to be used outside.
//"outside" and "inside" both will be displayed
5. Constants in PHP
echo constantName;
5. Constants in PHP
- Its used to fix the value of constant to not be changed anyway.
- We use keyword define to declare the constant.
- The way of creating constant is different than creating variable with $ symbol
echo constantName;
Comments
Post a Comment