Advertisement
modimil

Functions (Advanced)

May 1st, 2012
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.85 KB | None | 0 0
  1. http://phpmysql-academy.blogspot.in/
  2. Example: 1
  3.  
  4.     <?php
  5.  
  6.     function calc($num1, $num2, $op)
  7.     {
  8.     switch ($op)
  9.     {
  10.     case "+";
  11.     $total = $num1 + $num2;
  12.     break;
  13.    
  14.     case "-";
  15.     $total = $num1 - $num2;
  16.     break;
  17.    
  18.     case "*";
  19.     $total = $num1 * $num2;
  20.     break;
  21.      
  22.      
  23.     case "/";
  24.     $total = $num1 / $num2;
  25.     break;
  26.    
  27.     default:
  28.     echo "Unknown operator.";
  29.    
  30.     }  
  31.     }
  32.  
  33.     ?>
  34.  
  35.  
  36. Example: 2
  37.  
  38.     <?php
  39.  
  40.     function calc($num1, $num2, $op)
  41.     {
  42.     switch ($op)
  43.     {
  44.     case "+";
  45.     $total = $num1 + $num2;
  46.     break;
  47.    
  48.     case "-";
  49.     $total = $num1 - $num2;
  50.     break;
  51.    
  52.     case "*";
  53.     $total = $num1 * $num2;
  54.     break;
  55.      
  56.      
  57.     case "/";
  58.     $total = $num1 / $num2;
  59.     break;
  60.    
  61.     default:
  62.     echo "Unknown operator.";
  63.    
  64.     }  
  65.    
  66.     calc(10,10,"+");
  67.  
  68.     }
  69.  
  70.     ?>
  71.  
  72.  
  73. Example: 3
  74.  
  75.     <?php
  76.  
  77.     function calc($num1, $num2, $op)
  78.     {
  79.     switch ($op)
  80.     {
  81.     case "+";
  82.     $total = $num1 + $num2;
  83.     break;
  84.    
  85.     case "-";
  86.     $total = $num1 - $num2;
  87.     break;
  88.    
  89.     case "*";
  90.     $total = $num1 * $num2;
  91.     break;
  92.      
  93.      
  94.     case "/";
  95.     $total = $num1 / $num2;
  96.     break;
  97.    
  98.     default:
  99.     echo "Unknown operator.";
  100.    
  101.     }  
  102.    
  103.     echo calc(10,10,"+");
  104.  
  105.     }
  106.  
  107.     ?>
  108.  
  109. Example: 4
  110.  
  111.     <?php
  112.  
  113.     function calc($num1, $num2, $op)
  114.     {
  115.     switch ($op)
  116.     {
  117.     case "+";
  118.     $total = $num1 + $num2;
  119.     return $total;
  120.     break;
  121.    
  122.     case "-";
  123.     $total = $num1 - $num2;
  124.     return $total;
  125.     break;
  126.    
  127.     case "*";
  128.     $total = $num1 * $num2;
  129.     return $total;
  130.     break;
  131.      
  132.      
  133.     case "/";
  134.     $total = $num1 / $num2;
  135.     return $total;
  136.     break;
  137.    
  138.     default:
  139.     echo "Unknown operator.";
  140.    
  141.     }  
  142.    
  143.     echo calc(10,10,"+");
  144.  
  145.     }
  146.  
  147.     ?>
  148.  
  149. Example: 5
  150.  
  151.     <?php
  152.  
  153.     function calc($num1, $num2, $op)
  154.     {
  155.     switch ($op)
  156.     {
  157.     case "+";
  158.     $total = $num1 + $num2;
  159.     return $total;
  160.     break;
  161.    
  162.     case "-";
  163.     $total = $num1 - $num2;
  164.     return $total;
  165.     break;
  166.    
  167.     case "*";
  168.     $total = $num1 * $num2;
  169.     return $total;
  170.     break;
  171.      
  172.      
  173.     case "/";
  174.     $total = $num1 / $num2;
  175.     return $total;
  176.     break;
  177.    
  178.     default:
  179.     echo "Unknown operator.";
  180.    
  181.     }  
  182.    
  183.    
  184.     }
  185.     echo calc(10,10,"+");
  186.  
  187.     ?>
  188.  
  189.  
  190. Example: 6
  191.  
  192.     <?php
  193.  
  194.     function calc($num1, $num2, $op)
  195.     {
  196.     switch ($op)
  197.     {
  198.     case "+";
  199.     $total = $num1 + $num2;
  200.     return $total;
  201.     break;
  202.    
  203.     case "-";
  204.     $total = $num1 - $num2;
  205.     return $total;
  206.     break;
  207.    
  208.     case "*";
  209.     $total = $num1 * $num2;
  210.     return $total;
  211.     break;
  212.      
  213.      
  214.     case "/";
  215.     $total = $num1 / $num2;
  216.     return $total;
  217.     break;
  218.    
  219.     default:
  220.     echo "Unknown operator.";
  221.    
  222.     }  
  223.    
  224.    
  225.     }
  226.     echo calc(13,7,"/");
  227.  
  228.     ?>
  229.  
  230.  
  231. Example:7
  232.  
  233.     <?php
  234.  
  235.     function calc($num1, $num2, $op)
  236.     {
  237.     switch ($op)
  238.     {
  239.     case "+";
  240.     $total = $num1 + $num2;
  241.     return $total;
  242.     break;
  243.    
  244.     case "-";
  245.     $total = $num1 - $num2;
  246.     return $total;
  247.     break;
  248.    
  249.     case "*";
  250.     $total = $num1 * $num2;
  251.     return $total;
  252.     break;
  253.      
  254.      
  255.     case "/";
  256.     $total = $num1 / $num2;
  257.     return $total;
  258.     break;
  259.    
  260.     default:
  261.     echo "Unknown operator.";
  262.    
  263.     }  
  264.        
  265.     }
  266.     echo calc(13,7,"a");
  267.  
  268.     ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement