2016年2月7日 星期日

php 的一些基本研究



執行  php file....

php   index.php



PHP 支援八種基本型態 (primitive type)

這八種分別是
  • 布林 (boolean)
  • 整數 (integer)
  • 浮點數 (floating point number)
  • 字串 (string)
  • 陣列 (array)
  • 物件 (object)
  • 資源 (resource)
  • NULL
基本上程式 (program) 會自動判斷變數 (variable) 的資料型態 (data type) , PHP 的變數寫法就比較特別一點,用金錢符號 $ 開頭,其後可接英文字母,如下 $b 為布林型態, $i 為整數型態, $f 為浮點數型態, $s 為字串型態

$b = True; // 布林型態
$i = 1234; // 整數型態
$f = 12.3; // 浮點數型態
$s = "12"; // 字串型態

陣列就比較複雜一點,因為陣列分成兩部份,第一部分為索引,第二部份則是資料,兩者可以是上面四者型態任一,資料還可以是其他的陣列,例如
$a1 = array(10, 20, 30, 40);
$a2 = array(
    False => True,
    33 => "Tony",
    "A" => 33.06,
    6.32 => array(1, 2),
);
建立陣列需要用到內建函數 (function) array() ,索引與資料放在參數列 (parameter list) ,若無提供索引,如 $a1 ,索引會自動由 0 遞增填補,因此存取 $a1 如下
echo $a1[0];
echo $a1[1];
echo $a1[2];
echo $a1[3];

來個  imagettftext 的例子....

由於他需要  PHP GD2 library

sudo apt-get install php5-gd



<?php// Set the content-typeheader('Content-Type: image/png');// Create the image$im imagecreatetruecolor(40030);// Create some colors$white imagecolorallocate($im255255255);$grey imagecolorallocate($im128128128);$black imagecolorallocate($im000);imagefilledrectangle($im0039929$white);// The text to draw$text 'Testing...';// Replace path by your own font path$font 'arial.ttf';// Add some shadow to the textimagettftext($im2001121$grey$font$text);// Add the textimagettftext($im2001020$black$font$text);// Using imagepng() results in clearer text compared with imagejpeg()imagepng($im);imagedestroy($im);?>

改一下例子....

<?php
header('Content-Type: image/png');
$im = imagecreatetruecolor(400, 400);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 399, $white);

// The text to draw
//$text = 'Testing...';
$text = '紀富中';
// Replace path by your own font path
//$font = '/usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf';
$font1 = '/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc';
$font2 = '/usr/share/fonts/truetype/wqy/wqy-microhei.ttc';

// Add some shadow to the text
//imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
imagettftext($im, 30, 0, 40, 40, $black, $font1, $text);
imagettftext($im, 30, 0, 80, 80, $black, $font2, $text);
imagepng($im);
imagedestroy($im);
?>

執行結果如下 :


還有一些可以畫圖畫線的library....

http://jpgraph.net/download/
http://pydoing.blogspot.tw/2012/11/php-How-to-Execute.html

一些字型下載的網址
http://fonts.mobanwang.com/