Skip to main content

Custom Function in PHP

Tutorial Title: Custom Function in PHP
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.
function anyName(){
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. 
function anyName($number1, $number2){
$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

  • 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
define("constantName", 1000);
echo constantName;

Comments

Popular posts from this blog

External Page Submission/Submission to another page

Tutorial Title:   External Page Submission/Submission to another page Course: PHP Instructor: Muhammad Samim 1. Submitting action from one page to another page 2. First make one page with name page1.php <!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">     <title>Document</title> </head> <body>      <form action="page2.php" method="post"> //It will direct from page1.php to page2.php           <br><input type="text" placeholder="Email" name="anyNameForEmail">     <br><input type="password" placeholder="Password" name="anyNameForPassword">     <br><input type="submit" name="click">    </form>   </body> </html> 3. Second make another file with name page2.php <?php if(isset($_POST["click"])){     $anyNameO...

Extracting Information from form

Tutorial Title:  Extracting Information from form Course: PHP Instructor: Muhammad Samim 1. <?php if(isset($_POST["anyName"])) { $anyNameOfVariableForEmail = $_POST["anyNameForEmail"]; //Now because of it email must be typed otherwise it will not work $anyNameOfVariableForPassword = $_POST["anyNameForPassord"];//Now because of it password must be typed otherwise it will not work echo $anyNameOfVariableForEmail; //It will display the email echo $anyNameOfVariableForPassword; //It will display the password } ?> <!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">     <title>Document</title> </head> <body>      <form action="21form.php" method="post">        <input type="text" placeholder="Email" name="anyNameForEmail">        <input type="password" placeholder="...