Jump to content

PHP function problem


thenewperson

Recommended Posts

Hey, i am trying to make upload page and have upload.php and upload-file.php. The upload-file.php contains the testing and moving of the file to the folder. This all works until i put all the testing into a function. I tryed echoing out the username and it comes up blank. When i take it out of the function and echo it, it echos the username out correctly. I am using iframes for the upload.php page and the upload-file.php

 

code with the problem in upload-file.php

function uploadtesting(){


$user = $session->username;
}

 

The code above works perfectly fine when its not in a function

Link to comment
https://forums.phpfreaks.com/topic/184225-php-function-problem/
Share on other sites

You need to either pass along the $session variable as an argument in the function or globalize it.

 

Passing it on as an argument

 

function uploadtesting($session)
{
$user = $session->username;
}

 

Globalizing it

 

function uploadtesting()
{
global $session;

$user = $session->username;
}

 

For the variablie to be globalized you have to set it before the function is called same goes for passing it on as an argument.

Link to comment
https://forums.phpfreaks.com/topic/184225-php-function-problem/#findComment-972615
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.