I’m going to pass through some values between two required/included php file,ex:
mysql.php:
<?php
$conn = mysql_pconnect("mysql.host.com","root","password") or trigger_error(mysql_error(),E_USER_ERROR);
?>
fun.php:
<?php
function fun() {
mysql_select_db($conn);
}
?>
main.php:
<?php
require_once('mysql.php');
require_once('fun.php');
fun();
//Output: Error that can't find $conn
?>
Sorry that I describe too simple. I found that I can pass anything though two included files if code run directly. But if one of them write as a function(like fun.php), the fun() can’t read mysql.php’s value. Anyone can give me a solution? Forgive my bad English and poor knowledge of PHP > <
Update:
One of solution I found is to insert the mysql.php’s code in each functions like:
fun.php:
<?php
function fun() {
$conn = mysql_pconnect("mysql.host.com","root","password") or trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($conn);
}
?>
But the problem is it’s very very annoying(because I’ve wrote huge of functions) and maybe not safe because it stored username and password of the mysql server.
Your question is very confusing. If I understand it, then the answer depends on how you are using the included information.
If you need to use the variables in functions, you need to declare the variable as “global” at the beginning of the function (or Class if you are OOP).
For example, if your main.php file defines the following example function:
If you are coding OOP,