PHP-Cookbook-chapter4

程式

//'4.1'使用檔案常數
$session_name = $session_txt['4.1'];
echo $session_name."<br>";

//require_once 'DB.php';

function ferror( $file,
				 $line,
				 $message = 'General Error'){
	$errmsg = "There was an error in script $file,on $line :$message \n";
	error_log($errmsg);
	die ('An error occurred, it has been logged in the system log');
				 }

//$dbh = DB::connect("mysql://user:secret@localhost/dbname");
//if (!$dbh){
	//$dbh = @ferror(__FILE__,__LINE__,sprintf('[%d]:%s'),
	//$dbh->getCode(),$dbh->getMessage());
//}
	
$message="
<fieldset><legend><font color=red>TIPS</font></legend>
   此函式,它從使用者端接受參數\$file、\$line、和\$message,並<br>
   經過格式美化之後印出,使結果較容易閱讀。我們也為\$message指定<br>
   一個預設值,以便可以忽叫此函式時省略\$message這個參數。
<br>
</fieldset>
";
echo $message."<br>";
//'4.2'PHP的作業系統和版本常數
$session_name = $session_txt['4.2'];
echo $session_name."<br>";

switch (PHP_OS){
	case "WIN32";
		//win32_function();
		break;
	case "Linux";
		//linux_function();
		break;
	case "OS/2";
		//os2_function();
		break;
}

//error_reporting(E_WARNING);
//PHP_OS ="Linux";
if (!defined(PHP_OS)){
	//die ("OS Detection cannot be done automatically");
}
		
	
$message="
<fieldset><legend><font color=red>TIPS</font></legend>
   當你想寫出一個大小通吃的程式而又不知道作業系統版本時就很有用。
<br>
</fieldset>
";
echo $message."<br>";
//'4.3'用PHP的錯誤常數設定中斷點
$session_name = $session_txt['4.3'];
echo $session_name."<br>";

error_reporting(E_WARNING);
$line = "The \nQuick \nYoung \nProgrammer \nJumped \nOver \nThe
         \nLazy \nOld \nProgrammer";
$line = ereg_replace ("(\n"," ",$line);//mis-matched parentheses
print $line."<br>";


$message="
<fieldset><legend><font color=red>TIPS</font></legend>
   在程式裡,ereg_replace(),第一個參數的常規表示少了一個小括號<br>
   。通常PHP不會回報這個錯誤,但因為我們透過error_reporting()<br>
   呼叫PHP回報常規表示的錯誤,所以現在就會收到錯誤的回報。
<br>
</fieldset>
";
echo $message."<br>";
//'4.4'定義自己的PHP常數
$session_name = $session_txt['4.4'];
echo $session_name."<br>";

define ("HI","Hello World");
print HI."<br>";

/*
function four (){
	return 4;
}
define ("HI","four()");
print HI;
*/

if (!defined ("FIVE"))
	@defined ("FIVE","five");
else
	return;

print FIVE;



$message="
<fieldset><legend><font color=red>TIPS</font></legend>
   此程式用defined()函式定義HI為Hello World。
</fieldset>
";
echo $message."<br>";
/'4.5'PHP的全域變數
$session_name = $session_txt['4.5'];
echo $session_name."<br>";
unset ($message);

reset ($GLOBALS);
while (list($key,$var) = each ($GLOBALS)){
	print "$key =>$var \n<br>\n";
}



$message="
<fieldset><legend><font color=red>TIPS</font></legend>
  此範例使用PHP的list()和each()函式來處理\$GLOBALS陣列。
</fieldset>
";
echo $message."<br>";

PHP-Cookbook-chapter3

程式

//'3.1'陣列宣告
$session_name = $session_txt['3.1'];
echo $session_name."<br>";

$message="
<fieldset><legend><font color=red>TIPS</font></legend>
   我們用到serialize()函式,它會以字串形式傳回某一變數內容,並
   將該字串寫入檔案。在load_data()函式中,我們用implode()和file
   ()將檔案中代表變數的內容字串讀出,並放到\$data變數內。再用
   unserialize()函式將資料轉回原來的變數並傳回來。
<br>
</fieldset>
";
//'3.2' 印出陣列內容
unset ($message); //清空
unset ($session_name);
$session_name = $session_txt['3.2'];
echo $session_name."<br>";


