SunQuest
 
           PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today!
  #1  
Old June 26th, 2003, 05:19 AM
jben.net jben.net is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 51 jben.net User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 m 41 sec
Reputation Power: 6
Send a message via AIM to jben.net
writing custom functions with php

Hi,

I'm trying to write a custom function that removes the first 2 characters from a string and then returns the rest, her's what I've got...

PHP Code:
<?php

function trimString ($string)
{    
    echo 
"trimString called<br>";
    
$stringLength strlen($string);
    
$string substr ($string2$stringLength);
    
    echo 
"$string<br>";
    
    return 
$string;
}

$myString "./djing/preview/6203_devine.jpg";

trimString($myString);

echo 
"$myString<br>";

?>


But this just outputs:

trimString called
djing/preview/6203_devine.jpg
./djing/preview/6203_devine.jpg

How can I return the affected string ??????? Where am I going wrong ??

Thanks in advance,

Jon

Reply With Quote
  #2  
Old June 26th, 2003, 05:21 AM
jben.net jben.net is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 51 jben.net User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 m 41 sec
Reputation Power: 6
Send a message via AIM to jben.net
Does the problem lie with variable scope ?? ie, am I passing the variable by refference rather than passing the actual variable ??

thanks,

Jon

Reply With Quote
  #3  
Old June 26th, 2003, 10:16 AM
hearn hearn is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: East Lansing, MI
Posts: 7 hearn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to hearn Send a message via AIM to hearn Send a message via Yahoo to hearn
Within "function trimString" declare myString as:

global $myString;

(note that you can then call myString anywhere in the php file and have no problems with variable scope.

Reply With Quote
  #4  
Old June 26th, 2003, 10:19 AM
laidbak laidbak is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Location: In Tha IE -- San Bernardino COUNTY
Posts: 788 laidbak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 4 m 2 sec
Reputation Power: 7
Send a message via ICQ to laidbak Send a message via AIM to laidbak Send a message via MSN to laidbak Send a message via Yahoo to laidbak
There were two problems with your script.

1. You called the function strlen for no reason and passed it to substr ( don't need this... and wastes time)

You could have done this:
PHP Code:
 trim(substr($string2)); 

2. You passed your variable by value instead of by reference which means you will only be affecting a copy of the variable instead of the variable.

You should have coded:
PHP Code:
function trimString(&$string


The modificed code would be:
PHP Code:
<?php

function trimString (&$string)
{   
    echo 
"trimString called<br>";
    print 
$string trim(substr($string2)) . "<br>";
}

$myString "./djing/preview/6203_devine.jpg";
trimString($myString);
echo 
"$myString<br>";

?>
__________________
__________________________________________________ _
Wil Moore III, MCP | Integrations Specialist | Senior Consultant
Are You Listed...? | DigitallySmooth Inc.

Reply With Quote
  #5  
Old June 26th, 2003, 10:22 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
Actually, you just need to do this:

$myString=trimString($myString);

Since you're returning a value, you have to put it somewhere in order for it to be useful. Or you could pass by reference. Declaring everything globally probably isn't a good idea.

And you could save some space by doing something more like this for the function:

function trimString($string){
return substr($string,2,strlen($string));
}

No point in setting variables if you don't need to, right?

Reply With Quote
  #6  
Old June 26th, 2003, 10:48 AM
laidbak laidbak is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Location: In Tha IE -- San Bernardino COUNTY
Posts: 788 laidbak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 4 m 2 sec
Reputation Power: 7
Send a message via ICQ to laidbak Send a message via AIM to laidbak Send a message via MSN to laidbak Send a message via Yahoo to laidbak
Quote:
Originally posted by dhouston
Declaring everything globally probably isn't a good idea.

It is a very BAD idea
Quote:

And you could save some space by doing something more like this for the function:

function trimString($string){
return substr($string,2,strlen($string));
}

I think you are on the right track, however, he doesn't need the overhead of strlen... not that the time is huge, but he just doesn't need it. If you leave the last (optional) parameter out it grabs the string up to the end of the line. If you are worried about trailing whitespace then trim it.
PHP Code:
 trim(substr($string2)) 

Reply With Quote
  #7  
Old June 26th, 2003, 11:27 AM
jben.net jben.net is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 51 jben.net User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 m 41 sec
Reputation Power: 6
Send a message via AIM to jben.net
woh, that's a well good response, thanks. All points noted thanks.

So, if I want to pass a var by reference rather than value I use
PHP Code:
&$ 
in the function ?? Why ?

Thanks for all the replies,

Jon ;-)

Reply With Quote
  #8  
Old June 26th, 2003, 12:14 PM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
It's just a cue to PHP to look for the original namespace for the variable rather than to allocate memory for a new variable and to copy the original value into that variable. I imagine this saves some resources while also conveniently allowing you to write functions that don't have to return a value. That's my understanding of it, at least.

Reply With Quote
  #9  
Old June 26th, 2003, 04:42 PM
jben.net jben.net is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 51 jben.net User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 m 41 sec
Reputation Power: 6
Send a message via AIM to jben.net
ahh, namespace is something I've come across with actionscript, but I think that because of the way I learnt it (AS) I don't realise I'm using it in the correct fashion.

Thanks,

Jon.

Reply With Quote
  #10  
Old June 27th, 2003, 12:42 AM
AmericanD AmericanD is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Posts: 81 AmericanD User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
the & is a reference pointer to the variable $myString.

Anyway laidbak never ceases to impress me in his programming skills.
__________________
Hungry for Code

Programming works best with a team over one single person

Reply With Quote
  #11  
Old June 27th, 2003, 04:51 AM
laidbak laidbak is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Location: In Tha IE -- San Bernardino COUNTY
Posts: 788 laidbak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 4 m 2 sec
Reputation Power: 7
Send a message via ICQ to laidbak Send a message via AIM to laidbak Send a message via MSN to laidbak Send a message via Yahoo to laidbak
dhouston, You are right on the money with your explaination of variable references.

AmericanD, thanks for your comment.

Reply With Quote
  #12  
Old June 27th, 2003, 07:20 AM
devilFish devilFish is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 11 devilFish User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Your problem is that when you call the function you created you need to do $trimmedString = trimString($blah); NOT just calling the function, because it is returning something, but it is not getting assigned to another var or outputted.

This is another instance of how the simplicity of PHP can be the killer. In languages such as Java if you had tried to do that it would have given you an error because the methods/function was not being used as it would have been defined. (because in Java you would have had to say that the method was going to return a variable)

Last edited by devilFish : June 27th, 2003 at 12:04 PM.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > writing custom functions with php


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 

SlickEdit