Page 1 of 2
Interesting attepmt wont work :(
Posted: Sat Jan 19, 2008 5:58 pm
by Citizen
Code: Select all
function gstop($m){
$addError = 1;
$message .= "<br />Error: $m <br />";
}
$addError = 0;
rename ("./file/arcade/upload/$fEntry/$swfn", "./file/arcade/swf/$swfn") or gstop("Could not move the game's swf file.");
if($addError == 0){
echo"Success!
}
The problem is, it echos success even when the rename process doesn't work. Any ideas?
Re: Interesting attepmt wont work :(
Posted: Sat Jan 19, 2008 6:00 pm
by VladSun
You must use
global scope modifier inside the function in order to do it.
Re: Interesting attepmt wont work :(
Posted: Sat Jan 19, 2008 7:04 pm
by Chris Corbyn
Yes, if you don't specify global then it just creates a variable in "local scope" and then discards it after the function has ended.
I'm not sure this is a great way to learn about functions however, really you want to take some parameters in, and return a value back out.
Re: Interesting attepmt wont work :(
Posted: Sat Jan 19, 2008 7:07 pm
by Citizen
Researched a bit and tried again, still can't get this to work:
Code: Select all
function gstop($m){
global $addError, $message;
$addError = 1;
$message .= "<br />Error: $m <br />";
}
$addError = 0;
rename ("./file/arcade/upload/$fEntry/$pic1", "./file/arcade/images/$pic1") or gstop("Could not move picture 1.");
if($addError == 0)
{
$message .= "<br />$pic1 successfully imported...";
}
According to the manual, it should work, since I'm globaling the variables within the function... but it doesn't. Any ideas?
Chris Corbyn wrote:Yes, if you don't specify global then it just creates a variable in "local scope" and then discards it after the function has ended.
I'm not sure this is a great way to learn about functions however, really you want to take some parameters in, and return a value back out.
If I use "return" don't I have to use an equality to get the returned value? Like...
But as it seems, I cant use that in my "or" statement for testing if the rename went through or not.
Re: Interesting attepmt wont work :(
Posted: Sat Jan 19, 2008 7:15 pm
by John Cartwright
A much cleaner approach in my opinion,
Code: Select all
$messages = array(); //initialize blank message array
if (rename ("./file/arcade/upload/$fEntry/$pic1", "./file/arcade/images/$pic1")) {
$messages[] = 'Picture 1 successfully renamed';
} else {
$messages[] = 'Cannot rename picture 1';
}
echo implode('<br />', $messages);
Re: Interesting attepmt wont work :(
Posted: Sat Jan 19, 2008 7:18 pm
by Chris Corbyn
Jcart wrote:A much cleaner approach in my opinion,
Code: Select all
$messages = array(); //initialize blank message array
if (rename ("./file/arcade/upload/$fEntry/$pic1", "./file/arcade/images/$pic1")) {
$messages[] = 'Picture 1 successfully renamed';
} else {
$messages[] = 'Cannot rename picture 1';
}
echo implode('<br />', $messages);
+1
Re: Interesting attepmt wont work :(
Posted: Sat Jan 19, 2008 7:19 pm
by Citizen
Jcart wrote:A much cleaner approach in my opinion,
Code: Select all
$messages = array(); //initialize blank message array
if (rename ("./file/arcade/upload/$fEntry/$pic1", "./file/arcade/images/$pic1")) {
$messages[] = 'Picture 1 successfully renamed';
} else {
$messages[] = 'Cannot rename picture 1';
}
echo implode('<br />', $messages);
I didn't know that a failed function returned a boolean. Does that go with all failed functions?
Re: Interesting attepmt wont work :(
Posted: Sat Jan 19, 2008 7:41 pm
by John Cartwright
Each function has different return values, which is specified in the manual.
For instance,
Re: Interesting attepmt wont work :(
Posted: Sat Jan 19, 2008 7:53 pm
by Citizen
Interesting.
So, running:
Code: Select all
function foo(){
return true;
}
if(foo()){
echo"bar";
}
Would echo bar?
Re: Interesting attepmt wont work :(
Posted: Sat Jan 19, 2008 8:01 pm
by John Cartwright
Right.
Re: Interesting attepmt wont work :(
Posted: Sun Jan 20, 2008 1:09 am
by Mordred
Citizen wrote:Interesting.
So, running:
Code: Select all
function foo(){
return true;
}
if(foo()){
echo"bar";
}
Would echo bar?
No, it won't work, it will give you an error message instead. Just test it and you'll see.
Re: Interesting attepmt wont work :(
Posted: Sun Jan 20, 2008 12:20 pm
by Jonah Bron
Mordred wrote:Citizen wrote:Interesting.
So, running:
Code: Select all
function foo(){
return true;
}
if(foo()){
echo"bar";
}
Would echo bar?
No, it won't work, it will give you an error message instead. Just test it and you'll see.
Tested. Works.

Re: Interesting attepmt wont work :(
Posted: Sun Jan 20, 2008 12:46 pm
by John Cartwright
Mordred wrote:Citizen wrote:Interesting.
So, running:
Code: Select all
function foo(){
return true;
}
if(foo()){
echo"bar";
}
Would echo bar?
No, it won't work, it will give you an error message instead. Just test it and you'll see.
Not sure what you are talking about. There are no errors present in the code.
Re: Interesting attepmt wont work :(
Posted: Sun Jan 20, 2008 1:49 pm
by Mordred
*sigh*
I meant that it would not work specifically on
Citizen's machine. Apparently he has some strange setup. So strange indeed, that it is faster for him to ask on a forum whether some code would do something, instead of trying it himself. I was hoping that he would share his testing results with us. It must be a common problem, because I often see people asking similar questions

Re: Interesting attepmt wont work :(
Posted: Sun Jan 20, 2008 3:25 pm
by John Cartwright
Mordred wrote:*sigh*
I meant that it would not work specifically on
Citizen's machine. Apparently he has some strange setup. So strange indeed, that it is faster for him to ask on a forum whether some code would do something, instead of trying it himself. I was hoping that he would share his testing results with us. It must be a common problem, because I often see people asking similar questions

