Search
 
SCRIPT & CODE EXAMPLE
 

PHP

how to define function in php

<?php
function writeMsg() {
    echo "Hello world!";
}

writeMsg(); //call the function
?>
Comment

create function php

<?php
  #Syntax:
  function functionName() {
    code to be executed;
  }

  function Greetings() {
    echo "Hello, welcome!";
  }
  
  Greetings(); #Execute function 

  #Function arguements

  #Function arguements are just like prefixes/suffixes and can have multiple arguements

  function Polygon(str $prefix = penta, str $suffix = gon) {
    echo "$prefix$suffix"
  }

  Polygon("tetra", "gon");
  Polygon(); #Here, we use default value
  Polygon("hexa", "gon");
  Polygon("septa", "gon");
?>
Comment

define in php

//define() is used to create constants
define(name,value);
//here name has to be a string
//here value can be string, integer, float, boolean or NULL, 
//and can be an array to if you are using PHP 7.0+

//define() before php 7.3
define(name,value,case_insensitive);
//case_insensitive is optional and can be TRUE or FALSE by default its false
Comment

function in php

<?php
//am defining funciton here

function checkingResults($name, $marks){
    echo "hi ".$name;
    echo "<br/>";
    echo "Your total score: ".$marks;
}

checkingResults("denim", 125);
?>
Comment

function in php

                                                   // Function


function myFunctions($fname) {        // By using parameters we only need to pass arguments and we can only change parameter when we needed and print result. For example 
    echo $fname."<br>";                // if we make a function of sum and we want to change only values and want a result, thats why we use functions with parameters.
                                       // Here fname is a parameter. They also called formal parameters.
 }

 function myFunction($x, $y) {         // here x and y are formal parameters.
   int s;
   $s = $x+$y;
   return $x+$y;                     // 5+3 = 8   
   return s;                         // return 8 to the main function where it is invoked. And we can use the return value 
                                       // in main for any opration.
 }


                          // Compiler first read always main function OR where function is calling.

   myFunctions("Liam");                // Liam, Jenny and Anja are arguments. They also called actual
   myFunctions("Jenny");               // parameters. They passed their values to formal parameters.
   myFunctions("Anja");              
 
   echo "Enter two numbers";
   myFunction($a,$b);                  // values of a and b which user has been entered pass to formal parameter x and y in myFunction. After procssing in function the return value come in main function and cout(print) that return value.Here we passed a and b are calleed arguments. They also called actual parameters.
   //OR
   $c = myFunction($a,$b);             // We can store the value 8 which is return from myFunction in c variable and use it for further any operation.
   echo $c;                            // Now c = 8;
   //OR
   myFunction(5, 3);                   // return value of s from function arrive here and then print the answer.          
  


                               // Return Function

 We use return function when we have to use the value of that return function in main. If we not return value
 it can only use in that function. We cannot return more than one value from function.                                                



function sum($mth,$eng,$phy){
     $s = $mth+$eng+$phy;
     return $s;
 }

 function percentage ($totals){
     $per = $totals/300*100;
     return $per; 
 }

 $total = sum(80,85,90);

 echo $total. "<br>";

 $percentageNum = percentage($total);

 echo $percentageNum. "%";



                                        // Recursive Function


 function fact($n){                  // first n is 5. then 4,3,2,1
     if($n>1){
         return $n*fact($n-1);       // this is recursive statement which called fact function again and again
     }                               // with also reduce the value of n to 4,3,2,1;
     else{
         return $n;
         // return 1;             // At last 1 return to fact in return fact;
     }
 }

      $f = 5;
      echo fact($f);      // this f argument which is actual parameter is passed to function formal parameter
                     // of function fact. let f is 5.



 Step by step calculation of factorial(4)
 factorial(4) = 4 * factorial(3); 
 factorial(4) = 4 * 3 * factorial(2);
 factorial(4) = 4 * 3 * 2 * factorial(1);
 factorial(4) = 4 * 3 * 2 * 1;
 factorial(4) = 24;

 
Comment

define php

define("name", {value});
Comment

php function

 $myarray = array_filter($myarray, 'strlen');  //removes null values but leaves "0"
 $myarray = array_filter($myarray);            //removes all null values
Comment

define function in php

<?php

function addTwoNumbers($number1, $number2){
    echo "Result: ",$number1+$number2;
}

addTwoNumbers(100, 125);
?>
Comment

php function use

// Inherit $message
$example = function () use ($message) {
    var_dump($message);
};
$example();
Comment

PHP Functions

<?php
function writeMsg() {
  echo "Hello world!";
}

writeMsg(); // call the function
?>
Comment

PREVIOUS NEXT
Code Example
Php :: function to find the mod of a number in php 
Php :: what is Trustproxies handle in laravel 
Php :: laravel skip a loop if error 
Php :: php distinct 
Php :: laravel echo 
Php :: send email verification nootification laravel 
Php :: php DateTime only date 
Php :: laravel use cache 
Php :: id type laravel 
Php :: docker compose php 
Php :: create laravel update 
Php :: laravel project composer [ErrorException] Undefined index: name 
Php :: laravel seeder update 
Php :: laravel @env 
Php :: how to lookup value inside object php 
Php :: run cron job in seconds 
Php :: how to add drop a table in phpmyadmin 
Php :: laravel property 
Php :: adminlte con laravel 8 
Php :: send data to api php 
Php :: Add Custom Field to woocommerce Subscriptions 
Php :: #FF0000; 
Php :: php mysql delete from multiple tables 
Php :: validate phone number with dial code laravel 8 
Php :: digitalocean php as text 
Php :: cpt change link 
Php :: php replace all text from string with associate array values 
Php :: php tools for visual studio package did not load correctly 
Php :: Change Initial Country For Caldera Forms Phone Fields 
Php :: php code for english translation optin 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =