Page 94 - Open Soource Technologies 304.indd
P. 94

Web Technologies-I



                   Notes         </head>
                                 <body>
                                 <?php
                                     function add_subt($val1, $val2){
                                         $add = $val1 + $val2;
                                         $subt = $val1 - $val2;

                                         return $add;
                                     }
                                 ?>
                                 </body>
                                 </html>
                                 This function will take the two values we give it, it will do both addition and subtraction, and
                                 right now it is going to return addition. Subtraction will just get lost like nothing ever happened.
                                 So we cannot return more than once so we have to choose. But what if we want both?

                                 Array is going to be our friendly solution here. Let’s see how array can help us.
                                        Example:

                                 <html>
                                 <head>

                                 <title>Writing Functions In PHP Return Values</title>
                                 </head>
                                 <body>

                                 <?php
                                     function add_subt($val1, $val2){
                                         $add = $val1 + $val2;

                                         $subt = $val1 - $val2;
                                         $result = array($add, $subt);

                                         return $result;
                                     }
                                     $result_array = add_subt(10,5);

                                     echo “Add: “ . $result_array[0] . “<br>”;
                                     echo “Subt: “ . $result_array[1] . “<br>”;

                                 ?>
                                 </body>
                                 </html>

                                 Output: Add: 15
                                 Output: Subt: 5



        88                                LOVELY PROFESSIONAL UNIVERSITY
   89   90   91   92   93   94   95   96   97   98   99