SitePoint Sponsor |
|
User Tag List
Results 1 to 8 of 8
Thread: Functions
-
Feb 8, 2001, 07:49 #1
- Join Date
- Jul 2000
- Location
- Singapore
- Posts
- 2,103
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi,
How do you Define and Call Functions?
Also, How do you validate fields?
So if the form name is name
then you do this:
if ($name == '') { echo "Error! Name no Entered"; }
is it?
Thanks"Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
-- Albert Einstein
-
Feb 8, 2001, 08:37 #2
- Join Date
- Aug 1999
- Location
- Pittsburgh, PA, USA
- Posts
- 3,910
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Try this...
Code:function checkField($field) { $switch = ($field == "") ? 0 : 1; return $switch. }
Code:$check = checkField($name); if ($check) { echo("Hi, $name!"); } else { echo("Invalid name entered."); }
-
Feb 8, 2001, 09:29 #3
- Join Date
- Jul 2000
- Location
- Singapore
- Posts
- 2,103
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi,
Thanks...Thats a really cool function.
Okie, Here's my problem I need to clarify.
It seems php functions must have variables parsed into the function to work?
it can't just be
function name {
}
? just like perl's subroutine?
Thanks"Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
-- Albert Einstein
-
Feb 8, 2001, 10:24 #4
- Join Date
- Jul 2000
- Location
- Perth Australia
- Posts
- 1,717
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
no you do not need to.... this is an example from the manual,
<?
function small_numbers() {
return array (0, 1, 2);
}
list ($zero, $one, $two) = small_numbers();
?>
-
Feb 8, 2001, 10:37 #5
- Join Date
- Aug 1999
- Location
- Pittsburgh, PA, USA
- Posts
- 3,910
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Yup, no problem at all - I sometimes create a small function called "error" - I simply use empty parentheses (like this: "()") after the name of the function.
I call that function like this:
error();
It simply prints out a generic error message, however, the true power of functions lies in accepting arguments and returning values.
-
Feb 8, 2001, 11:47 #6
- Join Date
- Jul 2000
- Location
- Singapore
- Posts
- 2,103
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Oh. I get it..
but you must put the ()
( and thats what I idnd't put..hence my functions were producing errors..ehehs )
Thanks for your help guys!"Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
-- Albert Einstein
-
Feb 8, 2001, 12:15 #7
- Join Date
- Aug 2000
- Location
- San Diego, CA
- Posts
- 5,460
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Alternately instead of calling the function for every field you want to check, you can use the $HTTP_POST_VARS array inside your function to check all at once.
function error_checking() {
global $HTTP_POST_VARS;
extract($HTTP_POST_VARS);
if ($name == "") $error[] = "You must put in a name";
if ($address == "") $error[] = "You must put in an address";
if ($city == "") $error[] = "You must put in a city";
return $error;
}
Sample usage
if ($submit) {
$error = error_checking();
if (count($error) > 0) {
foreach($error as $value) {
print $value."<br>";
}
}
}Please don't PM me with questions.
Use the forums, that is what they are here for.
-
Feb 8, 2001, 20:46 #8
- Join Date
- Jul 2000
- Location
- Singapore
- Posts
- 2,103
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
WOW! thanks freddy!
Thats an excellent piece of code!
Jolting down....=)"Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
-- Albert Einstein
Bookmarks