首页 > 代码库 > Using the built-in functions of PHP

Using the built-in functions of PHP

PHP has a huge range of built-in functions designed to perform predefined tasks such as converting a string to upper case, getting the dimensions of an image, or sending an email. In this video I‘ll explain some of the basic features of working with functions. The first thing to note is that the function name is always followed by a pair of parenthesis. This function PHP version reports which version of PHP is running on your server.

All functions return a value.And unless you do something with it. It‘s simply thrown away. You can either display the value using echo or capture it in a variable. Sorry if use echo with this. And then load it in a browser. It tells me that the version I‘m currently running is 5.3.8. Now instead of displaying it immediately I could store it as a variable. So the version now contains the value returned by PHP version, so I could use this anywhere else I like in my script.

With PHP version there‘s no need to add anything between the parenthesis. But most functions expect you to pass one or more values as arguments. Now lets say that I‘m going to create a vairable called name and my caps lock has been stuck on and I‘ve spelt everything in upper case. PHP has a useful function which will convert uppercase to lowercase.

So if I lose echo and then STR to lower and pass it name as the argument. That should convert it to lowercase. just before I display it I‘m going to add a BR tag. And then I want to display name again.

Save that and load it into a browser. The first instance it‘s been converted to lower case, but when I display name again, it‘s still upper case, and this is a very important point. About variables being passed as arguments to functions. In the vast majority of cases, the original value stored in the variable remains unchanged. It‘s only the return value that has the changes reflected in it.

So if I want to save the changed value, I need to assign it either to a new variable or reassign it to a same variable. But having my name all in lowercase is not much better than having it all in uppercase. A really cool feature of php is the ability to pass a function as an argument to another function. Now, what I‘m going to do is use the, I‘m going to reassign the result of the return value to name, and use the function UC first, which converts the first character of a string to upper case.

And as the argument I‘m going to pass it, STR to lower and name. And then we‘ll just save that and see what happens in the browser. So what has happened is this PHP has executed this function first, converted name to lowercase and passed a return value as the argument to ucfirst which creates an initial cap. So in effect what has happened is that David has been converted to lowercase and then the lowercase version has been passed to ucfirst and the first letter has been capitalized.

Seeing this type of construction can be rather confusing when you‘re first beginning with PHP but that it‘s an extremely convenient shorthand and is used quite often. Both STR to lower and the UC first take only one arguments, but many functions take multiple arguments and some take optionable arguments. Here, I‘ve gotten input string, which might have come from, an online form. And it contains HTML tags.

And here There is some javascript. In this case, the javascript just says, Boo, but it could be sending me off to some malicious site, so obviously I wouldn‘t want to display this directly in my own website. So what I need to do is to be able to strip out the tags, and PHP very conveniently has a function which is called strip_tags. So let echo strip_tags, and pass it input. Now, strip tags accept one argument or two.

Let‘s try it with the second optional argument. So the first one will be input again, and the optional argument is allowable text, so I want to allow the p tag and the a tag. Just need the Opening tags as a string there. Close the parentheses. And let‘s save it, and see what happens in the browser. Now in both cases, the body of that Javascript has been left intact. But the script tags are no longer there, so the JavaScript won‘t be executed. Only the body of the script is displayed.

But in the second one, the link is still there, and if we just go into Source View, you can see that there are no html tags at all in the first example. The second one has preserved the

tags and tag, and we‘ve still got the "href" which will take you to the URL. I‘ve been able to use "input" twice here because the original value hasn‘t been changed.

And I haven‘t captured this as the return value. This is the normal behavior. Most functions don‘t change the original value of any variables passed some of these arguments. But there are some exceptions. Let‘s just see one here. I‘ve got an array which is not in alphabetical order. And down here, I‘ve used the sort function and asked it as its argument the characters array, and this will actually change the original array.

So if we view that in the browser, the array has been reordered in alphabetical order and there is another Function, which is called rsort, which reverses the order of the array. So if we just refresh the browser, it‘s now in reverse alphabetical order. I mentioned that functions always have a return value. And you might be wondering, well, what‘s the return value for sort and rsort? It‘s actually true or false depending on whether the sort was successful or not. So it‘s important to know the type of return value to expect from a built in function.

The best way to find out is to consult the excellent online PHP manual. The manual also describes which arguments a function expects. And whether any of them are optional. In the vast majority of cases, the original value of variables used as arguments remains unchanged. So you need to display the return value immediately, or capture it in a variable.

Using the built-in functions of PHP