rrsnider
12-17-2001, 02:15 PM
I am having problems with user written functions. The following statements don't seem to work inside the function when I pass a filename as a parameter (value or reference).
$userfile_name
$userfile_type
example
function upload($file) {
if ($file_name) {
...}
if ($file_type) {
...}
} //end upload
...
upload($userfile);
...
Is there something unique with functions that reside in the same .php file ? I am testing on a workstation using omni httpd, is it related to this web server ? I am even having trouble returning modified variables in the function back to the caller. Not using return statement. If I pass by reference, the passed parameter should take on any changes in the function.
Help, programmer getting frustrated with PHP !!!
Varun Shoor
12-17-2001, 02:36 PM
Originally posted by rrsnider
function upload($file) {
if ($file_name) {
...}
if ($file_type) {
...}
} //end upload
...
upload($userfile);
...
Seems to be a variable scope problem, you need to declare "outside" variables as global whenever you wish to use them in functions, maybe the following example will illustrate it better.
<?php
// These are the variables declared outside the scope
$a = 2;
$b = 3;
function doadd() {
global $a;
$b = 6;
echo "Entered Function<br>";
// This will print 8 and NOT 5
echo $a+b;
}
echo "Starting execution<br>";
// This will print 5
echo $a+b;
// This will print 8
doadd();
?>
Hope that helps :D
rrsnider
12-17-2001, 02:55 PM
Yes, but I'm trying to accomplish this: (I am questioning the passed parameter) does not seem to work.
function upload($file) {
if ($file_name) {
echo "$file_name";
...}
if ($file_type) {
echo "$file_type";
...}
} //end upload
...
upload($userfile1);
upload($userfile2);
upload($userfile3);
Varun Shoor
12-17-2001, 03:13 PM
you are just passing $file, you need to pass $file_name $file_type too if you want your function to process them, so it would be something like
function upload($file, $file_name, $file_type) {
.
.
.
}
upload($userfile1, $userfile1_name, $userfile1_type);
upload($userfile2, $userfile2_name, $userfile2_type);
upload($userfile3, $userfile3_name, $userfile3_type);
rrsnider
12-17-2001, 03:34 PM
Thats what I ended up doing. However, what prevents me from just passing $file as parameter and referencing $file_name in the function ?
Varun Shoor
12-17-2001, 04:03 PM
Originally posted by rrsnider
Thats what I ended up doing. However, what prevents me from just passing $file as parameter and referencing $file_name in the function ?
Why pass a reference? It wont save you any memory (probably couple of bytes), but you can do that too I guess, just add & beside the arguments
funkee
12-18-2001, 05:48 AM
Originally posted by rrsnider
Thats what I ended up doing. However, what prevents me from just passing $file as parameter and referencing $file_name in the function ?
Actually, $file and $file_name are completely separate variables. They both have the prefix "file" in their name but are separate variables.
HTH
MikeA
12-18-2001, 12:16 PM
Originally posted by rrsnider
Thats what I ended up doing. However, what prevents me from just passing $file as parameter and referencing $file_name in the function ?
You would need to define it as:
function upload($file_name)
{
if($file_name)
{
print $file_name;
}
}
upload($file);
This will work. But if you put $file up top then try an IF statement with $file_name, then like funkee said, two different variables.
You and also add & to the front of $file_name to make it a global variable so that:
function upload(&$file_name)
will make $file-name have the ability to be changed.