I want to call a function in one PHP file from a second PHP file and also pass two parameters to that function. How can I do this?
我想从第二个PHP文件中调用一个PHP文件中的函数,并向该函数传递两个参数。我该怎么做呢?
I am very new to PHP. So please tell me, should I include the first PHP file into the second?
我对PHP很陌生。请告诉我,我应该将第一个PHP文件包含到第二个PHP文件中吗?
Please show me an example. You can provide some links if you want.
请给我举个例子。如果你愿意,你可以提供一些链接。
3 个解决方案
#1
132
Yes include the first file into the second. That's all.
是的,将第一个文件包含到第二个文件中。这是所有。
See an example below,
请参见下面的一个例子,
File1.php :
File1。php:
<?php
function first($int, $string){ //function parameters, two variables.
return $string; //returns the second argument passed into the function
}
?>
Now Using include
(http://php.net/include) to include the File1.php
to make its content available for use in the second file:
现在使用include (http://php.net/include)来包含File1。php使其内容可在第二个文件中使用:
File2.php :
File2。php:
<?php
include 'File1.php';
echo first(1,"omg lol"); //returns omg lol;
?>
#2
24
file1.php
file1.php
<?php
function func1($param1, $param2)
{
echo $param1 . ', ' . $param2;
}
file2.php
php
<?php
require_once('file1.php');
func1('Hello', 'world');
See manual
参见手册
#3
5
files directory:
文件目录:
Project->
项目- >
-functions.php
显然也
-main.php
-main.php
functions.php
显然也
function sum(a,b){
return a+b;
}
function product(a,b){
return a*b;
}
main.php
main.php
require_once "functions.php";
echo "sum of two numbers ". sum(4,2);
echo "<br>"; // create break line
echo "product of two numbers ".product(2,3);
The Output Is :
的输出是:
sum of two numbers 6 product of two numbers 6
两个数字的和6两个数字的乘积6
Note: don't write public before function. Public, private, these modifiers can only use when you create class.
注意:函数前不要写public。这些修饰符只能在创建类时使用。
#1
132
Yes include the first file into the second. That's all.
是的,将第一个文件包含到第二个文件中。这是所有。
See an example below,
请参见下面的一个例子,
File1.php :
File1。php:
<?php
function first($int, $string){ //function parameters, two variables.
return $string; //returns the second argument passed into the function
}
?>
Now Using include
(http://php.net/include) to include the File1.php
to make its content available for use in the second file:
现在使用include (http://php.net/include)来包含File1。php使其内容可在第二个文件中使用:
File2.php :
File2。php:
<?php
include 'File1.php';
echo first(1,"omg lol"); //returns omg lol;
?>
#2
24
file1.php
file1.php
<?php
function func1($param1, $param2)
{
echo $param1 . ', ' . $param2;
}
file2.php
php
<?php
require_once('file1.php');
func1('Hello', 'world');
See manual
参见手册
#3
5
files directory:
文件目录:
Project->
项目- >
-functions.php
显然也
-main.php
-main.php
functions.php
显然也
function sum(a,b){
return a+b;
}
function product(a,b){
return a*b;
}
main.php
main.php
require_once "functions.php";
echo "sum of two numbers ". sum(4,2);
echo "<br>"; // create break line
echo "product of two numbers ".product(2,3);
The Output Is :
的输出是:
sum of two numbers 6 product of two numbers 6
两个数字的和6两个数字的乘积6
Note: don't write public before function. Public, private, these modifiers can only use when you create class.
注意:函数前不要写public。这些修饰符只能在创建类时使用。