2009年1月29日 星期四

寫PHP必知的單引號和雙引號區別

單引號內的變號,是不會被PHP解譯的。
因為PHP會當成字串處理!
所單引號和雙引號的使用,有必要釐清,建立好的使用習慣。


節錄自
10 Tips That Every PHP Developer Should Know, Part 2

Single and double quotes confused me for some time and it really should not have. I see this quite often in the forum as well. It's very easy to understand that double quotes allow php to parse and single quotes do not. Here are some examples:
$var = $value; // ok
$var = "$value"; // ok, but double quotes are not necessary
$var = '$value'; // will not work (single quotes will not allow parsing)

('.' the period adds/connects variables, functions, etc. together.
Oftentimes programmers will leave spaces around the ' . ' to make
things easier to read.)

$var = 'This is the ' . $value . ' of things.'; // ok - preferred
technique
$var = "This is the $value of things."; // ok, but harder to read/debug
$var = 'This is the $value of things.'; // will not parse $value
$var = This is the $value of things.; // error

$var = $array['name']; // ok, generally the preferred technique
$var = $array["name"]; // ok, but why use double quotes if they are not
necessary?
$var = "$array[name]"; // ok, but harder to read/debug - poor coding
style

$var = 'Name: ' . $array['name']; // ok - preferred technique
$var = "Name: $array[name]"; // ok, but harder to read/debug - poor
coding style
$var = "Name: $array["name"]"; // error
$var = "Name: $array['name']"; // error

exampleFunction($value); // ok
exampleFunction("$value"); // ok, but double quotes are not necessary
exampleFunction('$value'); // will not parse $value

2009年1月28日 星期三

PEAR是寫PHP的好工具!

MySQL

  • 伺服器: Localhost via UNIX socket
  • 伺服器版本: 5.1.30
  • 通訊協定版本: 10
  • 使用者: bullock@localhost
  • MySQL 文字編碼: UTF-8 Unicode (utf8)

Web server

  • Apache/2.2.11 (Unix) DAV/2 mod_ssl/2.2.11 OpenSSL/0.9.8i PHP/5.2.8 mod_apreq2-20051231/2.6.0 mod_perl/2.0.4 Perl/v5.10.0
  • MySQL 客戶端版本: 5.1.30
  • PHP extension: mysql

phpMyAdmin

  • 版本資訊: 3.1.1


環境是用XAMPP,免去安裝和設定LAMP主機的麻煩!
仿照書,來手工打造一個公告系統!
先開發一個純手工的授權資料用資料表吧!

CREATE TABLE `phplab`.`myauth` (
`id` MEDIUMINT( 8 ) NOT NULL AUTO_INCREMENT ,
`username` VARCHAR( 20 ) NOT NULL DEFAULT '',
`passwd` VARCHAR( 32 ) NOT NULL DEFAULT '',
`category` INT( 5 ) NOT NULL DEFAULT '0',
`realname` VARCHAR( 30 ) NOT NULL DEFAULT '',
`appointment` INT( 5 ) NOT NULL DEFAULT '0',
PRIMARY KEY ( `id` )
)
ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci ;


再來建要裝公告內容的資料表!

CREATE TABLE `phplab`.`news` (
`news_id` INT( 10 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`department` VARCHAR( 10 ) NOT NULL DEFAULT '',
`title` TEXT NOT NULL ,
`content` TEXT NOT NULL ,
`bind1` VARCHAR( 255 ) NULL DEFAULT NULL ,
`size1` VARCHAR( 8 ) NULL DEFAULT NULL ,
`bind2` VARCHAR( 255 ) NULL DEFAULT NULL ,
`size2` VARCHAR( 8 ) NULL DEFAULT NULL ,
`bind3` VARCHAR( 255 ) NULL DEFAULT NULL ,
`size3` VARCHAR( 8 ) NULL DEFAULT NULL ,
`download1` SMALLINT( 5 ) NOT NULL DEFAULT '0',
`download2` SMALLINT( 5 ) NOT NULL DEFAULT '0',
`download3` SMALLINT( 5 ) NOT NULL DEFAULT '0',
`readtimes` INT( 10 ) NOT NULL DEFAULT '0',
`username` VARCHAR( 20 ) NOT NULL DEFAULT '',
`datetime` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
`limitdate` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
`appointment` VARCHAR( 10 ) NOT NULL DEFAULT '0'
) ENGINE = MYISAM ;

搜尋此網誌

本站大事記

這個部落格(網站)內容以分享LINUX和延伸出的技術文章為主!
特別是為了工作和進修需要,搜集了不少網站連結。
希望對來這裡觀文的朋友們,有提供一些有用的資訊或文章。
但這裡的文章中,也包含個人的心情扎記和隨興言談……
若是當中沒有對上你的口味,請多包涵!

原「琳娜絲與希斯寇的邂逅」,改名為「愛上琳娜絲」!

原「琳娜絲與希斯寇的邂逅」,改名為「愛上琳娜絲」!
--原序文--
就是當LINUX遇上CISCO啦!他們的結合還能作什麼事…不就是讓這個世界的網路,串…串起來啊…不然你們那能上這網站看部落格!