[閒聊] LPC & php 語法對照

看板mud_sanc (Sanctuary - 聖殿)作者 (揮淚斬馬雲)時間7年前 (2018/09/06 10:06), 7年前編輯推噓2(201)
留言3則, 1人參與, 7年前最新討論串1/1
有自架 linux、且已安裝 apache、yum 了 php 腳本相關檔的, php 腳本的檔案基本上如下 #!/usr/bin/php -q <?php . . <- 中間包著的就是程式內容 . ?> php 腳本有著 php 語法的直譯、大部份時候不需事先宣告變數 、以及可像 VB 執行檔那樣執行的好處,而且與 LPC 有很多共 通名稱的函數,相當方便。 LPC php ========================================================================== int x,y,z; 不用宣告,可直接使用 $x,$y,$z string str,tmp; 不用宣告,可直接使用 $str,$tmp mixed tmps; $tmps=null; mixed tmps=({}); $tmps=array(); mixed tmps=({1,2,3}); $tmps=array(1,2,3); mapping datas; $datas=array(); x=1; $x=1; str="hi"; $str="hi"; tmps=({1,2,3}); $tmps=[1,2,3]; $tmps=array(1,2,3); tmps=({"hello","world"}); $tmps=["hello","world"]; $tmps=array("hello","world"); datas=(["a":"abc","x":"xyz"]); $datas=(["a"=>"abc","x":"xyz"]); $datas=array("a"=>"abc","x":"xyz"); x++; $x++; str+="hey"; $str.="hey"; tmps+=({4}); $tmps[]=4; datas["h"]="hij"; $datas["h"]="hij"; keys_data=keys(datas); $keys_data=array_keys($datas); foreach(tmp in tmps) foreach($tmps as $tmp) foreach(tmp1,tmp2 in datas) foreach($datas as $tmp1 => $tmp2) 原本以為沒有這東西,感謝 typers tmp=sprintf("%-s %3d",str,x); $tmp=sprintf("%-s %3d",$str,$x); str=replace_string(str,"h","H"); $str=str_replace("h","H",$str); 比方 2..5 共取出長度4的子字串 2 5-2+1=4 tmp=str[a..b]; $tmp=substr($str,a,b-a+1); tmp=str[a..strlen(tmp)-1]; $tmp=substr($str,a); tmp=implode(tmps,","); $tmp=implode(",",$tmps); tmps=explode(tmp,","); $tmps=explode(",",$tmp); write(str+"\n"); echo($str."\n"); 或者 echo("$str\n"); write(identify(datas)); var_dump(datas); $tmp=read_file("/file/a.txt"); $tmp=file_get_contents("/file/a.txt"); write_file("/file/a.txt",tmp); file_put_contents("/file/a.txt",$tmp); if(undefinedp(tmps)) if(tmps==null) if(undefinedp(datas["xxx"]) if(isset($datas["xxx"])==FALSE) 也有使用 empty() 的語法, 不過這個就夠用了 if(!undefinedp(tmps)) if(is_array(tmps)) tmps=sort_array(tmps, asort(tmps); (: sort_tmps :)); rsort(tmps); ksort(tmps); 呼叫不同函數有不同的sort . . tmp=tmps[0]; $tmp=$tmps[0]; tmp=tmps[0][1]; $tmp=$tmps[0][1]; tmp=datas["a"]; $tmp=$datas["a"]; tmp=datas["a"]["b"]; $tmp=$datas["a"]["b"]; ========================================================================== 大概瞭解這些,讀檔進行字串拆解、資料分析與儲存、然後再做格式化 輸出就沒啥問題,再怎樣都能土法煉鋼。 (所以我暫時只摸到這裡而已,與資料庫的連結部份暫時不碰) php 的陣列其實廣義來說都是 mapping,例如 $tmps=["a","b","c"]; 它其實是 $tmps=[0=>"a",1=>"b",2=>"c"]; 所以 $tmps[0]="a" 既合理且直覺 foreach($tmps as $tmp) 就相當於 foreach($tmps as $i => $tmp) 其中 $i 在這裡就象徵著陣列索引值,就能替代 for(i=0;i<n;i++) 的傳統用法,直接用 foreach 就能做大部份的處理。 而且它的陣列是 mixed mapping,也就是陣列索引值可同時並存數值 與字串。 讀網頁檔時則必須進行轉換,我的轉換方式是 $fh= file_get_contents('https://www.revivalworld.org/mud/taiwanmudlist'); $encode=mb_convert_encoding($fh,'BIG5','UTF-8'); 這個也是 try 出來的,它的意思是先對讀回來的東西做 UTF-8 轉換, 然後再對轉換後的東西再做 BIG5 轉換,有可能是 sanc 的主機所灌的 linux 版本的設定的緣故。 Laechan -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 122.117.106.224 ※ 文章網址: https://www.ptt.cc/bbs/mud_sanc/M.1536199604.A.38C.html

09/06 12:01, 7年前 , 1F
LPC mapping foreach 語法: foreach (k, v in data)
09/06 12:01, 1F
喔喔,這個讚。 ※ 編輯: laechan (122.117.106.224), 09/07/2018 00:23:03 多補了一些東西,以後會陸續補充。 理論上,會寫 php 的人,來 sanc coding 應該是沒問題的。 ※ 編輯: laechan (122.117.106.224), 09/07/2018 00:29:20

09/07 01:21, 7年前 , 2F
其實程式語言的語法都差不多,倒是觀念差異比較大
09/07 01:21, 2F

09/07 01:29, 7年前 , 3F
LPC在觀念上其實比較接近javascript
09/07 01:29, 3F
文章代碼(AID): #1Ra8kqEC (mud_sanc)
文章代碼(AID): #1Ra8kqEC (mud_sanc)