PHP Basics Part3-Learn PHP Functions

Sharing is Caring
Share

In our last post about PHP variable, we came across example where we talked about function. If you haven’t gone through that then you can read PHP Basics Part 2.

Functions in programming languages are used to perform certain functions/actions like calculation, displaying time & year.  These are some of the most basic examples of functions I have used.

You can create your own custom functions to perform certain actions.

PHP is a very good language in terms of functions. It has more than 1000 built-in functions and on the top of that you can create your own function.

You will use variables while creating your functions in PHP.

PHP Function

As mentioned earlier, there are more than 1000 built-in functions in php. Some of the examples of these function are;

array_key_exists – Checks if the given key or index exists in the array

set_magic_quotes_runtime – Sets the current active configuration setting of magic_quotes_runtime

msql_db_query – Send mSQL query

msql_create_db- Create mSQL database

msql_result- Get result data

SWFMovie::setbackground – Sets the background color

SWFTextField::setColor – Sets the color of the text field

Count – Count all elements in an array, or something in an object

highlight_file – Syntax highlighting of a file

Some of these functions are really new and are currently in experimental stage which means that there could be some change in them at later stage. To find more examples of php functions, click here.

But that’s not the point, point is to let you know that there are many ready to use functions in PHP which make it really useful.

For example, if you are building a website and need to play with your database you can use these functions to perform relevant activities.

How To Create Custom Functions in PHP

You can create custom php functions by using the code shown below;

Now if you look at the above code, you will find;

<?php is the beginning of the php code as explained in PHP basics Part 1.

function functionName () – every function in php must begin with the keyword “function” . function tell php that it is a function, then you should give it a name as “functionName”. It could be anything but try to choose a relevant name so that you can easily remember it. In the parenthesis (), you need to enter arguments. Arguments are like variable. There is no limit on number of arguments you can pass it on in php functions, but make sure to separate them using coma (,). Arguments could be anything like a,b or any value/number. After argument in () in {}, you need to provide a code to be executed.

You need to remember one thing, functions doesn’t perform the action automatically. You have to call them in order to trigger them.

You can call them by putting their names along with any arguments in parenthesis like;

functionName();

It may sound too much but once you get use to it, it is fairly simple.

Now let’s go through some of the example of these php functions to understand them better.

 

 

You can run the codes above to see their output. I haven’t given the output so that you can try it yourself to see how they function!.

PHP Function Conclusion

This is the broad overview of php function. I have tried to reduce the extra and advance content in this post. Idea is to first introduce you with function and then we will further elaborate it.

As you can see that we have used lots of variables in lesson of php, similarly we will continue to look further into php functions as we move ahead in series.

PHP functions are simple to create and maintain along with being powerful. You can create custom php functions to perform variety of functions in your website or plugins or any other tool you are creating.

But that’s not enough, we have only learned about PHP functions, We still need to cover lot of ground about other topics and what we have covered so far. I will be coming up with more on php learnings.

If you are looking to learn php variable, then you can refer to the link above and for more php overview & definitions,click here.

Please leave your comments & queries.

Sharing is Caring
Share
About akhilendra

Hi, I’m Akhilendra and I write about Business Analysis, Data Science, IT & Web. Join me on Twitter, Facebook & Google+

Speak Your Mind

*