So you've heard about functions, and what they can do.. and how leet they are. Now its your turn to write your own function. First of all let's recap what functions do, how you define them and how to call them.
What functions do
Ever had to do the same thing more than once? It compiles a load of code that you can put one or more variables through, and spits out the output without you having to go through the same process or copy and paste it.
And they're useful for..?
Repetitive scripts, big sites and so on. It's good to have a centralised functions file or even a class (which I'll come onto later) so you can call them anywhere.
How'd you define them?
We start off with writing this:
| function newFunction($var1, $var2) { |
nstead of newFunction you can write the function name. With $var1 and $var2, you can have one variable or any amount you want. You can also use any name for the variable; it's useful to give variables relavant names so you know what you're inputting. Your complete function would look something along the lines of this:
| <?php function newFunction($var1, $var2) { #your function code here.. return $var1; return $var2; } ?> |
And so on.
Now we're going to make a script that does something quite trivial - checks if a variable is a number or not. If it's not a number, it will notify the user that it's not numerical, and if it is it will tell the user that it is. (Yes, I know there's a function that already does that for you, I'm only giving an example)
So let's write the function:
| <?php function isNumber($number) { // names it if(ctype_digit($number)) { // is it a number? echo "This variable is a number!"; // yes it is! } else { echo "This variable is not a number!"; // no, it's not } // end function ?> |
That's a very straight forward one. If you don't get this, just leave a comment and ask me about it, then I'll explain to you in further detail.
Happy coding!
Related Articles
- String FunctionsThis tutorial will teach you all of the basic string functions available in ASP using VBScript. I will keep things concise as possible. The following is a list of the functions and an example of each.
- Some PHP Functions You Must KnowPHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.
- Banner Rotation and PHP IncludeWith this script you can add a banner rotation to your site, the base code is:
- The Basics- JavaScript TutorialTo get started with JavaScript, you will want to be able to see the tag that will set a script apart from the HTML. The tags used to begin and end a script are the tags. The opening tag should appear like this
- Intro To Object OrientedOne thing you dont see too much, is tutorials on object oriented php. Now some may be familiar with object from Java and C++, and then on the other hand, many of you may have no clue what object is. For this tutorial, you need to be pretty fluent in php.
- Declaring Variables and FunctionsNow its time to get into some really fun stuff. Yes, variables and functions. Dont worry, its not as bad as it sounds.....lets start with declaring variables. Youll want to keep all of your variables in the HEAD section for now. Place the declarations between the SCRIPT tags inside the
- Random Password GenerationWeb programmers will enjoy this article. It is a quick and effective way to create Random Password Generation - needed for security and anti-spam measures. The author provides concise lines of code, screenshots and simply read texts.
- Date and TimeDisplay the current data and time using a very small and common code.
- Object Oriented Programming With PHP 4.1.xObject Oriented Programming provides raw power to PHP 4.1.x Part 1:Introduction First off, an object (also know as a class) is a very simple section of code that has a section of its own variables and functions. In a simple way an object is kind of like a program itself. Objects can be used fo...
- PHP File SystemWe will discuss the common filesystem functions available to you. First of all, in order to do anything with a file, we must first open it. To do so we will use the following line:
