PHP scripts and programs for Beginner to Master

php programs, php ready to use scripts & functions, css tutorials, html, javascripts, ajax examples for Beginner to Master

This code can be used to show some random quotes or random links every time the page is loaded or refreshed.




$quote = array(
1 => "Random Quote 1",
2 => "This is sample quote 2",
3 => "check http://programming-in-php.blogspot.com",
4 => "it really works",
5 => "wooooooooooooooooooooow",
6 => "I am on twitter"
);
$randnum = rand(1,6);
echo "
Random Quote - $quote[$randnum]
";
?>


If it is needed to Split string in php and then use those splitted values for some processing in that case you can use this script which Splits string with php explode function and prints array.


$string = "Contents of the string can be splitted by explode function.";
$splitted = explode(" ",$string);
$cnt = count($splitted);
$i=0;
while($cnt > $i)
{
echo "$splitted[$i]";
echo "\n";
$i++;
}
?>

Caution: While using Javascript and an alert message is fired using a timer, then keep the timer for atleast 10 seconds, i.e. 10000. Else, the system will hang. I experienced it recently.

Please note.

Run this program, and watch the color change accelerate.

This part is for Horizontal Menu using CSS.

The below one will come under the Head Tag.


The below one will come under Body Tag.



This code was taken from http://www.w3schools.com/css/tryit.asp?filename=trycss_float5

This is very simple and useful php script if you want to display some random quotes each time a page is visited or refreshed.


$quote = array(
1 => "Random Quote 1",
2 => "This is sample quote 2",
3 => "check http://programming-in-php.blogspot.com",
4 => "it really works",
5 => "wooooooooooooooooooooow",
6 => "I am on twitter"
);
$randnum = rand(1,6);
echo "
Random Quote - $quote[$randnum]
";
?>


This is a very basic version of the script.

Soon you can see the modified version of this script with database support to show random quotes from the database.

Sometimes it is required to show the contents of some text file to the web page. So we can change those contents easily without any problem.

First we will create a simple text file, with some contents.
name it as "test.txt"


These contents are saved in test.txt file.
I am showing you the contents of some text file.

Its very useful to show the contents of the text file in a browser.


Now we will create our php file which will show the contents of the test.txt file to the webpage.


$file1="test.txt";
if (file_exists($file1))
{
$file = fopen("test.txt", "r");
while (!feof($file))
{
$display = fgets($file, filesize("test.txt"));
echo $display . "
";
}
fclose($file);
}
else
{
echo "Error occured ! ! ! Try again or report it to us";
}
?>
?>


Now when we execute this script, it will show the contents of the test.txt to our page.
In this script we are checking whether file exists or not, it it doesnt exists then we will show the error message.

This is a small program which generates a factorial of a number in php.


function doFactorial ($x) {
if ($x <= 1)
return 1;
else
return ($x * doFactorial ($x-1));
}

while($i<100)
{
print $i." --> ".(doFactorial ($i)) . "
";
$i++;
}
?>



Its a very basic program which generates a factorial of small numbers.

sometimes it is required to restrict the length of the textarea so that user should not enter characters beyond a certain limit.
For this we can make use of this simple javascript.

First we will create a 2 textarea.












Here we have created 2 textboxes "mytextarea" and "mytextarea1".
and onkeyup event of the mytextarea we will call our javascript function "restrict(textareaname)", means whenever any key is pressed we will call our javascript function.
and onchange event of the mytextarea we will call our javascript function "restrict(textareaname)", means whenever we will move out of this textbox javascript function will be called.

Here is our javascript function


function restrict(mytextarea) {

var maxlength = 15; // specify the maximum length

if (mytextarea.value.length > maxlength)

mytextarea.value = mytextarea.value.substring(0,maxlength);

} // -->



Include this function either in the head section or at the end before body tag.

Here is full source code to try this


























Today we will be seeing a small easy function to generate a password of specified length.

For this we will be creating a function called "genpwd()"



Now this function is ready, now we want to display the generated password of our specified length.
For this we will print the function value, specifying the length of the password to be generated.



The full working sample example is here, you can copy and paste this code to try it out. It will generate a random string everytime you refresh the page.



function genpwd($cnt)
{
// characters to be included for randomization, here you can add or delete the characters


$pwd = str_shuffle('abcefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890@#%$*');


// here specify the 2nd parameter as start position, it can be anything, default 0
return substr($pwd,0,$cnt);
}

echo genpwd(10); // specify the length of the generated password



UPDATE : 12th September 2009 Hey thanks Filip. Here is one more function to generate a random password suggested by Filip in comments below.


function passwordGenerator($length = null)
{
$pass = md5(time());
if($length != null) {
$pass = substr($pass, 0, $length);
}
return $pass;
}
echo passwordGenerator(6);


UPDATE : 17th December 2009. Hey thanks Fredrik Karlström. Here is one more function to generate a random password suggested by Fredrik Karlström in comments below



function generate_password($len = 8) {
$chars = str_split('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#');
$pwd = '';

for($i=0; $i < $len; $i++)
$pwd .= $chars[rand(1, $sizeof($chars)) -1];
return $pwd;
}

Sometimes we require that when the users click on a textarea at that time all the text should get selected.
This can be done with a small javascript.







When a user clicks on a textarea then all the contents will get selected.

Hi guys,

Generally we require that all the contents of our website should be centered to display the website properly.
For this we can make use of this code so that your website will be in the center of the browser, doesnt matter what is the resolution.

We will create a div with id "outer-div" in the body tag of our webpage.




Now we will apply a CSS styling to the "outer-div" div.




The full code to test is here

Hello,
Today we will be seeing a javascript which will help us to show how many characters are remaining for a textbox or textarea,As soon as the user starts typing in a textbox or textarea, then we will show how many characters are remaining for entering into the textbox.

Here we are creating a simple textbox, and we are specifying the "maxlength" of the textbox to 20.





And "onKeyUp" event of the textbox we are calling a javascript function "countCharacters()".

we will be writing a standard function which accepts only some parameters and rest of the processing will be done in the function.

To this function we have to pass 3 parameters
1) id of the span - where counter will be shown when user types something
2) max_chars - length of the textarea which we specified in maxlength="20"
3) myelement - the id of the textbox where user types text

The function is as below





Now you can use this function anywhere, without a need to modify this function.

The full code to try out is here



Put a link back to our site, if you like this !

Here is a way to convert numbers to words (upto one lakh). It can be extended as per your requirement as well. This is useful for billing etc. Note that only one parameter should be input, and not both. The second parameter is used only for some internal recursive calling.