SitePoint Sponsor |
|
User Tag List
Results 1 to 6 of 6
Thread: PHP function ()
-
Mar 25, 2004, 10:05 #1
- Join Date
- Mar 2004
- Location
- Germany
- Posts
- 12
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
PHP function ()
Hi,
I am relativelly new to PHP programming and have searched a number of newsgroups on the following question:
Can someone explain to me the general difference between, e.g. function xyz () and function xyz ($123, $567)?
Some functions obviously need variables within the parenthesize and some don't. So, were's the difference and how do they function?
I appreciate your help.
Samy
-
Mar 25, 2004, 10:17 #2
- Join Date
- Oct 2001
- Location
- Texas
- Posts
- 598
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Both functions have the same name obviously. The first has no parameters(variables sent to it). To call the first one you just need xyz(), to call the second one you need xyz($var1, $var2). Then inside the second function you have access to these two sent variables.
PHP Code:function xyz ()
{
return 1000; // you do the stuff inside and return some value if needed.
}
function xyz ($123, $567)
{
return $123+$567; // you generally manipulate the variables sent and return some value if needed.
}
-
Mar 25, 2004, 11:08 #3
- Join Date
- Nov 2001
- Location
- UK
- Posts
- 553
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Generally speaking a function with no parameters will return the same thing each time. Accepting parameters like in the second example allows you to change the data somehow.
phpinfo() would be a good example of the first - the output never really changes. in_array() would be a good example of the second type, where you pass arguments to the function, and the function return a value which will vary depending on the arguments.Regards, Ant.
-
Mar 26, 2004, 09:51 #4
- Join Date
- Mar 2004
- Location
- Germany
- Posts
- 12
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanx so far. I think I got the overall idea of how they work, now.
Regards,
Samy
-
Mar 31, 2004, 02:44 #5
- Join Date
- Feb 2003
- Location
- Berlin
- Posts
- 370
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
What is the difference between writing
function xyz ($123, $567)
and
function xyz ()
globals $123, $567;
?
Flözen
-
Mar 31, 2004, 03:47 #6
- Join Date
- Feb 2004
- Location
- Örebro, Sweden
- Posts
- 2,716
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
If you use the first method (sending variables as arguments) then you can manipulate them and they will only change inside the function. You can also send any variable you want to the function which makes it easier to reuse. If you declare a variable as global you will always have to name your variable the same.
When you send a variable/value as an argument, you can use a different variable to fetch the returned value.
PHP Code:<?php
function foo($bar)
{
return $bar . 'Hello World';
}
$bar = 'Just testing...';
echo foo($bar); // prints "Just testing...Hello World"
$foobar = 'Hi World';
$foo = foo($foobar);
echo $foobar; // prints "Hi World"
echo $foo; // prints "Hi WorldHello World"
?>PHP Code:<?php
function foo()
{
global $bar;
$bar .= 'Hello World';
}
$bar = 'Hi World';
$foobar = foo();
echo $bar; // prints "Hi WorldHello World"
echo $foobar; // since foo() doesn't return anything, this variable will be empty
?>
PHP Code:<?php
function foo($value)
{
return "Your value: $value";
}
echo foo('A little test'); // prints "Your value: A little test"
echo foo(1); // prints "Your value: 1"
?>
Bookmarks