Active9 months ago

Defining and Using Functions in PHP. Functions will not be executed until you write some code to do so. To invoke the sample function, you would use a statement like this. Move the three. This function is EXPERIMENTAL. The behaviour of this function, its name, and surrounding documentation may change without notice in a future release of PHP. This function should be used at your own risk. How to call function of one php file from another php file and pass parameters to it? You can write the function in a separate file (say common-functions.php) and.

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?

I am very new to PHP. So please tell me, should I include the first PHP file into the second?

Please show me an example. You can provide some links if you want.

Don't Panic
32.8k9 gold badges43 silver badges60 bronze badges
Pushpendra KuntalPushpendra Kuntal
3,14715 gold badges60 silver badges117 bronze badges

4 Answers

Yes include the first file into the second. That's all.

See an example below,

File1.php :

Now Using include (http://php.net/include) to include the File1.php to make its content available for use in the second file:

File2.php :

Max Leske
4,2676 gold badges35 silver badges48 bronze badges
MobPhp File Write FunctionsMob
9,2896 gold badges34 silver badges55 bronze badges
Dmitry TeplyakovDmitry Teplyakov
1,7104 gold badges19 silver badges43 bronze badges

files directory:

Project->

-functions.php

-main.php

functions.php

main.php

The Output Is :

sum of two numbers 6 product of two numbers 6

Note: don't write public before function. Public, private, these modifiers can only use when you create class.

Hafiz Shehbaz AliHafiz Shehbaz Ali

you can write the function in a separate file (say common-functions.php) and include it wherever needed.

You can include common-functions.php in another file as below.

You can include any number of files to another file. But including comes with a little performance cost. Therefore include only the files which are really required.

Php Fopen

Abubkr ButtAbubkr Butt

protected by Jack BashfordJun 1 at 9:39

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged php or ask your own question.

  • PHP Tutorial
  • Advanced PHP
  • PHP Form Examples
  • PHP login Examples
  • PHP AJAX Examples
  • PHP XML Example
  • PHP Frame Works
Functions
  • PHP Design Patterns
  • PHP Function Reference
  • PHP Useful Resources
  • Selected Reading

PHP functions are similar to other programming languages. A function is a piece of code which takes one more input in the form of parameter and does some processing and returns a value.

Write the php filesystem functions

You already have seen many functions like fopen() and fread() etc. They are built-in functions but PHP gives you option to create your own functions as well.

There are two parts which should be clear to you −

  • Creating a PHP Function
  • Calling a PHP Function

In fact you hardly need to create your own PHP function because there are already more than 1000 of built-in library functions created for different area and you just need to call them according to your requirement.

Please refer to PHP Function Reference for a complete set of useful functions.

Php File Open

Creating PHP Function

Its very easy to create your own PHP function. Suppose you want to create a PHP function which will simply write a simple message on your browser when you will call it. Following example creates a function called writeMessage() and then calls it just after creating it.

Note that while creating a function its name should start with keyword function and all the PHP code should be put inside { and } braces as shown in the following example below −

This will display following result −

PHP Functions with Parameters

PHP gives you option to pass your parameters inside a function. You can pass as many as parameters your like. These parameters work like variables inside your function. Following example takes two integer parameters and add them together and then print them.

This will display following result −

Passing Arguments by Reference

It is possible to pass arguments to functions by reference. This means that a reference to the variable is manipulated by the function rather than a copy of the variable's value.

Any changes made to an argument in these cases will change the value of the original variable. You can pass an argument by reference by adding an ampersand to the variable name in either the function call or the function definition.

Following example depicts both the cases.

This will display following result −

Php Fwrite

PHP Functions returning value

A function can return a value using the return statement in conjunction with a value or object. return stops the execution of the function and sends the value back to the calling code.

Php File Write Example

You can return more than one value from a function using return array(1,2,3,4).

Following example takes two integer parameters and add them together and then returns their sum to the calling program. Note that return keyword is used to return a value from a function.

This will display following result −

Setting Default Values for Function Parameters

You can set a parameter to have a default value if the function's caller doesn't pass it.

Following function prints NULL in case use does not pass any value to this function.

This will produce following result −

Php File Write Functions In Mysql

Dynamic Function Calls

It is possible to assign function names as strings to variables and then treat these variables exactly as you would the function name itself. Following example depicts this behaviour.

Php File Write Functions In R

This will display following result −