/    Sign up×
Community /Pin to ProfileBookmark

Generate a static random number?

hi

Is there any way to generate a random number and then assign that static number to a variable?

e.g. $rand = static rand();

?

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@JayMFeb 27.2008 — What do you mean by static number?

Did you mean static [I]variable[/I]?
Copy linkTweet thisAlerts:
@treelovinhippieauthorFeb 27.2008 — 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)
Copy linkTweet thisAlerts:
@JayMFeb 27.2008 — 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 [I]static[/I] keyword. But if you're working with procedural code, you don't need to worry about it.
Copy linkTweet thisAlerts:
@treelovinhippieauthorFeb 27.2008 — I need that same variable to be the same in two separate areas though...

[code=php]$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);[/code]
Copy linkTweet thisAlerts:
@TheRaveFeb 27.2008 — $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.

[code=php]$_SESSION["rand"] = rand()."_";[/code]
Copy linkTweet thisAlerts:
@treelovinhippieauthorFeb 28.2008 — hmm, the session thing didn't work. I added session_start() before it as well.
Copy linkTweet thisAlerts:
@treelovinhippieauthorFeb 28.2008 — Don't worry. I ended up just doing a unique identifier using the current date and user IP.
×

Success!

Help @treelovinhippie spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...