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();
PHP Mail Introduction
The mail() function allows you to send emails directly from a script.
Requirements
For the mail functions to be available, PHP requires an installed and working email system. The program to be used is defined by the configuration settings in the php.ini file.
--------------------------------------------------------------------------------
Installation
The mail functions are part of the PHP core. There is no installation needed to use these functions.
--------------------------------------------------------------------------------
Runtime Configuration
The behavior of the mail functions is affected by settings in the php.ini file.
SMTP "localhost" Windows only: The DNS name or IP address of the SMTP server PHP_INI_ALL
smtp_port "25" Windows only: The SMTP port number. Available since PHP 4.3 PHP_INI_ALL
sendmail_from NULL Windows only: Specifies the "from" address to be used in email sent from PHP PHP_INI_ALL
sendmail_path NULL Unix systems only: Specifies where the sendmail program can be found (usually /usr/sbin/sendmail or /usr/lib/sendmail) PHP_INI_SYSTEM
ezmlm_hash()
Calculates the hash value needed by the EZMLM mailing list system
mail()
Allows you to send emails directly from a script
Requirements
For the mail functions to be available, PHP requires an installed and working email system. The program to be used is defined by the configuration settings in the php.ini file.
--------------------------------------------------------------------------------
Installation
The mail functions are part of the PHP core. There is no installation needed to use these functions.
--------------------------------------------------------------------------------
Runtime Configuration
The behavior of the mail functions is affected by settings in the php.ini file.
SMTP "localhost" Windows only: The DNS name or IP address of the SMTP server PHP_INI_ALL
smtp_port "25" Windows only: The SMTP port number. Available since PHP 4.3 PHP_INI_ALL
sendmail_from NULL Windows only: Specifies the "from" address to be used in email sent from PHP PHP_INI_ALL
sendmail_path NULL Unix systems only: Specifies where the sendmail program can be found (usually /usr/sbin/sendmail or /usr/lib/sendmail) PHP_INI_SYSTEM
ezmlm_hash()
Calculates the hash value needed by the EZMLM mailing list system
mail()
Allows you to send emails directly from a script
Difference between sort & asort in php ?
Many times when you go for an interview or technical test you may find this question
"What is the difference between the sort() and asort() function in php ?"
Basically sort() and asort() functions are used to sort the array in php.
* sort() function syntax - sort(array,sorttype)
sorttype is Optional. Specifies how to sort the array values.
* SORT_REGULAR - Default. Treat values as they are (dont change types)
* SORT_NUMERIC - Treat values numerically
* SORT_STRING - Treat values as strings
* SORT_LOCALE_STRING - Treat values as strings, based on local settings
This function assigns new keys for the elements in the array. Existing keys will be deleted.
eg. See the code below when we create array $myarray we have assigned keys as a b & c.
Now we are applying sort function and printing the array.
$my_array = array("a" => "chaprak", "b" => "programming", "c" => "dexter");
print_r($my_array);
echo "
";
sort($my_array);
echo "
";
print_r($my_array);
echo "
";
?>
so the output of the above code will be -
initial array contents -
Array ( [a] => chaprak [b] => programming [c] => dexter )
and after applying sort function -
Array ( [0] => chaprak [1] => dexter [2] => programming )
its keys are changed to numeric values and sorting is done.
View php manual for the array sort function
* asort() function syntax - asort(array,sorttype)
The asort() function sorts an array by the values. The values keep their original keys (index).
sorttype is Optional. Specifies how to sort the array values.
* SORT_REGULAR - Default. Treat values as they are (don't change types)
* SORT_NUMERIC - Treat values numerically
* SORT_STRING - Treat values as strings
* SORT_LOCALE_STRING - Treat values as strings, based on local settings
$my_array = array("a" => "chaprak", "b" => "programming", "c" => "dexter");
echo "
";
print_r($my_array);
echo "
";
asort($my_array);
echo "
";
print_r($my_array);
?>
when we execute above code then it will produce the following output
initially array contents are - Array ( [a] => chaprak [b] => programming [c] => dexter )
after applying asort() function the contents are -
Array ( [a] => chaprak [c] => dexter [b] => programming )
see here sorting is done and even index is maintained.
View php manual for the array sort function
"What is the difference between the sort() and asort() function in php ?"
Basically sort() and asort() functions are used to sort the array in php.
* sort() function syntax - sort(array,sorttype)
sorttype is Optional. Specifies how to sort the array values.
* SORT_REGULAR - Default. Treat values as they are (dont change types)
* SORT_NUMERIC - Treat values numerically
* SORT_STRING - Treat values as strings
* SORT_LOCALE_STRING - Treat values as strings, based on local settings
This function assigns new keys for the elements in the array. Existing keys will be deleted.
eg. See the code below when we create array $myarray we have assigned keys as a b & c.
Now we are applying sort function and printing the array.
$my_array = array("a" => "chaprak", "b" => "programming", "c" => "dexter");
print_r($my_array);
echo "
";
sort($my_array);
echo "
";
print_r($my_array);
echo "
";
?>
so the output of the above code will be -
initial array contents -
Array ( [a] => chaprak [b] => programming [c] => dexter )
and after applying sort function -
Array ( [0] => chaprak [1] => dexter [2] => programming )
its keys are changed to numeric values and sorting is done.
View php manual for the array sort function
* asort() function syntax - asort(array,sorttype)
The asort() function sorts an array by the values. The values keep their original keys (index).
sorttype is Optional. Specifies how to sort the array values.
* SORT_REGULAR - Default. Treat values as they are (don't change types)
* SORT_NUMERIC - Treat values numerically
* SORT_STRING - Treat values as strings
* SORT_LOCALE_STRING - Treat values as strings, based on local settings
$my_array = array("a" => "chaprak", "b" => "programming", "c" => "dexter");
echo "
";
print_r($my_array);
echo "
";
asort($my_array);
echo "
";
print_r($my_array);
?>
when we execute above code then it will produce the following output
initially array contents are - Array ( [a] => chaprak [b] => programming [c] => dexter )
after applying asort() function the contents are -
Array ( [a] => chaprak [c] => dexter [b] => programming )
see here sorting is done and even index is maintained.
View php manual for the array sort function
Get number of days between two dates using MySQL?
How can we know the number of days between two given dates using MySQL ? or find the difference between dates using MySQL ?
Do you ever faced this question in technical interview or technical test?
I know many of you have faced this....... :)
Here is a simple function which can be used to find the number of days between two given dates in MySQL.
Syntax - DATEDIFF(firstdate,seconddate)
so internally it will calculate the difference in MySQL by doing "firstdate minus seconddate", so if firstdate is greater than sencoddate then output will be positive and if firstdate is less than seconddate then output will be negative.
eg. if you use this function as
SELECT DATEDIFF('2009-11-19', '2009-10-15');
in MySQL
then you will get output as 35.
And if you pass parameters as
SELECT DATEDIFF('2009-10-15','2009-11-19')
then you will get output as -35.
Do you ever faced this question in technical interview or technical test?
I know many of you have faced this....... :)
Here is a simple function which can be used to find the number of days between two given dates in MySQL.
Syntax - DATEDIFF(firstdate,seconddate)
so internally it will calculate the difference in MySQL by doing "firstdate minus seconddate", so if firstdate is greater than sencoddate then output will be positive and if firstdate is less than seconddate then output will be negative.
eg. if you use this function as
SELECT DATEDIFF('2009-11-19', '2009-10-15');
in MySQL
then you will get output as 35.
And if you pass parameters as
SELECT DATEDIFF('2009-10-15','2009-11-19')
then you will get output as -35.
Guest counter using PHP
Guest counter using PHP is a script has many people posting, but here I just want to post in order I always remember about this script. he...he...Copy code below and create file text with name "counter.txt"
$filecounter="counter.txt";
$fl=fopen($filecounter,"r+");
$hit=fread($fl,filesize($filecounter));
echo("
bordercolor=#0000FF>");
echo("");
echo("");
echo("You are guest to:");
echo($hit);
echo("");
echo(" ");
echo(" ");
fclose($fl);
$fl=fopen($filecounter,"w+");
$hit=$hit+1;
fwrite($fl,$hit,strlen($hit));
fclose($fl);
?>
$filecounter="counter.txt";
$fl=fopen($filecounter,"r+");
$hit=fread($fl,filesize($filecounter));
echo("
echo("
echo("");
echo("You are guest to:");
echo($hit);
echo("");
echo("
echo("
fclose($fl);
$fl=fopen($filecounter,"w+");
$hit=$hit+1;
fwrite($fl,$hit,strlen($hit));
fclose($fl);
?>
readir () is PHP function is used to read the contents of a directory.
/ / This script is used to display the contents of c: /
if ($ handle = opendir ( "c :/")) (
while (false! == ($ file = readdir ($ handle))) (
echo $ file. "
";
)
closedir ($ handle);
)
?>
if ($ handle = opendir ( "c :/")) (
while (false! == ($ file = readdir ($ handle))) (
echo $ file. "
";
)
closedir ($ handle);
)
?>
Subscribe to:
Comments (Atom)
