Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Functions

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Functions

Postby Xerpher » Wed Aug 28, 2002 2:51 pm

Lets say I make a function that takes the inputed value and manipulates it then orints it out... That all works great for me, but I was wondering if I could make my function be used without having to put the '(' and ')' just like how the print function doesn't need those, it works with just the double quotes.
Xerpher
New php-forum User
New php-forum User
 
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada

Postby Jay » Thu Aug 29, 2002 2:32 am

Any user defined function HAS to be in the format of:
functionName ([arg1.[arg2[arg3...]]]) { function parameters }

You can't create a function without the paranthesis otherwise it wouldn't know where to get the variables from!
Jay
 

Postby Xerpher » Thu Aug 29, 2002 4:53 am

I didn't mean the actual function creation... I meant when you call the function. For example:

print ("Welcome!") // This outputs "Welcome!"
print "Welcome!" // This outputs "Welcome!"

Whether you use the parentheses or not, the print function still works... Can I do that to my own functions?
Xerpher
New php-forum User
New php-forum User
 
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada

Postby elitecodex » Thu Aug 29, 2002 4:55 am

No
At least not to my knowledge
elitecodex
New php-forum User
New php-forum User
 
Posts: 68
Joined: Tue Jul 09, 2002 8:45 am
Location: East Coast, USA

Postby Jay » Thu Aug 29, 2002 2:06 pm

No, don't try to copy the built-in PHP functions!

If you made your own function like so
function myFunct($arg) {
$arg .= $arg;
return $arg
}

you could write the following code:
print myFunct("Some text");

which will output
Some textSome text

But when you call a function, even one with no additional parameters, you have to use the paranthesis
Jay
 

Postby Xerpher » Thu Aug 29, 2002 3:02 pm

Or I could make it simpler by doing this...

If you made your own function like so
function myFunct($arg) {
$arg .= $arg;
print $arg
}

you could write the following code:
myFunct("Some text");

which will output
Some textSome text
Xerpher
New php-forum User
New php-forum User
 
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada

Postby Jay » Fri Aug 30, 2002 12:53 am

True, but I find the 'return' idea to be much more useful as a function because you've still got an element of control over the value it returns, and you can pass it through another function, say.

If you're function is printing it out, then you're stuffed if you want to manipulate it more!
Jay
 


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 4 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.