0

I have two functions and each has a variable with a decimal point number inside it:

    function comehere1($fap_value1)
    {
        $first=$fap_value1;
        echo 'fap_value1_comehere= '.$first;            
    }


    function comehere2($fpdc_value1)
    {
        $second=$fpdc_value1;
        echo 'fpdc_value1_comehere= '.$second;
    }

Now I want a third function, lets say calc(), in which I calculate the sum of the two variables $first and $second, from the two functions, and store in a variable called $final_sum. Like:

    function calc()
    {
        $final_sum=calculate($first, $second, '+');
        echo 'final_sum= '.$final_sum;          
    }

These all functions are in a view file in CakePHP 2.5.6 and I have tried defining them with global OR putting them in bootstrap OR configure::write and read OR return and etc, but nothing is letting them add up because they are 'undefined' outside of their respective functions.

Any help would be highly appreciated!

floriank
  • 25,546
  • 9
  • 42
  • 66

1 Answers1

0

You can define the functions in a helper and use these in a view. Read Here You have to create the Helper, load it in the controller and the you can use its methods in the view with $this->myHelper->myFunction()

Create function in helper:

function comehere1($fap_value1)
{
    return $fap_value1;
}

function comehere2($fpdc_value1)
{
    return $fpdc_value1;
}

function calc($val1, $val2)
{
    $final_sum=$val1 + $val2;
   return $final_sum;          
}

Call in View:

This->yourHelper->calc();
Guillaume Jacquenot
  • 11,217
  • 6
  • 43
  • 49
Tolga
  • 262
  • 5
  • 16
  • Thank you for the input; I just read the link about helpers and I can't move these functions out of this 'view' as both of them get called / get their values from other 'if' statements in this same 'view': `if ($ap_value1>0) { $fap_value1=$ap_value1; echo comehere1($fap_value1); }` and `if ($pdc_value1>0) { $fpdc_value1=$pdc_value1; echo comehere2($fpdc_value1); }` What should I do now within this 'view'? The other 'calculate's work fine, without helpers, for some reason? – Farrah Zakir Jan 11 '17 at 11:22
  • Your primary problem is that you don't understand how OOP, functions, variables and their scope works in php. You really should fix that issue first by learning the basics of the language you try to work with first. Start by reading the php manual from the beginning and check this http://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and Metaphorical spoken if you don't learn how to walk first you'll keep falling. So do yourself a favour and learn the ground laying basics first. – floriank Jan 11 '17 at 11:54
  • Dear Burzum, Thank you for the input of your thoughts since everybody teaches you something. I was looking for an answer to my question and your suggestion did help me a bit but if you had taught me this exact same thing a bit nicely, it would have been much better but still thank you for the useful link :-) – Farrah Zakir Jan 26 '17 at 06:42