Using the built-in functions of PHP

Source: Internet
Author: User

PHP have a huge range of built-in functions designed to perform predefinedtasks 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 was that the function name was always followed by a pair of parenthesis. This function PHP version reports which version of PHP was running on your server.

All functions return a value. And unless you does something with it.It ' s simply thrown away. You can either display the value usingEcho 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 is contains the value returned by PHP version, so I could use this anywhere else I as in my script.

with PHP Version there ' s no need to add anything between the parenthesis. but most Functions expect you-pass one or more values as arguments. now lets say that I ' m going to create a vairable called name and my caps lock have been stuck on and I ' ve spelt everything in upper case. php has a useful function which would convert uppercase to lowercase.

So if I lose echo and then STR to lower and pass it name as the argument. That's 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 tolower case, if 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, the changes reflected in it.

So if I want to save the changed value, I need to assign it either to a newVariable or reassign it to a same variable.But have my name all in lowercase are not much better than have 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 does is use the, I ' m going to reassign the result of the return value to name, and the Functi On 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 and see what's happens in the browser.so-have happened is this PHP have executed this function first, converted name to lowercase and passed a return Value as the argument to Ucfirst which creates an initial cap. So's effect what have happened is that David have been converted to lowercase and then the lowercase version have been C4>passed to Ucfirst and the first letter have been capitalized.

Seeing this type of construction can is rather confusing when you ' re firstbeginning with PHP It's a 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 c3>optionable arguments. Here, I ' ve gotten input string, which might has come from, an online form. And it contains HTML tags.

And here there are 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 are to being able to strip out the tags, and PHP very conveniently have a function which is called strip_tags. so let Echo strip_ tags, and pass it input. now, strip tags accept one argument or both.

Let's try it with the second optional argument.So the first one would be being 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 this javascript have been left intact. But the script tags is no longer there, so the JavaScript won ' t be executed. The body of the script is displayed.

But on the second one, the link is still there, and if we just go to Source View, you can see that there be no HTML Tags at the first example. The second one has preserved the

tags and tag, and we ' ve still got the "href" which would take 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 is 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 would actually change the original array.

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

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

Using the built-in functions of PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.