peteschulte Posted October 22, 2008 Share Posted October 22, 2008 Hello Coders, How do I call a function which I have defined in one PHP script from another PHP script? In the past I've used OSCommerce where this is done. Quite a bit of time researching on php.net and in "Learning PHP 5" has only enabled me to use Include() for a file to ACT AS a function. vars.php <?php $color = 'green'; $fruit = 'apple'; ?> test.php <?php echo "A $color $fruit"; // A include 'vars.php'; echo "A $color $fruit"; // A green apple ?> If my included file has a function definition in it, I can't call that function by name. Maybe include() isn't what I need. I read that require() acts the same except causes a fatal error if it fails. In the following, I commented out the long function which I would like to use, multipurpose(). Then I used the top five lines as an include(), same as the quoted example from php.net. The commented code in Multipurpose() works fine as a php script when started with three $_REQUEST statements -- whereas I want to pass parameters to it as a function. Thanks in advance! <?php if (is_numeric($operand1) && is_numeric($operand2)){ $result = $operand1 + $operand2; // echo $result; } return $result; /* //function Multipurpose(operation, operand1, operand2){ $operation = operation; $operand1 = operand1; $operand2 = operand2; $change = 'Y'; //if (is_numeric($operand1)) $operand1type= 'NUM'; //if (is_numeric($operand2)) $operand2type= 'NUM'; //echo "|" . $operand1type . "|" . $operand2type . "|" ; //if empty($operand1) $operand1 = 0; if (empty($operand2) and $operation == "MUL") $operand2 = 0; if (empty($operand1) and $operation == "ADD") $operand1 = 0; if (empty($operand2) and $operation == "ADD") $operand2 = 0; //echo "|" . $operand1 . "|" . $operand2 . "|" ; if (is_numeric($operand1) && is_numeric($operand2)){ switch ($operation) { case "ADD": $result = $operand1 + $operand2; break; case "SUB": $result = $operand1 - $operand2; break; case "MUL": $result = $operand1 * $operand2; break; case "DIV": $result = $operand1 / $operand2; break; case "PCT": $result = ($operand1 / $operand2)*100; break; case "MAX": if($operand1 >= $operand2){ $result = $operand1; } else { $result = $operand2; } break; case "MIN": if($operand1 <= $operand2){ $result = $operand1; } else { $result = $operand2; } break; case "EMP": if (empty($operand2)){ $result = $operand1; $change = 'N'; } else { $result = $operand2; $change = 'N'; } break; case "CON": $change = 'N'; $result = $operand1.$operand2; break; default: $change = 'N'; $result = "***bad operation request - '$operation' "; } // echo $operand1 . " " . $operation . " " . $operand2 . " Result: " . $result . "<br>\n"; } else { $change = 'N'; switch ($operation) { case "EMP": if (empty($operand2)){ $result = $operand1; } else { $result = $operand2; } break; case "CON": $result = $operand1.$operand2; break; case "FOR": $i = 0; $j = 0; $result = ""; while (strlen($operand1) > $i) { if ($operand2[$j] == "|") { $result = $result . $operand1[$i]; $j ++ ; $i ++ ; } else { $result = $result . $operand2[$j]; $j ++ ; } if (strlen($operand2) == $j) $j = 0; } while (strlen($operand2) > $j) { if ($operand2[$j] <> "|") $result = $result . $operand2[$j]; $j ++ ; } break; case "CHK": if(!check_cc($operand1) OR strlen($operand1)<15){ $result = 'INVALID'; // give an error message }else{ $result = 'OK'; // continue with processing } break; default: $result = "***unknown operation OR operand 1 and 2 need to be numeric for '$operation' "; } } if ($change == 'Y'){ $return = number_format($result, 2, '.', ''); } else { $return = $result; } //echo $result; echo $return; return $return; //} // end Multipurpose function */ ?> : EDITED BY WILDTEEN88: Please use code tags ( ) when posting code Quote Link to comment https://forums.phpfreaks.com/topic/129606-solved-how-to-call-a-user-defined-function-from-another-php-script/ Share on other sites More sharing options...
JonnoTheDev Posted October 22, 2008 Share Posted October 22, 2008 If you are including the file where the function resides then you should have no problem calling it. i.e. functions.php =========== function helloWorld() { return "Hello World"; } index.php =========== include('functions.php'); print helloWorld(); Quote Link to comment https://forums.phpfreaks.com/topic/129606-solved-how-to-call-a-user-defined-function-from-another-php-script/#findComment-671894 Share on other sites More sharing options...
peteschulte Posted October 23, 2008 Author Share Posted October 23, 2008 Thank you Neil. :-) This looks like what I did, but I will use your format with a function with parameters which returns something to print. Quote Link to comment https://forums.phpfreaks.com/topic/129606-solved-how-to-call-a-user-defined-function-from-another-php-script/#findComment-673132 Share on other sites More sharing options...
peteschulte Posted October 24, 2008 Author Share Posted October 24, 2008 As usual, it was about single quotes and quotation marks, in this case where I took the params which had been coming as HTTP requests. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/129606-solved-how-to-call-a-user-defined-function-from-another-php-script/#findComment-674094 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.