PHP Functions - Adding parameters - Full PHP Master

Learn Php & Make Base For Development.

Thursday 15 February 2018

PHP Functions - Adding parameters

PHP Functions - Adding parameters 
To add more functionality to a function, we can add parameters. A parameter is just like a variable. 
Parameters are specified after the function name, inside the parentheses. 
Example 1 
The following example will write different first names, but equal last name: 

<html>
 <body>
  <?php 
   function writeName($fname)
 {
 echo $fname . " Refsnes.<br />"; 
    }
  echo "My name is "; 
      writeName("Kai Jim");
 echo "My sister's name is "; 
      writeName("Hege");
 echo "My brother's name is "; 
      writeName("Stale"); 
   ?>
  </body>
 </html>  

Output: 
My name is Kai Jim Refsnes. My sister's name is Hege Refsnes. My brother's name is Stale Refsnes.  
Example 2 
The following function has two parameters: 

<html>
 <body>
  <?php
 function writeName($fname,$punctuation)
 {
 echo $fname . " Refsnes" . $punctuation . "<br />";
 } 
 echo "My name is ";
 writeName("Kai Jim","."); 
     echo "My sister's name is ";
 writeName("Hege","!");
 echo "My brother's name is ";
 writeName("StÃ¥le","?");
 ?>  
 </body>
</html>  

Output: 
My name is Kai Jim Refsnes. My sister's name is Hege Refsnes! My brother's name is StÃ¥le Refsnes?  
   
Shape 
PHP Functions - Return values 
To let a function return a value, use the return statement. 
Example 

<html>
 <body>
  <?php function add($x,$y)
 {
 $total=$x+$y;
 return $total;
 }
  echo "1 + 16 = " . add(1,16);
 ?>
  </body>
</html>  

Output: 
1 + 16 = 17 


No comments:

Post a Comment