Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites

(singke) #1

integer rand(integer lowest, integer highest)


The rand function returns a number between the two optional arguments, inclusive. If
left out, zero and the integer returned by the getrandmax function will be used. Use the
srand function to seed the random number generator.


<?
srand(time());


//get ten random numbers from -100 to 100
for($index = 0; $index < 10; $index++)
{
print(rand(-100, 100). "
\n");
}
?>


srand(integer seed)


The srand function seeds the random number generator. It is best to call this function
once before using the rand function.


string tempnam(string directory, string prefix)


The tempnam function returns the complete path to a unique temporary filename. This
guarantees you will not overwrite an existing file. You must take the responsibility to
create and then destroy the file.


The directory argument specifies a directory to put the file in, but it will be discarded if
a default temporary directory is defined in an environment variable. Under UNIX this
variable is called TMPDIR. Under Windows it is called TMP.


You must also specify a prefix for the file, but you may pass an empty string. It's a good
idea to pass a meaningful prefix, which will allow you to distinguish between temporary
files created by different processes. Note that no suffix is added to the file. Under
Windows you may want to add .tmp or some other file extension.


An empty string is returned on failure. This function is similar to uniqid.


<?
$myFile = tempnam("C:\temp", "data");
if(strlen($myFile) > 0)
{
print($myFile);
}
else
{
print("Couldn't make temporary name");

Free download pdf