PHP它是一個weak type的程式語言
所以只要字串有值,拿去做if判斷時,就一定會回傳true
但有時候會希望帶進來的參數為字串 "false" 時希望它的boolean為 false
在php5以前的版本可能只能這樣寫
```php
$bool = 'false';
$result = NULL;
if ($bool == 'false' OR !$bool){
$result = FALSE;
}
var_dump($result);
```
不過php5之後的版本可以換一個方式寫了
```php
$bool = filter_var($_GET['bool'], FILTER_VALIDATE_BOOLEAN);
```
這樣$_GET['bool']帶進來的值為 'false','no','null','0' 都會被轉為 FALSE
這個函式就像是JAVA的
```javascript
Boolean.parseBoolean(strBoolean);
```
如果要寫一些比較嚴謹的PHP程式用這個函式可能是比較好的作法...
PS. filter_var這個函式不只可以字串轉boolean、也可以驗證email、ip、url等等....
有興趣的話就上PHP官網查一查吧
2011年11月24日 星期四
2011年8月12日 星期五
php 單例設計模式
單例設計模式
今天在網路上看到的一個名詞
說穿了它就是例用static這個關鍵字
其實static早就php4就已經存在了
但很少看到討論它的功能
再順便討論一下吧
```php
function test()
{
static $a;
if ($a == FALSE)
{
$a = 1;
}
else
{
$a++;
}
echo $a;
}
test(); //輸出 1
test(); //輸出 2
test(); //輸出 3
test(); //輸出 4
/*
所以當你指定了變數為static時
它就不會因為function執行結束後而將變數回收
所以我們這個例用這個方法來實現單例設計模式
*/
class Test
{
function instance()
{
static $instance;
if ($instance == FALSE)
{
$instance = new Test();
}
return $instance;
}
}
$test =& Test::instance();
$test2 =& Test::instance();
$test3 =& Test::instance();
/*
這樣$test,$test2,$test3都會指向同一個物件
以上為PHP4的寫法
PHP4必須帶參考才能將物件指到同一下
那php5怎麼寫呢?
*/
class Test
{
static private $instance;
static public function instance()
{
if (self::$instance == FALSE)
{
self::$instance = new self;
}
return self::$instance;
}
}
$test = Test::instance();
/*
PHP5的CLASS支援這種寫法當然要用這種寫法了啊!
雖然採用PHP4的寫法也行....
但新的東西總是要用一下的是不是
*/
```
2011年8月10日 星期三
FB.api究竟要如何使用?
在使用FB的javascript寫應用程式不外乎最常用到的就是FB.api 但查到的文件不外乎都只是用來做資訊查詢... ex.
```javascript
FB.api({
method:'fql.query',
query:'SELECT uid,first_name,last_name FROM user WHERE is_app_user = 1 AND (uid IN (SELECT uid2 FROM friend WHERE uid1 = me())) '
},function(){
console.info(arguments);
});
// 或者是用Graph API來做資訊查詢 ex.
FB.api(object_id+"/likes",function(){
console.info(arguments);
});
// 但事實上FB.api真正的功能並不只如此 ex.
//讚
FB.api(object_id+"/likes","post",function(){
console.info(arguments);
});
//取消讚
FB.api(object_id+"/likes","delete");
//publish feed 至 https://graph.facebook.com/arjun/feed
FB.api("arjun/feed",{message:'hello'},"post",function(){
console.info(arguments);
});
```
帶入參數的個數和型態FB.api會自動去判断 善用這些東西很多事情就不用都透過php sdk來執行可以大量減少Server的負擔喔
2011年1月14日 星期五
php date日期加減
寫了那麼久的php我到今天才發現原來日期的增減這麼簡單!
看到之前自己寫的函式才發現自己真的很蠢!哈
```php
$currentDate = date("Y-m-d");// current date
echo "Current Date: ".$currentDate;
$date30DaysAdded = date('Y-m-d', strtotime("+30 days"));
echo "30 Days added: ".$date30DaysAdded;
$date10YearsBack = date('Y-m-d', strtotime("-10 years"));
echo "10 Years back: ".$date10YearsBack;
$date2DaysAdded = date('Y-m-d', strtotime("+2 days"));
echo "2 Days Added: ".$date2DaysAdded;
```
標籤:
php
2010年12月12日 星期日
php5 json_decode回傳array
在php5內的json_decode的第二個參數決定回傳值為std class或array
```php
$json = json_encode(['a' => 'b']);
// 回傳 std class
var_dump(json_decode ($json));
// 回傳 array
var_dump(json_decode($json, true));
```
標籤:
php
2010年11月20日 星期六
PHP 的 XOR
唉!真不知道該說PHP不用指定變數型態這件事情到底好還是不好!
這兩天在寫一段加密的程式,有用到XOR 結果JAVA和PHP的流程部份一模一樣
結果加密後的結果確有些不相同 更扯的是XOR沒辦法自己解密成功 = =
到最後才發現XOR的字元必須用ord做轉換再做XOR
還好在網上有找到別人寫好的程式碼
```php
/**
* XOR encrypts a given string with a given key phrase.
*
* @param string $InputString Input string
* @param string $KeyPhrase Key phrase
* @return string Encrypted string
*/
function XOREncryption($InputString, $KeyPhrase){
$KeyPhraseLength = strlen($KeyPhrase);
// Loop trough input string
for ($i = 0; $i < strlen($InputString); $i++){
// Get key phrase character position
$rPos = $i % $KeyPhraseLength;
// Magic happens here:
$r = ord($InputString[$i]) ^ ord($KeyPhrase[$rPos]);
// Replace characters
$InputString[$i] = chr($r);
}
return $InputString;
}
// Helper functions, using base64 to
// create readable encrypted texts:
function XOREncrypt($InputString, $KeyPhrase){
$InputString = XOREncryption($InputString, $KeyPhrase);
$InputString = base64_encode($InputString);
return $InputString;
}
function XORDecrypt($InputString, $KeyPhrase){
$InputString = base64_decode($InputString);
$InputString = XOREncryption($InputString, $KeyPhrase);
return $InputString;
}
```
標籤:
php
2010年10月11日 星期一
chrome font size無法設定小於12px???
今天遇到了一個問題
我將某一區塊的font-size設為10px
IE、Firefox都起作用了,但偏偏Chrome沒效果 我就想是不是我打錯字了!
結果最後上google查
Chrome要將字體大小設於小於12px
必須再加上一個webkit獨有的屬性
-webkit-text-size-adjust:none;
標籤:
css
訂閱:
文章 (Atom)