Articles database
 
 
Web AnyArticles.com
Browse by Category:
  Internet and Businesses Online >
  Subcategories
Affiliate Revenue Affiliate Revenue (725)
Auctions Auctions (490)
Audio Streaming Audio Streaming (59)
Blogging RSS Blogging RSS (535)
Domain Names Domain Names (189)
E Books E Books (244)
Ecommerce Ecommerce (395)
Email Marketing Email Marketing (496)
Forums Forums (81)
Internet Marketing Internet Marketing (1737)
Podcasting Podcasting (59)
PPC Advertising PPC Advertising (461)
Security Security (418)
SEO SEO (1482)
Site Promotion Site Promotion (461)
Spam Blocker Spam Blocker (179)
Traffic Building Traffic Building (621)
Video Streaming Video Streaming (41)
Web Design Web Design (1015)
Web Development Web Development (599)
Web Hosting Web Hosting (413)


  Categories :
 
  Arts and Entertainment
  Automotive
  Business
  Communications
  Computers and Technology
  Finance
  Food and Drink
  Health and Fitness
  Home and Family
  Home Based Business
  Internet and Businesses Online
  Kids and Teens
  Legal
  News and Society
  Recreation and Sports
  Reference and Education
  Self Improvement
  Shopping and Product Reviews
  Travel and Leisure
  Womens Interests
  Writing and Speaking
  Random Category
  Leases Leasing
  Small Business
  Shopping
Web Development article : Generating a Randon Number in PHP
 

Internet and Businesses Online > Web Development > Generating a Randon Number in PHP

0 Reviews [ add review ], Article rating : 0.00, 0 votes. Author : 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 http://www.aboutwebdesigning.com/2005/09/29/generate-a-random-number-using-php

First, here's the function:

function random_num($n=5)
{

return rand(0, pow(10, $n));
}

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

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

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:

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

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 http://www.aboutwebdesigning.com


0 Reviews [ add review ], Article rating : 0.00, 0 votes. Author : Amrit Hallan
Rate this story : and read/post review(s)


Article reviews



Post your review
[ Note : no HTML/URLs - will removed automatically ]
Your name
Your comments


More articles from Internet and Businesses Online > Web Development

Add article | Manage Articles | Top Rated articles | Most Reviewed articles | Contact us | Links