|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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:
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 |
|
#2
|
|||
|
|||
|
Does the problem lie with variable scope ?? ie, am I passing the variable by refference rather than passing the actual variable ??
thanks, Jon |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
|||
|
|||
|
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:
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:
The modificed code would be: PHP Code:
__________________
__________________________________________________ _ Wil Moore III, MCP | Integrations Specialist | Senior Consultant Are You Listed...? | DigitallySmooth Inc. |
|
#5
|
||||
|
||||
|
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? |
|
#6
|
||||
|
||||
|
Quote:
It is a very BAD idea Quote:
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:
|
|
#7
|
|||
|
|||
|
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:
Thanks for all the replies, Jon ;-) |
|
#8
|
||||
|
||||
|
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.
|
|
#9
|
|||
|
|||
|
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. |
|
#10
|
|||
|
|||
|
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
|
|
#11
|
|||
|
|||
|
dhouston, You are right on the money with your explaination of variable references.
AmericanD, thanks for your comment. ![]() |
|
#12
|
|||
|
|||
|
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. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > PHP Development > writing custom functions with php |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|