PHP functions are used in every single script. Get acquainted with the PHP functions to create your own tasks.
<?php
explode(' ', $value1);
?>
These are built in PHP functions and they are the same and available on every PHP setup. You can access them in any script without needing to do anything special.
A function is a subprogram that can act on data and return a value. Each function has its own name and when that name is encountered, the execution of the programme branches off to the first line (or statement) of that function. When the function has completed, it 'returns' and execution resumes from where it left off. A function will perform a specific task and does it well, returning the value you want with the arguments you pass to it.
For example:
array explode ( string separator, string string [, int limit])
This line is known as the prototype. It tells you exactly what the function is and what parameters it should be passed. We can see that it will return an array when you call it, and as parameters (the name of a variable usually passed to a function e.g $split and $string are the parameters in explode($split, $string)), it takes separator, string and limit. Parameters are always within the ( ). The first 2:
( string separator, string string [, int limit])
are required and you must pass arguments (the actual value of the parameter e.g ' ' and 'split this up' are the arguments in explode(' ', 'split this up') ) for these when you call the function. If you do not specify a separator or a string, you will get an error. The parameters must also be passed in the order they are listed here. You cannot swap them around.
However, there is the limit parameter at the end:
( string separator, string string [, int limit] )
Notice how this is within [squared brackets]. This means it is optional. You do not need to pass that parameter to the function.
The return and parameter types (highlighted below) are not really important in most cases.
array explode ( string separator, string string [, int limit])
PHP is known as a loosely typed language which means you do not need to worry about variable types. PHP will do the work for you. In the case of explode() , you know however that it will return an array when it is completed and you have to pass it 2 strings. You would get an error if you tried to pass it an integer:
explode(1, 15117);
But generally speaking, you can safely ignore the types.
When calling the function, you can do several things with the result. For example, you could echo it directly to the page:
echo explode(' ' , 'split this up');
Which wouldn't be much use. The most common thing you'd do is save the result in a variable like so:
$split = explode(' ' , 'split this up');
You can then play with the data as you like because it is in the $split variable.
This article has been compiled by the content
development team at Pegasus
InfoCorp which pulls subject
matter specialists from different work domains. They can be
contacted through the Pegasus
InfoCorp website at info@pegasusinfocorp.com. Pegasus
InfoCorp is an India based web
design, web
development and online/
offline software development company. Please visit http://www.pegasusinfocorp.com to read more articles and know more about us!
Other companies and organizations are welcome to reprint this article
on their websites provided the following conditions are met.
- The article is not changed in any manner
- The article is copied as is in its entirety (including back
links to the Pegasus InfoCorp website).
- The company/ organization reprinting the article agrees to
defend, indemnify and hold harmless Pegasus InfoCorp, its employees,
directors, officers, agents, partners and their successors and
assigns from and against any and all liabilities, damages, losses,
costs and expenses, including attorney's fees, caused by or
arising out of claims based upon the use of the article, including
any claim of libel, defamation, violation of rights of privacy
or publicity, loss of service by subscribers and infringement
of intellectual property or other rights.
|