$list = array ("Emily","Jesse","Franklin","Chris");
print substr (implode (',',$list),0);



$i = 0;
while ($i <count($list)){
	print $list[$i++]."<br>";
}
$message="
<fieldset><legend><font color=red>TIPS</font></legend>
   implode()函式會在每個陣列元素後插入逗號,並傳回一個字串,
   接著我們用substr()函式去除逗號。
<br>
</fieldset>
";
echo $message."<br>";
//'3.3' 除去相同的陣列元素
unset ($message); //清空
unset ($session_name);
$session_name = $session_txt['3.3'];
echo $session_name."<br>";

//$unique = array_unique ($dupllicates);

$n_array = array (1 =>"<br>//'3.1'陣列宣告",
		   2 =>"<br>//'3.2' 印出陣列內容",
		   3 =>"<br>//'3.3' 除去相同的陣列元素"
			);
foreach ($n_array as  $element){
	
	if (@$tstarray [$element] <3){
		array_push ($n_array,$element);
		@$tstarray [$element]++;
	}
}

$message="
<fieldset><legend><font color=red>TIPS</font></legend>
   你想取出陣列中唯一的元素,並去除相同的 array_unique()<br>
   另外會出現 Undifine index Undefined variable 對於<br>
   \$tstarray變數,加上@在\$tstarray前方,就不會出現。
<br>
</fieldset>
";
echo $message."<br>";
//'3.4' 擴大或縮小陣列
unset ($message); //清空
unset ($session_name);
$session_name = $session_txt['3.4'];
echo $session_name."<br>";

$list = array ("dog","cat","rabbit","ant","horse","cow");
array_splice ($list,5);

foreach ($list  as $key => $value){
      
	  echo "{$key}: =>{$value} <br>"; //方法一
	 
}

for ($i=0; $i<=count($list);$i++){
	
		  echo @$list[$i]."<br>";   //方法二

}
$message="
<fieldset><legend><font color=red>TIPS</font></legend>
  縮小陣列的大小,本來是50個(PHP4),用array_splice(),去切
<br>
</fieldset>
";
echo $message."<br>";
//'3.5' 合併陣列
unset ($message); //清空
unset ($session_name);
$session_name = $session_txt['3.5'];
echo $session_name."<br>";

$good_guys =array ("Gandalf","Radagast","Sarauman");
$bad_guys =array ("Nazgul","Sauron","Orcs");

//$all_guys = array_merge ($good_guys,$bad_guys);
$all_guys = array_merge_recursive ($good_guys,$bad_guys);
//$all_guys = $good_guys + $bad_guys; 使用+不行


$good_guys = show_array($good_guys);
$bad_guys = show_array($bad_guys);
$all_guys = show_array($all_guys);

$message="
<fieldset><legend><font color=red>TIPS</font></legend>
  使用array_merge()去合併多個陣列,用array_merge_recursive()也可以。
<br>
</fieldset>
";
echo $message."<br>";
//'3.6' 反覆處理陣列元素
unset ($message); //清空
unset ($session_name);
$session_name = $session_txt['3.6'];
echo $session_name."<br>";

function ascii_codes (&$element){
	$element = ord ($element);
}

$nice_line ="What Up everyone ";
$chars = preg_split ("//",$nice_line);
array_walk ($chars,'ascii_codes');

show_array($chars);

function ascii_codes1 (&$element1){
	$element1 = ord ($element1);
}

$nice_line1 ="What Up everyone ";
$chars1 = preg_split ("//",$nice_line1);
for ($i = 0; $i < count ($chars1); $i++){
	$chars1 [$i ] = ascii_codes1($chars1[$i]);
}
//array_walk ($chars1,'ascii_codes1');
//show_array($chars1);

$message="
<fieldset><legend><font color=red>TIPS</font></legend>
   array_walk(),參數 (變數,'函數')。空白的ascii_code=32<br>
   這邊較留意的是變數前加&符號,稱為以指標傳遞。<br>
   不過有個要注意的是,使用物件時,此時應該永遠以指標傳遞。
<br>
</fieldset>
";
echo $message."<br>";
//'3.7' 存取陣列內不同區域
unset ($message); //清空
unset ($session_name);
$session_name = $session_txt['3.7'];
echo $session_name."<br>";

