Categories

Tools

bitdefender

1&1 Web Hosting

How to generate the six winning numbers to win the Lottery with PHP


The following example shows how to create six random numbers using PHP.

Code:

<style type="text/css">
	div.lottery-panel {
		background-color:#eee;
		border:1px solid #789;
		font-family:Verdana;
		text-align:center;
		width:200px;
	}
	
	span.lottery-panel-title {
		border-bottom:1px solid green;
		color:green;
		font-family:Verdana;
		font-size:14px;
	}
	
	span.lottery-panel-footer {
		color:gray;
		font-size:12px;
	}
	
	table {
		margin:10px auto;
		width:180px;
	}
	
	td {
		background-color: orange;
		border:1px solid #aaa;
		border-radius:14px;
		color:black;
		font-size:16px;
		height:28px;
		text-align:center;
		width:24px;
	}
</style>

<div class="lottery-panel">
<span class="lottery-panel-title">Here's your lucky numbers!</span>
<table border='0'>
	<tr>
	<?php
	// Declare an array of numbers
	$numbers = array();
					
	for($n=0;$n<6;) {
		// Generate a random number
		$r = rand (1, 90);
		
		// Checking if the number is not in the array
		if(!in_array( $r, $numbers) )  :
			$numbers[$n] = $r;
			$n++;
		endif;
	}
		
	// Order the array in ascending order
	sort($numbers);
	
	// Print the numbers drawn
	for($n=0;$n<6;$n++) {
		if($numbers[$n] == 0)
			echo("<td>?</td>");	
		else
			echo("<td>" . $numbers[$n] . "</td>");
	}
	?>
	</tr>
</table>
<span class="lottery-panel-footer">Good luck ;-)</span>
</div>

This example is useful to understand some PHP functions:

  • - Use arrays
  • - Generate random numbers within a range
  • - Check if a number is present in the array
  • - Sort an array


We do not guarantee that you will win the Lottery with this example, but at least you learned something about PHP.

Posted in PHP by MdmSoft

If our work has been of help, you can help us with a small donation...
Our programmers will thank you!



All information contained in this web site are the property of MdmSoft. The information is provided "as is", MdmSoft will not be liable for any misuse of the code contained in these pages, nor can it be for inaccuracies, grammatical errors or other factors that may have caused damage or lost earnings. MdmSoft is not responsible for the content of comments posted by users.
The examples in this area have the educational and demonstration purposes only, and may be copied only for your reference, but cannot be used for commercial purposes, or for any other purpose, without the express written consent of MdmSoft.
MdmSoft also reserves the right to change, without notice, to your liking this web site, the pages and its sections, and may suspend temporarily or definitely the various services included on this site.
While using this site, you agree to have read and accepted our Terms of Service and Privacy Policy.