Search
Left Quote    Black holes are where God divided by zero.
- Steven Wright    
Right Quote
 
[login] | [Register]
 

Introduction to PHP

by: Jester
Page: 5 of 7
(View All)

In this chapter we’ll cover functions with PHP. How to use them, where to look them up and how you can define your own.

Functions

Functions are what you use to make PHP manipulate and process data, or just do a big bunch of statements quickly and easily without having to type the same lines of code over and over. PHP has hundreds of available functions for you to use and there’s normally always one you can use for what you want to do. On the rare occasion there isn’t, you can just write your own.

Firstly, let’s look at how we use existing functions.



Look at the example, we used the strip_tags() function. What this function does is strip any HTML or PHP tags out of a string. Look: $nohtml = strip_tags($string); – we passed the value of $string to the function strip_tags() which then strips all the HTML and PHP tags from the string, then returns the new, tagless, string into $nohtml. So what would be the output?




The tags are gone. The function stripped them out and then returned a new version of the string, with no tags, back into the variable that we specified ($nohtml). Let’s have a closer look at the syntax of a function.



  • $string – This is the variable containing our string, we know this already.
  • strip_tags – This is the name of the function we are using. All functions have () at the end. In this case we passed the function a value, when you do this you put the value you want to pass to the function within the brackets. It’s not compulsory with all functions, but just remember, value inside brackets. In this case we wanted to pass the value contained in $string. The value we pass is called a parameter or argument.
  • = – The assignment operator. We use this to assign a value to a string or array item, we already know this too.
  • $nohtml – This is the variable that the function will pass the new string back to. This is called returning a value. However, not all functions return a value, but this one does.

So the function strips the tags off the value that we pass to it, then we assign the new value from the function to $nohtml and there we have it, we used a function to modify a string.

PHP has many built-in functions like the one above. You can search on PHP.net’s function list to find them (see form at top of page). It’s really down to memory, the more you get into PHP the more functions you’ll use and remember.

_“I see and I forget, I hear and I remember, I do and I understand.”—Confucious_

This applies to programming, too. Sit here reading and you’ll forget it, open up a text editor, write a program, test it, debug it and change it, play around with PHP and you will remember.

Parameters/Arguments

Firstly parameter and argument is the same thing. Just some people call them parameters, some call them arguments. In the above example we passed one parameter to the function strip_tags. Functions aren’t limited to accepting one parameter, they can accept a number of parameters. Some functions require more than one parameter, some only have one, some require a certain amount of parameters or one parameter and have extra, optional parameters. strip_tags has an optional parameter that you can take advantage of. Let’s take a look.



Look, we passed an extra parameter to the function. The second parameter is a string (hence why it is surrounded by quotes) containing any tags we don’t want stripped from the string. We passed the function only one tag, the paragraph tag, so any paragraph tags in that string will not be stripped, but all other tags will. So the string returned from the function would be the string we submitted, minus any HTML or PHP tags, except for paragraph tags.
  • Functions can be passed parameters, depending on the function, some are optional, some are not. Functions can receive more than one parameter.
  • Multiple parameters are separated by commas.
  • If you submit a string as a parameter, quote it. The single quotes, double quotes thing also applies to passing parameters to functions.

Okay now we know about parameters and how we call and use functions, let’s look at how we can write some of our own.

Custom Functions

PHP allows us to define our own functions, let’s look at a very simple one, just to see the format.



Easy eh? We just write “function”, followed by the function name, followed by two brackets. We then open a curly bracket to start the function statements. It is ended by the closing of the curly brackets. We could now use this function in our script.



Doing that would call our function and run the statements inside it. That’s a pointless function, but it shows you how to define one. We could pass a parameter.



With that function we passed a parameter. So now we could do:



Passing the string "print this out" to the function. Now, when we define the function we need to give that function a name that it can use to handle the parameter passed to it. In this case I chose $string. It can be whatever you want. We could pass more than one parameter. Just give them each names when you define the function.



Returning a Value

To make our function return a value is simple, this is how we do it.



We just do whatever we want to do, and then return "whatever". We could just return a string enclosed in quotes, we could just return a number, we can return a variable or an array.

Note: in this function we used two new functions: substr() and strtoupper(). Click on the links to go to the PHP reference pages and see what these functions do. This is how you’ll learn functions if you’re going to get into PHP, so go read it.

So now we could do:



Our function returned the string which we then assigned to a variable. We could then pass this variable to other functions if we wanted, do whatever we want with it before we print it out. Have a play around with functions and get used to them. Before we move on, one more thing….

Getting GLOBAL

Sometimes in functions you have to use global. Let’s look at some examples to show you why.



See how we use $thestring within the function? Well if we then, outside the function, typed echo $thestring; it wouldn’t work. Any variable you initialisecannot automatically be used outside the function. Similarly, any variable created outside of a function cannot be used within a function by default. The term used it "global". If a variable is global it is available within a function and can be used outside it too.

There’s two main reasons why we’d need this:
  • We have a variable that we made and we want to use it in our function without passing it as a parameter.
  • A function creates a variable that we want to be available outside the function without having to return it.

Here’s how we make a variable global.



What we did above was make a variable named $length which we wanted to be usable within the function too. The line that does it is global $length;. It makes that variable usable within the function. If you ran that code without that line the variable $length would not be usable within the function. Inside the function it would not exist. global lets the function use it too. The same applies for variables we create within a function, use global $var; and that variable will be available outside the function, too.

That’s about it on functions, in the next chapter we will look at an important part of your learning: Control Structures.



1  |  2  |  3  |  4  |  5  |  6  |  7  |  
Next »


No Comments for this page.

You Must be logged in or a member to comment.


Tutorial Stats

Tutorial Stats

320,094 Views
9 Total Comments
5 Rating of 5 (2 Votes)

Options

Tutorial Options

· Login to Rate This Article
· Login to Post a Comment
· Read more by this author
Digg This Article! Del.icio.us: Bookmark This Article Reddit: Bookmark This Article BlinkList: Blink This Article! YahooMyWeb BlogMarks: Add This Mark! Furl: Save This Article Spurl: Mark This Article

Articles

Related    

· Simple PHP Tutorial
· One File Website
· Pagination with PHP
· Building a Comments Script
· Complete Members System
· Variable Scope



"AllSyntax.com" Copyright © 2002-2017; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank