Interesting attepmt wont work :(

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Interesting attepmt wont work :(

Post 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?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Interesting attepmt wont work :(

Post by VladSun »

You must use global scope modifier inside the function in order to do it.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Interesting attepmt wont work :(

Post 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.
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Re: Interesting attepmt wont work :(

Post 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...

Code: Select all

$foo = bar();
But as it seems, I cant use that in my "or" statement for testing if the rename went through or not.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Interesting attepmt wont work :(

Post 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);
 
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Interesting attepmt wont work :(

Post 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
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Re: Interesting attepmt wont work :(

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Interesting attepmt wont work :(

Post by John Cartwright »

Each function has different return values, which is specified in the manual.

For instance,
rename() documentation wrote:Return Values
Returns TRUE on success or FALSE on failure.
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Re: Interesting attepmt wont work :(

Post by Citizen »

Interesting.

So, running:

Code: Select all

function foo(){
  return true;
}
if(foo()){
  echo"bar";
}
 
Would echo bar?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Interesting attepmt wont work :(

Post by John Cartwright »

Right.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Re: Interesting attepmt wont work :(

Post 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.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Interesting attepmt wont work :(

Post 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. :dubious:
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Interesting attepmt wont work :(

Post 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.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Re: Interesting attepmt wont work :(

Post 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 :banghead: :evil:
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Interesting attepmt wont work :(

Post 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 :banghead: :evil:
:dubious:
Post Reply