Skip to content

Instantly share code, notes, and snippets.

@summer10920
Last active May 12, 2020 07:26
Show Gist options
  • Save summer10920/ce630e2dd7fc44c369aef712e5a60b53 to your computer and use it in GitHub Desktop.
Save summer10920/ce630e2dd7fc44c369aef712e5a60b53 to your computer and use it in GitHub Desktop.
20200512-php-baseclass-6
<?php
/*可以拿來查看PHP環境,作為檢查上的資訊確認*/
// phpinfo();
/*隨機變數,給兩個數字做為區間範圍*/
echo "rand(1,100)=" . rand(1, 100);
print("<hr>");
/*系統時間與日期*/
$now = date("Y-m-d H:i:s");
echo "系統時間為 " . $now . "<br>";
//date()可以根據想要的format(前面)去顯示,同時也能做增加減少(後面)但必須先把文字轉成時間語
$timelang = strtotime("+6 hours"); //你可以試著echo 這裡會得到一堆數字
$tw = date("Y-m-d H:i:s", $timelang); //台灣時間
echo "台灣時間為 " . $tw . "<br>";
print("<hr>");
//時區宣告
date_default_timezone_set('Asia/Taipei');
echo "現在系統時間為 " . date("Y-m-d H:i:s"); //台灣時間
print("<hr>");
// ceil 無條件進位
// floor 無條件捨去
// round 四捨五入
echo "10÷3=??<br>";
echo "ceil: " . ceil(10 / 3) . "<br>";
echo "floor: " . floor(10 / 3) . "<br>";
echo "round: " . round(10 / 3) . "<br>";
print("<hr>");
// substr 擷取字串
echo substr("abcdefg", 2, 3) . "<br>";
echo mb_substr("不要打架要打去練舞室打", 4, 6);
print("<hr>");
//chr 轉為ascii code=> 0-9, a-z,A-Z = 48-57, 97-122, 65-90
echo chr(97); //a
print("<hr>");
//練習:產生一組隨機密碼,由0-9, a-z,A-Z組成,密碼長度5~12
$num = rand(8, 16);
for ($i = 0; $i < $num; $i++) {
$word = rand(0, 61);
if ($word > 35) echo chr($word + 29);
elseif ($word > 10) echo chr($word + 87);
else echo $word;
}
print("<hr>");
/*custom function*/
function say($who, $num)
{
echo $ai_say = "hello " . $who . "! ";
if ($num > 60) return "You are Great";
}
$name = "Mark";
echo $msg = say($name, 30);
// echo $ai_say; //error_sample
print("<hr>");
function pwd($many)
{
for ($run = 1; $run <= $many; $run++) {
$num = rand(8, 16);
$code = "";
for ($i = 0; $i < $num; $i++) {
$word = rand(0, 61);
if ($word > 35) $code .= chr($word + 29);
elseif ($word > 10) $code .= chr($word + 87);
else $code .= $word;
}
$ary[] = $code;
}
return $ary;
}
$num = 5;
$result = pwd($num);
foreach ($result as $msg) {
echo $msg . "<br>";
}
?>
<hr />
<?php
/*
回家作業1
隨機50~200顆星星填滿你的畫面,每個星星的顏色大小隨機不同
用到php=rand+for與HTML=DIV+style
高手挑戰
背景黑色,星星顏色偏向淺色系。
*/
?>
<style>
body {
margin: 0px;
overflow: hidden;
background-color: black;
}
@keyframes rollstar {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
</style>
<?php
$r_x = rand(1, 1000) / 10; //post x use vw
$r_y = rand(1, 1000) / 10; //post y use vh
$r_fs = rand(1, 5); //font-size
echo '<div style="position:absolute;top:' . $r_y . 'vh;left:' . $r_x . 'vw;font-size:' . $r_fs . 'em";line-height:0;>★</div>';
//先做出一顆黑色的隨機
?>
<?php
//成功在做多顆
$r_num = rand(20, 100); //many star
for ($i = 0; $i <= $r_num; $i++) {
$r_x = rand(1, 1000) / 10;
$r_y = rand(1, 1000) / 10;
$r_ts = rand(1, 3);
$r_clr = rand(150, 255);
$r_clg = rand(150, 255);
$r_clb = rand(150, 255);
echo "
<div style='
position:absolute;
top:" . $r_y . "vh;
left:" . $r_x . "vw;
font-size:" . $r_ts . "em;
color:rgb(" . $r_clr . "," . $r_clg . "," . $r_clb . ");
line-height:0;
animation: rollstar ".$r_ts."s infinite linear;
'>
</div>
";
}
?>
<?php
/*
回家作業2
大樂透電腦選號1~49,每次產生5~10組樂透號組不重複,每組6個號碼
用到array,array_values(),sort(),for,rand(),foreach();
*/
for($i=1;$i<50;$i++) {
$data[]=$i;
}
// maybe you can try it => $data=range(1,49);
$pay=rand(5,10);
echo "<h1>這次購買".$pay."組樂透</h1>";
for($buy=1;$buy<=$pay;$buy++){
$max=48;
$play=$data;
for($j=0;$j<6;$j++){
$get=rand(0,$max);
$mynumber[]=$play[$get];
unset($play[$get]);
$play=array_values($play);
$max--;
}
sort($mynumber);
foreach ($mynumber as $value) echo "[".$value."]";
unset($mynumber);
echo "<hr>";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment