Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Calling funtions from an include in PHP

Options
  • 18-05-2006 3:48pm
    #1
    Closed Accounts Posts: 8,866 ✭✭✭


    I've written a couple of functions to be contained in a seperate file, which i call with include(). Here's one of them:

    [php]function getpremium($def,$indexation, $age, $term, $sex, $smokerstatus, $sum_assured, $premium_freq) {

    if($def==3 && $indexation==0) $rate_series="P249";

    if($def==6 && $indexation==0) $rate_series="P250";

    if($def==12 && $indexation==0) $rate_series="P251";

    if($def==3 && $indexation==1) $rate_series="P252";

    if($def==6 && $indexation==1) $rate_series="P253";

    if($def==12 && $indexation==1) $rate_series="P254";

    $rate_to_use=$rate[$rate_series][$age][$term][$sex][$smokerstatus];

    if($premium_freq=="12"){

    $basic=((11.5*$rate_to_use*$sum_assured/10000)/$premium_freq);

    }else{

    $basic=((12*$rate_to_use*$sum_assured/10000)/$premium_freq);

    }

    $premium=($policy_charge+$basic);


    Return $premium;[/php]
    Now my problem is this. The variables $def, $indexation etc. are entered into a form, call it script1.php, which sends off to script2.php, where i need to call the function to make the calculations. However its not working, and to be honest I don't know why! Here's script2.php:

    [php]<?php

    $def = $_POST;
    $indexation = $_POST;
    $age = $_POST;
    $term = $_POST;
    $sex = $_POST;
    $smokerstatus = $_POST;
    $sum_assured = $_POST;
    $premium_freq = $_POST;

    include 'mortgage_phi.php';

    validate($age, $term, $sex, $smokerstatus);

    echo "You are ". $age . " years old and requested a term of " . $term . " years. Your are ". $sex ." and your smoking status is ". $smokerstatus .".<br />";

    getpremium();

    echo "You're premium is " .$premium. ".";

    ?>
    </body></html>[/php]
    Any help appreciated, cheers!


Comments

  • Registered Users Posts: 683 ✭✭✭Gosh


    change the first piece of code to
    function getpremium() {
    
    global $def,$indexation, $age, $term, $sex, $smokerstatus, $sum_assured, $premium_freq;
    
    etc etc etc
    



  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Nope, it still doesn't return anything for $premium...


  • Registered Users Posts: 683 ✭✭✭Gosh


    add $premium on to the end of the global statement


  • Registered Users Posts: 683 ✭✭✭Gosh


    the $rate array and $policy_charge will have to be declared as global as well


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    That worked, mind you $premium is coming back as 0 so I'll have to revise my maths! :D


  • Advertisement
  • Closed Accounts Posts: 2,046 ✭✭✭democrates


    There's no closing curly brace on the getpremium function,
    and you are passing it no arguments while it expects 8.
    It needs the $rate 5-dimensional array(!) and $policy_charge passed in too. I assume the include defines $policy_charge and the $rate array, they're not defined in 'script2.php'.

    This is about 'variable scope'. Inside a function there is no default access to outside variables, you either pass them in as arguments, or define them inside as globals so the function can access them as gosh suggested. Otherwise the php interpreter sets up a brand new $policy_charge for example within the scope of the function, a totally seperate one to any $policy_charge variable in global scope or within other functions.


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    don't bother declaring anything global. The problem is that the function getpremium is designed to evaluate to something - in its original form it doesn't alter any global variables.

    All you have to write is $premium = getpremium() (and insert the values required).


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Sorry, there is a curly bracket closing the function, i just didn't copy it. Yep, $rate and $policy_charge are defined in the include script. I'm still getting nothing for $premium though...when I say nothing I mean '0' as apposed to literally nothing. Here's script1.php and the revised function getpremium:

    script1.php
    [PHP]<html>
    <body>

    <form action="test_result.php" method="post">
    <table width=95% cellspacing=0 cellpadding=1 border=0 class=newtbl>

    <tr>
    <th width=100% colspan=10>Customers Selection</td>
    </tr>
    <tr>
    <td> </td>
    </tr>
    <tr>
    <td width=20%> Enter Age: </td>
    <td><input name=age value=<?=$age?>></td>
    </tr>
    <tr>
    <td width=20%> Enter Term: </td>
    <td><input name=term value=<?=$term?>></td>
    </tr>
    <tr>
    <td width=20%> Enter Sex: </td>
    <td>
    <select name="sex" value=<?$sex?>>
    <option value='M'>M
    <option value='F'>F
    </td>
    </tr>
    <tr>
    <td width=20%> Smoking Status: </td>
    <td>
    <select name="smokerstatus" value=<?$smokerstatus?>>
    <option value='Y'>Y
    <option value='N'>N
    </td>
    </tr>
    <tr>
    <td width=20%> Sum Assured: </td>
    <td><input name=sum_assured value=<?=$sum_assured?>></td>
    </tr>
    <tr>
    <td width=20%> Premium Frequency: </td>
    <td>
    <select name="premium_freq" value=<?$premium_freq?>>
    <option value='1'>Month
    <option value='2'>Quarter
    <option value='3'>Half
    <option value='4'>Year
    </td>
    </tr>
    <tr>
    <td width=20%> Deferral: </td>
    <td>
    <select name="def" value=<?$def?>>
    <option value='3'>3
    <option value='6'>6
    <option value='12'>12
    </td>
    </tr>
    <tr>
    <td width=20%> Indexation: </td>
    <td>
    <select name="indexation" value=<?$indexation?>>
    <option value='1'>Y
    <option value='0'>N
    </td>
    <tr>
    <td width=30%> </td>
    <td width=70% align=right><input type="submit" /></td>
    </tr>
    </table>

    </body>
    </HTML>[/PHP]

    function getpremium:
    [PHP]function getpremium() {

    global $def,$indexation, $age, $term, $sex, $smokerstatus, $sum_assured, $premium_freq, $premium;

    if($def==3 && $indexation==0) $rate_series="P249";

    if($def==6 && $indexation==0) $rate_series="P250";

    if($def==12 && $indexation==0) $rate_series="P251";

    if($def==3 && $indexation==1) $rate_series="P252";

    if($def==6 && $indexation==1) $rate_series="P253";

    if($def==12 && $indexation==1) $rate_series="P254";

    if($premium_freq==1) $policy_charge="2.54";

    if($premium_freq==2) $policy_charge="7.62";

    if($premium_freq==3) $policy_charge="15.24";

    if($premium_freq==4) $policy_charge="25.40";

    $rate_to_use=$rate[$rate_series][$age][$term][$sex][$smokerstatus];

    if($premium_freq=="4"){

    $basic=((11.5*$rate_to_use*$sum_assured/10000)/$premium_freq);

    }else{

    $basic=((12*$rate_to_use*$sum_assured/10000)/$premium_freq);

    }

    $premium=($policy_charge+$basic);


    Return $premium;

    }[/PHP]

    And when I run through the form i get the policy charge as the premium, which means that $basic is empty... :confused:


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    seamus wrote:
    don't bother declaring anything global. The problem is that the function getpremium is designed to evaluate to something - in its original form it doesn't alter any global variables.

    All you have to write is $premium = getpremium() (and insert the values required).
    What do you mean by insert the values required?


  • Registered Users Posts: 683 ✭✭✭Gosh


    Gosh wrote:
    the $rate array and $policy_charge will have to be declared as global as well

    You missed this post - I think that's why your $rate to use is evaluating to zero


  • Advertisement
  • Registered Users Posts: 683 ✭✭✭Gosh


    What do you mean by insert the values required?
    Go back to your original code and change
    getpremium();
    
    to 
    
    $premium = getpremium($def, $indexation, $age, $term, $sex, $smokerstatus, $sum_assured, $premium_freq);
    


    you'll need to add $policy_charge to the function getpremium statement and the call to getpremium statement , and define the $rate array as global since you are using it within the function to retrieve the rate

    and remove the global statement

    it's your choice which way you want to go - stay as global or go as seamus suggests


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    ^^
    What he said :)


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Ok, still no luck...

    I think i've done everything advised here...

    script1.php:

    [PHP]<html>
    <body>

    <form action="test_result.php" method="post">
    <table width=95% cellspacing=0 cellpadding=1 border=0 class=newtbl>

    <tr>
    <th width=100% colspan=10>Customers Selection</td>
    </tr>
    <tr>
    <td> </td>
    </tr>
    <tr>
    <td width=20%> Enter Age: </td>
    <td><input name=age value=<?=$age?>></td>
    </tr>
    <tr>
    <td width=20%> Enter Term: </td>
    <td><input name=term value=<?=$term?>></td>
    </tr>
    <tr>
    <td width=20%> Enter Sex: </td>
    <td>
    <select name="sex" value=<?$sex?>>
    <option value='M'>M
    <option value='F'>F
    </td>
    </tr>
    <tr>
    <td width=20%> Smoking Status: </td>
    <td>
    <select name="smokerstatus" value=<?$smokerstatus?>>
    <option value='Y'>Y
    <option value='N'>N
    </td>
    </tr>
    <tr>
    <td width=20%> Sum Assured: </td>
    <td><input name=sum_assured value=<?=$sum_assured?>></td>
    </tr>
    <tr>
    <td width=20%> Premium Frequency: </td>
    <td>
    <select name="premium_freq" value=<?$premium_freq?>>
    <option value='1'>Month
    <option value='2'>Quarter
    <option value='3'>Half
    <option value='4'>Year
    </td>
    </tr>
    <tr>
    <td width=20%> Deferral: </td>
    <td>
    <select name="def" value=<?$def?>>
    <option value='3'>3
    <option value='6'>6
    <option value='12'>12
    </td>
    </tr>
    <tr>
    <td width=20%> Indexation: </td>
    <td>
    <select name="indexation" value=<?$indexation?>>
    <option value='1'>Y
    <option value='0'>N
    </td>
    <tr>
    <td width=30%> </td>
    <td width=70% align=right><input type="submit" /></td>
    </tr>
    </table>

    </body>
    </HTML>[/PHP]


    script2.php:

    [PHP]<?php

    $def = $_POST;
    $indexation = $_POST;
    $age = $_POST;
    $term = $_POST;
    $sex = $_POST;
    $smokerstatus = $_POST;
    $sum_assured = $_POST;
    $premium_freq = $_POST;

    include 'mortgage_phi.php';

    validate($age, $term, $sex, $smokerstatus);

    echo "You are ". $age . " years old and requested a term of " . $term . " years. Your are ". $sex ." and your smoking status is ". $smokerstatus .".<br />";

    $premium = getpremium($def, $indexation, $age, $term, $sex, $smokerstatus, $sum_assured, $premium_freq, $premium, $basic, $policy_charge);

    echo "You're premium is " .$premium. ".";

    ?>
    </body></html>[/PHP]

    function getpremium:

    [PHP]function getpremium($def, $indexation, $age, $term, $sex, $smokerstatus, $sum_assured, $premium_freq, $premium, $basic, $rate) {


    if($def==3 && $indexation==0) $rate_series="P249";

    if($def==6 && $indexation==0) $rate_series="P250";

    if($def==12 && $indexation==0) $rate_series="P251";

    if($def==3 && $indexation==1) $rate_series="P252";

    if($def==6 && $indexation==1) $rate_series="P253";

    if($def==12 && $indexation==1) $rate_series="P254";

    if($premium_freq==1) $policy_charge="2.54";

    if($premium_freq==2) $policy_charge="7.62";

    if($premium_freq==3) $policy_charge="15.24";

    if($premium_freq==4) $policy_charge="25.40";

    $rate_to_use=$rate[$rate_series][$age][$term][$sex][$smokerstatus];

    if($premium_freq=="4"){

    $basic=((11.5*$rate_to_use*$sum_assured/10000)/$premium_freq);

    }else{

    $basic=((12*$rate_to_use*$sum_assured/10000)/$premium_freq);

    }

    $premium=($policy_charge+$basic);


    Return $premium;

    }[/PHP]

    Going a little bit crazy..... :confused:


  • Closed Accounts Posts: 2,046 ✭✭✭democrates


    That returned a premium for me ok, had to comment the call to the validate function of course, could there be something cooky going on with that one as happened with getPremium?


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Yeah, probably gotta have a look at validate now too, should manage now with the pointers from ya'll on getpremium, cheers!


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Ok even commenting out validate i cant get it to work...

    I mean I do get a premium, but its just the value of which ever policy charge applies from the choice of premium frequency in the form...

    So $basic is still coming back empty....

    Just as a matter of interest, what did you use for $rate?


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    For future reference (although I'm not sure if this'll be more confusing than helpful :))

    There are two types of functions: Those that return values and those that don't.
    In general, if you're writing a function which returns a value, you make it as non-specific as possible. That is, you should be able to lift the function from one script and drop it into another, with no "variable not defined" errors, etc.

    For example, imagine you are working with files. You want to write a function which saves and closes a file that you have open. You also want this function to return "true" or "false", based on whether you successfully closed the file. Ignore anything you know about files or php - I'm talking in general programming terms here ::)
    So you could just call the function closeFile(). Inside this function, you have the handle/name of the file which you will close. It all works hunky dory. But what if in another script you want to use this in, opens a different file? You either have to write a new function (e.g. closeFile2), or you have to change the text of the function and copy and paste it into the other script. Right?
    Or you could write the function and take the file handle as an argument - e.g. closeFile($filehandle). Now you've got a function that can close any file, anywhere, and tell you if it was successful or not.

    Functions which don't return values are generally used for things specific to your scripts or set of scripts. For example, if I'm writing an application based on a Database backend, I'll have a function called init(), which will set up global variables, include extra files, etc etc. It doesn't return any value.


  • Closed Accounts Posts: 2,046 ✭✭✭democrates


    Just as a matter of interest, what did you use for $rate?
    Oops!
    You need to be able to see what's in each variable at each step in the process.
    The simple method is to echo them, but you risk forgetting to remove these before you go live opening a security risk.
    I now have a function which stores debug statements in a seperate db which only exists on my dev box, and I've a php script to view them. Worth developing when you get the time.


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Yeah I know what you mean.

    But anyway, an example of $rate would be:

    [PHP]$rate=148.53;[/PHP]

    There are literally thousands of these, for all the variations of age, term, sex, and smoker status, and the correct one should be selected depending on the criteria specified in the form. I know the getpremium function works, as i tested it using just one example of $rate and hardcoding the variables age, term etc. into the test script, and I got back a premium.

    The problem arises with using the form variables but I cant see why... it is receiving the variables, as $policy_charge is based on the form selection and that will come out as my premium when i run the script, because $premium=($policy_charge + $basic), therefore if i get the policy back as my premium that would imply that $basic isn't being calculated... :confused:


  • Registered Users Posts: 683 ✭✭✭Gosh


    If the value of $rate_to_use is zero then the value of $basic will be zero.

    $rate_to_use=$rate/COLOR][COLOR=#0000bb]$rate_series[/COLOR][COLOR=#007700/COLOR][COLOR=#0000bb]$age[/COLOR][COLOR=#007700/COLOR][COLOR=#0000bb]$term[/COLOR][COLOR=#007700/COLOR][COLOR=#0000bb]$sex[/COLOR][COLOR=#007700/COLOR][COLOR=#0000bb]$smokerstatus[/COLOR][COLOR=#007700;

    it is the $rate array that is causing the problem it has a local scope in getpremium - you need to define this as a global so that getpremium can get the values from outside of it's scope.


  • Advertisement
  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Define $rate or getpremium as global? That confused me.... yes, I am new to defining functions! :o


  • Registered Users Posts: 683 ✭✭✭Gosh


    in getpremium add

    global $rate;


  • Registered Users Posts: 683 ✭✭✭Gosh


    One final check before you set $rate_to_use would be to check that you actually have an entry in the $rate array. If it didn't exist you would get a value of zero for $rate_to_use.

    To check if the rate combination exists do the following
    if (!isset($rate[$rate_series][$age][$term][$sex][$smokerstatus])) {
       $invalid_rate="Y";
    }else{
      // carry on with your setting $rate_to_use and calculation
    }
    
    The invalid rate flag would have to be added to the list of parameters on the function and the call to the function and set to "N" before calling the function.

    After $premium=getpremium(..... you can test the value of $invalid_rate to see if you had a valid rate to use in the function.


    All of this, goes to show that using global is probably better (IMO) - you would not have to add variables to the function or function call. If you had used the function many times in your script then you would have to go and change every call to the function, whereas using global only needs it to be defined in the function itself.


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    That works! Sort of... heh

    No, your advice was brilliant. There's obviously a problem elsewhere, it wont calculate at all for a smoker... any criteria with a non smoker works, but the same criteria for a smoker wont, but should... :confused:


  • Registered Users Posts: 683 ✭✭✭Gosh


    PM sent


Advertisement