If you want to control the amount of destruction that explode can wreak on your original string, consider using the third (optional) argument which allows you to set the number of pieces explode can return. This means it will stop exploding once the number of pieces equals the set limit. Below we've blown up a sentence with no limit and then with a limit of 4.
PHP CODE
$someWords = "Please don't blow me to pieces.";
$wordChunks = explode(" ", $someWords);
for($i = 0; $i < count($wordChunks); $i++){
echo "Piece $i = $wordChunks[$i]
";
}
$wordChunksLimited = explode(" ", $someWords, 4);
for($i = 0; $i < count($wordChunksLimited); $i++){
echo "Limited Piece $i = $wordChunksLimited[$i]
";
}
DISPLAY
Piece 0 = Please
Piece 1 = don't
Piece 2 = blow
Piece 3 = me
Piece 4 = to
Piece 5 = pieces.
Limited Piece 0 = Please
Limited Piece 1 = don't
Limited Piece 2 = blow
Limited Piece 3 = me to pieces.
The explode Function
The first argument that explode takes is the delimiter (our dynamite) which is used to blow up the second argument, the original string. explode returns an array of string pieces from the original and they are numbered in order, starting from 0. Lets take a phone number in the form ###-###-#### and use a hyphen "-" as our dynamite to split the string into three separate chunks.
PHP CODE
$rawPhoneNumber = "800-555-5555";
$phoneChunks = explode("-", $rawPhoneNumber);
echo "Raw Phone Number = $rawPhoneNumber
";
echo "First chunk = $phoneChunks[0]
";
echo "Second chunk = $phoneChunks[1]
";
echo "Third Chunk chunk = $phoneChunks[2]";
DISPLAY
Raw Phone Number = 800-555-5555
First chunk = 800
Second chunk = 555
Third Chunk chunk = 5555
PHP CODE
$rawPhoneNumber = "800-555-5555";
$phoneChunks = explode("-", $rawPhoneNumber);
echo "Raw Phone Number = $rawPhoneNumber
";
echo "First chunk = $phoneChunks[0]
";
echo "Second chunk = $phoneChunks[1]
";
echo "Third Chunk chunk = $phoneChunks[2]";
DISPLAY
Raw Phone Number = 800-555-5555
First chunk = 800
Second chunk = 555
Third Chunk chunk = 5555
User Defined Functions
The syntax to create a user defined function is:
function function_name(){
//statements here
}
You use the same naming conventions as you do for variables, without the dollar sign. Another rule to remember is to not use spaces when giving a name to a function. Otherwise it will be interpreted as two different words which will result in an error message. Use an underscore instead. Also, as a matter of good coding practice, it is good to give a representative name to a function. For example, set_name() would be a better function name than name1().
There are no limits as to how many statements can be included in the function. A function must include all the required elements, which are:
Function name
Opening and closing parentheses - ()
Opening and closing braces - {}
Statements
The actual formatting of the function itself is not important, as long as the above elements are included. You call the function by its name to execute it.
function_name();
The above will cause the statements in the function to be executed.
Let's create a function that generates a random password.
We will call the function randpass():
Script:createrndpass.php
function randpass()
$chars = "1234567890abcdefGHIJKLMNOPQRSTUVWxyzABCDEF
ghijklmnopqrstuvwXYZ1234567890";
$thepass = '';
for($i=0;$i<7;$i++)
{
$thepass .= $chars{rand() % 39};
}
return $thepass;
}
//to use the function
$password=randpass();
?>
The above function creates a password with random numbers and letters. The $chars variable contains letters and numbers that are mixed up. The content of that variable is then randomized with the rnd() function and a result is returned in the $thepass variable.
The "return $thepass;" part of the function returns the function result to whatever variable you want it in. Therefore to run this function, all we need to do is:
$password =randpass();
function function_name(){
//statements here
}
You use the same naming conventions as you do for variables, without the dollar sign. Another rule to remember is to not use spaces when giving a name to a function. Otherwise it will be interpreted as two different words which will result in an error message. Use an underscore instead. Also, as a matter of good coding practice, it is good to give a representative name to a function. For example, set_name() would be a better function name than name1().
There are no limits as to how many statements can be included in the function. A function must include all the required elements, which are:
Function name
Opening and closing parentheses - ()
Opening and closing braces - {}
Statements
The actual formatting of the function itself is not important, as long as the above elements are included. You call the function by its name to execute it.
function_name();
The above will cause the statements in the function to be executed.
Let's create a function that generates a random password.
We will call the function randpass():
Script:createrndpass.php
function randpass()
$chars = "1234567890abcdefGHIJKLMNOPQRSTUVWxyzABCDEF
ghijklmnopqrstuvwXYZ1234567890";
$thepass = '';
for($i=0;$i<7;$i++)
{
$thepass .= $chars{rand() % 39};
}
return $thepass;
}
//to use the function
$password=randpass();
?>
The above function creates a password with random numbers and letters. The $chars variable contains letters and numbers that are mixed up. The content of that variable is then randomized with the rnd() function and a result is returned in the $thepass variable.
The "return $thepass;" part of the function returns the function result to whatever variable you want it in. Therefore to run this function, all we need to do is:
$password =randpass();
Subscribe to:
Comments (Atom)
