Click to See Complete Forum and Search --> : Generate a static random number?
treelovinhippie
02-26-2008, 07:14 PM
hi
Is there any way to generate a random number and then assign that static number to a variable?
e.g. $rand = static rand();
?
What do you mean by static number?
Did you mean static variable?
treelovinhippie
02-26-2008, 07:38 PM
Well yeah if that works... how do I do that?
Basically I'm after:
$rand = rand(); (say result = 1234567)
echo $rand; (result = 1234567)
echo $rand; (result = 1234567)
Unless you re-assign it a value it's going to remain the same. If you're working in an object oriented environment and want the static variable accessible among all classes, you would precede it with the static keyword. But if you're working with procedural code, you don't need to worry about it.
treelovinhippie
02-26-2008, 07:58 PM
I need that same variable to be the same in two separate areas though...
$rand = rand()."_";
$uploadDir = dirname(__FILE__) . '/images/hq/';
$uploadFile = $uploadDir.$rand.basename($_FILES['Filedata']['name']);
if ($_POST['submit'] != '') {
// 1. submitting the html form
if (!isset($_GET['jqUploader'])) {
// 1.a javascript off, we need to upload the file
if (move_uploaded_file ($_FILES[0]['tmp_name'], $uploadFile)) {
// delete the file
// @unlink ($uploadFile);
$html_body = '<h1>File successfully uploaded!</h1>';
} else {
$html_body = '<h1>File upload error!</h1>';
switch ($_FILES[0]['error']) {
case 1:
$html_body .= 'The file is bigger than this PHP installation allows';
break;
case 2:
$html_body .= 'The file is bigger than this form allows';
break;
case 3:
$html_body .= 'Only part of the file was uploaded';
break;
case 4:
$html_body .= 'No file was uploaded';
break;
default:
$html_body .= 'unknown errror';
}
$html_body .= 'File data received.';
}
$dirr = dirname(__FILE__) . '/images/hq/'.$rand.$_POST["header"];
$size = getimagesize($dirr);
TheRave
02-27-2008, 03:13 AM
$rand will remain the same throughout the code unless you reassign it.
If you need/want it to persist between each run of the code try assigning it to a session variable.
$_SESSION["rand"] = rand()."_";
treelovinhippie
02-27-2008, 08:42 PM
hmm, the session thing didn't work. I added session_start() before it as well.
treelovinhippie
02-27-2008, 11:59 PM
Don't worry. I ended up just doing a unique identifier using the current date and user IP.