首页 > php开发

PHP中随机产生一组不重复的数字实现程序

<?php
/**
 * PHP获取一组随机数字不重复
 */
$a = microtime();
function createRandID($m){
    // 产生一个从1到$m的数组
    $arr = range(1,$m);
    // 打乱数组
    shuffle ($arr); 
    // 取前十个
    for($i=0;$i<=10;$i++){
        // 赋值给新数组$n
        $n[] = $arr[$i];    
    }
    // 返回这组数字
    return implode($n,',');
}
 
echo createRandID(700000);
echo '<br />';
echo $a - microtime();
?>

阅读全文

php获取google当前天气实现程序

se.php

<?php
$city = $_GET['city'];
$data = createXml($city);
 
$xml = simplexml_load_string($data);
header("Content-type: text/xml");
echo $xml->asXML();
 
// 生成XML数据
function createXml($city)
{
    // Google 天气API
    $weather = simplexml_load_file("http://www.google.com/ig/api?weather={$city}");
    if(isset($weather->weather->forecast_conditions))
    {
        $low = f2c($weather->weather->forecast_conditions->low['data']);
        $high = f2c($weather->weather->forecast_conditions->high['data']);
        return "<weather>n<city>{$city}</city>n<low>{$low}</low>n<high>{$high}</high></weather>n";
    }
    else
    {
        return "<weather>n</weather>n";
    }
}
 
// 华氏度转摄氏度
function f2c($fahrenhite)
{
    return floor(($fahrenhite - 32) / 1.8);
}

阅读全文