Jump to content

[SOLVED] how to call a user-defined function from another PHP script?


Recommended Posts

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

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();

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.