$message="
<fieldset><legend><font color=red>TIPS</font></legend>
    array_slice(變數,n,n),例如array_slice(\$mash_cast,2,2)<br>
	取變數\$mash_cast,第三個取兩個
<br>
</fieldset>
";
echo $message."<br>";


//'3.11'隨機排列陣列中的元素 
unset ($message); //清空
unset ($session_name);
$session_name = $session_txt['3.11'];
echo $session_name."<br>";

srand ((double)microtime()*1000000);
$some_array = range(1,52);
shuffle($some_array);
echo $some_array[0]."<br>";


$message="
<fieldset><legend><font color=red>TIPS</font></legend>
    使用shuffle()函式,隨機改變次序
<br>
</fieldset>
";
echo $message."<br>";
//'3.13'陣列排序 
unset ($message); //清空
unset ($session_name);
$session_name = $session_txt['3.13'];
echo $session_name."<br>";

function cmp_debt ($a,$b){
	if ($a [1] == $b[1]) return 0;
	return ($b [1] >> $a [1]) ? 1 : -1;
}

$buddy_debts = array(array ("John",31),
				     array ("Dave",12),
					 array ("Kris",15),
					 array ("Werner",38),
					 array ("Phil",9));
usort ($buddy_debts,'cmp_debt');
foreach ($buddy_debts as $buddy_debt){
	print $buddy_debt [0]."Owes".$buddy_debt [1];
	print "<br>";
}


$message="
<fieldset><legend><font color=red>TIPS</font></legend>
    usort()函式接受一個陣列,並以使用者自訂的函式來排列
<br>
</fieldset>
";
echo $message."<br>";

PHP-Cookbook-chapter2

程式

//2.1檢查變數值是否為有效數字 
$session_name = "2.1檢查變數值是否為有效數字  ";
echo $session_name."<br>";
// is_int -> 檢查整數, is_float->檢查浮點數字, is_numeric->檢查字串裡為數字
$num1 = 'test';
if (is_float ($num1) || is_int($num1)) {
	print '$num1 是數字 <br>';
}elseif (is_numeric ($num1)){
	print '$num1 可能不是數字,但其內容為數值<br>';
}else {
	print '$num1 既不是數字,也非數值字串<br>';
}
//2.2處理一連串數字
$session_name = "2.2處理一連串數字  ";
echo $session_name."<br>";
$x =10;
$y =20;

for ($i = $x; $i <= $y; $i++){
	//$i 將會是$x到$y之間的整數
	print "$i \n";
}


$items = array ( "Linux",
				 "Apache",
				 "PHP",
				 "A",
				 "SQL",
				 "Server",
				 "Talented",
				 "Administrator");
				 
for ($i = 2; $i <=6; $i++){
     print "$items[$i]<br>";
}
//2.10 設定數字輸出格式  number_format		
$session_name = "2.10 設定數字輸出格式 ";
echo $session_name."<br>";
$num = 12500000.8356;
$num = number_format($num,2); //取到小數2位
print "$num<br>";
//2.11弧度與角度的轉換
$num1 = 90;
$num2 = 3.14;

$rad = deg2rad ($num1);
$deg = rad2deg ($num2);
print "角度 $num1 轉成弧度為 $rad <br>";
print "弧度 $num2 轉成角度為 $deg 度 <br>";
//2.13產生亂數
$session_name = "2.13產生亂數 ";
echo $session_name."<br>";

$items = array ( "Linux",
				 "Apache",
				 "PHP",
				 "A",
				 "SQL",
				 "Server",
				 "Talented",
				 "Administrator");

$rand_num = mt_rand(0,count ($items)-1);

print "rand items =>$items[$rand_num]";
// 2.14產生一個唯一的隨機數字  用檔案  @->忽略錯誤
$session_name = "<br>##2.14產生一個唯一的隨機數字  用檔案  ";
echo $session_name."<br>";

mt_srand ((double)microtime()*1000000);

$data = implode ("",file ('random_numbers.txt'));
$random_numbers = $data ? unserialize ($data) : array ();

$random =mt_rand();
while (@in_array ($random_numbers,$random));
	$random = mt_rand();
@array_push ($random_numbers,$random);

$data = serialize ($random_numbers);
$fp = fopen ("random_numbers.txt",'w');
if (!$fp){
	die ("Cannot open random_numbers.txt");
}
@fwrite ($fp,$data);
@fclose ($fp);

