*Note: The article presented here is written by authors not affiliated with hashemian.com.
This site is not responsible for any errors, omissions, or objectionable content.
Exercise care before engaging in business with any companies mentioned in this article.

Go to: /articles/2005/09/29/ for other articles.

WebDev: Generating a randon number in PHP

Dear Publisher,

You can publish this article wherever you feel like, as long as you use the resource box. Although it is not necessary to inform me, but I feel happy if some tell me that my article has been posted :-)

-- Amrit

==========================================

Article Title: Generating a randon number in PHP Category: WebDev Copyright (c) 2005 Amrit Hallan

==========================================

Sometimes you need to generate unique randon numbers if you want to assign IDs to your members or assign unique values to your shopping cart items. Here I'm just citing to reason whereas it depends on you for what purpose you'd like to generate a random number in your PHP scripts. I have written a small function that takes one argument. This argument tells the function how many digits you want in the generated number.

For a live example of the article, go to www.aboutwebdesigning.com/2005/09/29/generate-a-random-number-using-php

First, here's the function:

<code>

function random_num($n=5) { return rand(0, pow(10, $n)); }

</code>

If you send no argument to the random_num() function, it generates a 5-digit random number. This is how we use it:

<example>

echo random_num(); gave 96161 when tested and echo random_num(7); gave 5983582 when tested

</example>

This function uses the PHP math function pow() to get the number of digits we want. The function pow() calculates the power of a number like say 10 raised to the power 5. In math we write it like 10 Exp 5. Basically, the real rand() function takes 2 arguments: the lower limit and the upper limit. So if you want to generate a random number that should be greater than 107 and less than 5067, you might get somethin like:

<example>

echo rand(107, 5067); gave 3456 when tested

</example>

Since we normally don't need the upper limits and the lower limits, I've elucidated a generic function that gives you a random number of spedicif number of digits.

===============================================

Amrit Hallan is a freelance copywriter, and a website content writer. He also dabbles with PHP and HTML. For more tips and tricks in PHP, JavaScripting, XML, CSS designing and HTML, visit his blog at www.aboutwebdesigning.com

===============================================

Article Topics
Adsense Advertising Bankruptcy Blog Credit Card
Debt Google Ira Marketing Mortgage
Real Estate Rental Retirement Rss Search Engine
Seo Stocks Tax
Recent Articles

Read Financial Markets  |   Home  |   Blog  |   Web Tools  |   News  |   Articles  |   FAQ  |   About  |   Contact

© 2001-2012 Robert Hashemian
Support the effort
Liked this page?
Please consider creating a link to it
from your Web site.

hashemian.com
هاشمیان.com

Home
Blog
Web Tools
News
Articles
FAQ
About
Contact
Financial Markets

Visits: Powered by hashemian.com

Search Hashemian.com