print "$random  為首次產生";
//2.15 亂數加權
$session_name = "<br>##2.15 亂數加權  ";
echo $session_name."<br>";
$ads =  array ( "Linux" => 1,
				"Apache" => 1,
				"PHP" => 3,
				"A" => 1,
				"SQL" => 4,
				"Server" => 10,
				"Talented" => 1,
				"Administrator" => 4
					);
$j = 0;

foreach ($ads as $company => $weight){
	for ($i = 0 ; $i < $weight ; $i++){
		$dist [$j++] = $company;
		//$j."<br>";
	}
     //echo $j."<br>";	
}
srand((double)microtime()*1000000);
$rand_num = mt_rand(0,$j -1);
@shuffle($dist); //隨機排列陣列元素

$company = $dist [$rand_num ];
print "選擇的公司是:$company<br>"; 
//2.16 將今天日期載入陣列中
$session_name = "<br>##2.16 將今天日期載入陣列中  ";
echo $session_name."<br>";
$da = getdate();
$date = "$da[mon]/$da[mday]/$da[year]";
print $date."<br>";

$datestamp = date ("j/n/Y");
print $datestamp."<br>";

list ($month,$day,$year) = explode ('/',$date);
if (checkdate ($month,$day,$year)){
	print "有效日期<br>";
}else{
	print "無效日期<br>";
}

echo "<br>--------- 日期格式: 月 / 日 / 年  ex:09/06/2024 2024年9月6日 ------<br>";

$check_date = "13/06/2024";
$check_str = is_valid_date($check_date);

echo "$check_date<br>";

$check_str= ($check_str==1) ? "是" : "否";
echo "$check_str<br>";


function is_leap_year($year){
	return ((($year%4)==0 &&($year%100)!=0)||($year%400)==0);
}
function is_valid_date ($date){
	// 把日期拆開
    list($month,$day,$year)=explode ('/',$date);
	$month_days =array(31,28,31,30,31,30,31,31,30,31,30,31);
	
	if ($month <1 || $month >12)
		return false;
	
	//年必須包含四位數字
	if ((strlen($year)!=4) || eregi("[^0-9]",$year))
		return false;
	
	//若是閏年,二月將有29天
	if (is_leap_year($year))
		$month_days [1] == 29;
	
	if ($day < 1 || $day > $month_days [$month-1] )
		return false;
	
	return true;
}
/2.22效能測試
$session_name = "<br>##2.22效能測試  ";
echo $session_name."<br>";

function get_microtime()
{
	$mtime = microtime();
	$mtime = explode(" ",$mtime);
	$mtime = doubleval($mtime[1 ])+doubleval($mtime[0 ]);
	return ($mtime);
}
$time1 = get_microtime();

for ($i=0; $i<99999; $i++){
	$ar []=$i;
}
$time2 = get_microtime();
$diff = abs ($time2-$time1);

print "<br>For 迴圈:$diff 秒<br>";

$i  =0;
unset ($ar);
$time1 = get_microtime();
while ($i<99999){
	$ar [] = $i;
	$i++;
}
$time2 = get_microtime();
$diff = abs ($time2-$time1);

print "<br>For 迴圈:$diff 秒<br>";

Associative Array

關聯式陣列 結合陣列

在電腦科學中,關聯陣列(英語:Associative Array),又稱對映(Map)、字典(Dictionary)是一個抽象的資料結構,它包含著類似於(鍵,值)的有序對。一個關聯陣列中的有序對可以重複(如C++中的multimap)也可以不重複(如C++中的map)。
這種資料結構包含以下幾種常見的操作:
向關聯陣列添加配對
從關聯陣列內刪除配對
修改關聯陣列內的配對
根據已知的鍵尋找配對
  • 陣列大小無須宣告。
  • 各元素的資料型態,不一定要相同。
  • 透過索引值或字串值來存取值。

向量陣列(Actor array)

傳統程式語言(如VB,C,C##,JAVA)的陣列屬於向量陣列

  • 陣列大小事先宣告。
  • 各元素型態都一樣。
  • 透過索引值來存取值。


PHP-Cookbook-chapter1

程式

<?php
//1.1字串剖析 strpos()
$email = 'sterling@php.net';
$user_name = substr ($email,0,strpos($email,'@'));
echo $user_name."<br>";

//1.1字串剖析 unpack()
$str = "Gumbi and PDOC are smelly";
$names = unpack("A5name1/x5/A4name2", $str);
echo $names['name1']."<br>".$names['name2']."<br>";

//1.1字串剖析 unpack()
$str2 = 'A box without hinges, key, or lid, Yet golden treasure inside is hid';
$results = unpack ("A1pronoun/x5/A7adjective/X11/A3noun", $str2);
foreach($results as $key => $value){
      echo "{$key}: =>{$value} <br>";


 
}
//1.3交換變數  list()
$items = array ( "Linux",
		 "Apache",
		 "PHP",
		 "A SQL Server",
		 "Talented Administrator");
print "The essentials for operating your own webserver:\n";

reset ($items);
while (list (, $item) = each ($items)){
		print "$item\n";
}
//1.4將ASCII碼轉換為字元
$letter = chr(67);
$ascii_code = ord ($letter);
$letter = sprintf ("%c",$ascii_code);
echo "<br>{$letter}<br>";
//1.5劃分字元
function str_ord ($str){
	for ($i = 0; $i < strlen ($str); $i++){
	$ascii_values[$i] = ord ($str[$i]);
	}
    return $ascii_values;
}	

$str = "Hello World";
$ascii_values = str_ord ($str);
print "The third letter of \$str, a-> $str[2],";
print "has an ASCII value of $ascii_values[2].<br>";

$ascii_values = unpack ("C*char", $str);
//key char
$ascii_values2 = array_values (unpack("C*char", $str));
//轉換key 0,1...
foreach($ascii_values as $key => $value){
      echo "{$key}: =>{$value} <br>"; 
}
foreach($ascii_values2 as $key => $value){
      echo "{$key}: =>{$value} <br>";
}
//1.6 反轉部分字串 array_reverse是反轉單字字串  strrev是反轉字元
function word_reverse ($str){
	return implode ("",array_reverse (preg_split ("/\s+/",$str)));
}
$str = "A ross by any other name ";
print $str."<br>";

$str_reversed = word_reverse($str);
print $str_reversed."<br>";

$chart_reverse = strrev ($str);
print $chart_reverse."<br>";
//1.8 移除字串中的空白 chop 去除尾部空白 ltrim 去除開頭空白 trim 去除頭尾空白
$str = "red apple     " ;
$str = chop ($str);
echo $str."abc<br>";
$str = "   red apple     " ;
$str = ltrim ($str);
echo "abc".$str."abc<br>";
//1.11 解析URL
$session_name = "1.11 解析URL";
echo $session_name."<br>";

$url = 'http://www.php.net/search.php?show=nource&';
$url .='patter=parse_url&sourceurl=http%3A%2F%2Fwww.php.net%2F ';
$url_parts = parse_url ($url);
foreach ($url_parts as $key =>$value ){
	echo "{$key} => {$value} <br>";
}
//1.14 對字串加密
$session_name = "1.14 對字串加密  支援md5() base64_encode() base64_decode()";
echo $session_name."<br>"; 
/*  無法支援mcrypt
$key = "Sterling Hughes ";
$string = "Hello World" ;
$encrypted = mcrypt_ecb (MCRYPT_TripleDES, $key ,$string, mcrypt_encrypt);
$decrypted = mcrypt_ecb (MCRYPT_TripleDES, $key ,$string, mcrypt_decrypt);
echo $encrypted."<br>";
echo $decrypted."<br>";
*/
/*
$key = pack('H*', "bcb04b7e103a0cd8b54763051cef08bc55abe029fdebae5e1d417e2ffb2a00a3");
$key_size = strlen($key);
echo "key size: ".$key_size."<br>";
$string = "Hello World" ;
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key,
                                 $string, MCRYPT_MODE_CBC, $iv);
$ciphertext = $iv . $ciphertext;
$ciphertext_base64 = base64_encode($ciphertext);
echo  $ciphertext_base64 . "\n";
*/
$data = "Thanks for this idea Zak!";
$hashed_data = md5 ($data);
print "The md5 hash of $data is $hashed_data ";

$data = "Hello World";
$hashed_data = md5 ($data);
print "The md5 hash of $data is $hashed_data <br> ";

$base63_data = base64_encode ($data);
print "The base64_encode hash of $data is $base63_data <br> ";

$decode_data = base64_decode($base63_data);
print "The base64_decode hash of $base63_data is $decode_data <br> ";

三元運算子

你想為變數設定預設值,並且可根據使用者輸入而改變

$file_name = isset ($argv[1]) ? $argv[1] : "php://stdin";

相同

if (isset ($argv[1])){
   $file_name = $argv[1];
} else {
   $file_name = "php//stdin";
}

語法如下

條件?執行(若結果為真):執行(若結果為假)

也可以改寫為

$file_name = $argv[1] or $file_name = "php://stdin";

php +0

php 數字加上一個0會變成?

今天學習一組程式碼,一直納悶著為何sairwolf要加一個數字0,有甚麼用處呢?
原來,加數字零之後,取出的兩位數會變成一位數,當若是要取出兩位數,而需要是個位數時。
如:取出數是09,但是我要9時,加個數字0就會變成9。但是你會說:那不就取1位就好,但是常常是也需要2位數的。
<?php
$or_num="20045";
$count_num=substr($or_num,2,2);
echo $count_num."<br>";
$count_num +=0;
echo $count_num."<br>";

?>

結果

04
4

PHP isset() empty() is_null() 的區別

它們的區別   出處

三個方法都是來判斷是否是空值或有沒有宣告變數的方法,比較容易混淆的是isset(),empty()。

  • isset()檢查變數是否存在
  • empty()檢查變數的值是否為空
  • is_null()檢查變數是否為null
gettype()isset()empty()is_null()
$x is undefinedNULLFALSETRUETRUE
$x = null;NULLFALSETRUETRUE
$x = 0;integerTRUETRUEFALSE
$x = “0”;stringTRUETRUEFALSE
$x = 1;intergerTRUEFALSEFALSE
$x = “”;stringTRUETRUEFALSE
$x = “PHP”;stringTRUEFALSEFALSE

intval

intval  函數

(PHP 4, PHP 5, PHP 7, PHP 8)

intval — 獲取變數的整数值

說明

intval(mixed $value, int $base = 10): int

通过使用指定的进制 base 转换(默认是十进制),返回变量 value 的 int 数值。 intval() 不能用于 object,否则会产生 E_WARNING 錯誤並返回 1。

範例

<?php
echo intval(42);                      // 42
echo intval(4.2);                     // 4
echo intval('42');                    // 42
echo intval('+42');                   // 42
echo intval('-42');                   // -42
echo intval(042);                     // 34
echo intval('042');                   // 42
echo intval(1e10);                    // 10000000000
echo intval('1e10');                  // 10000000000
echo intval(0x1A);                    // 26
echo intval('0x1A');                  // 0
echo intval('0x1A', 0);               // 26
echo intval(42000000);                // 42000000
echo intval(420000000000000000000);   // -4275113695319687168
echo intval('420000000000000000000'); // 9223372036854775807
echo intval(42, 8);                   // 42
echo intval('42', 8);                 // 34
echo intval(array());                 // 0
echo intval(array('foo', 'bar'));     // 1
echo intval(false);                   // 0
echo intval(true);                    // 1
?>

ord

ord   函數

(PHP 4, PHP 5, PHP 7, PHP 8)

ord — 轉換字符串第一个字节为 0-255 之间的值

说明

ord(string $character): int

解析 character 二进制值第一个字节为 0 到 255 范围的无符号整型类型。

如果字符串是 ASCII、 ISO-8859、Windows 1252之类单字节编码,就等于返回该字符在字符集编码表中的位置。 但请注意,本函数不会去检测字符串的编码,尤其是不会识别类似 UTF-8 或 UTF-16 这种多字节字符的 Unicode 代码点(code point)。

该函数是 chr() 的互补函数。

範例:

<?php
declare(encoding='UTF-8');
$str = "?";
for ( $pos=0; $pos < strlen($str); $pos ++ ) {
 $byte = substr($str, $pos);
 echo 'Byte ' . $pos . ' of $str has value ' . ord($byte) . PHP_EOL;
}
?>

輸出:

Byte 0 of $str has value 240
Byte 1 of $str has value 159
Byte 2 of $str has value 144
Byte 3 of $str has value 152