thinkphpÄ£°åÖ´ÐÐÒªÁì
thinkphpÊÇÒ»¿îÊ¢ÐеÄphp¿ò¼Ü¡£ËüÓµÓÐÇ¿Ê¢µÄÄ£°åÒýÇ棬¸ÃÄ£°åÒýÇæ¿ÉʵÏÖphp¿ØÖÆÆ÷ÓëhtmlÊÓͼµÄÊèÉ¢£¬Ìá¸ßÁË¿ª·¢Ð§ÂʺͿÉά»¤ÐÔ¡£±¾ÎĽ«ÏÈÈÝthinkphpÖÐÄ£°åÒýÇæ³£Óõļ¸ÖÖÖ´ÐÐÒªÁì¡£
Ò»¡¢ ת´ï±äÁ¿
ת´ï±äÁ¿ÊÇÄ£°åÒýÇæµÄ»ù´¡²Ù×÷£¬¿ÉÒÔ½«PHPÖеıäÁ¿ÔÚÄ£°åÖÐÊä³ö¡£ÀýÈ磺
ÔÚPHP¿ØÖÆÆ÷ÖУ¬ÎÒÃǽç˵ÁËÒ»¸ö±äÁ¿ $name£º
$name = 'John Doe'; $this->assign('name', $name);
µÇ¼ºó¸´ÖÆ
ÔÚHTMLÊÓͼÖУ¬ÎÒÃÇͨ¹ýÄ£°å±êÇ©½«±äÁ¿Êä³ö£º
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
<html> <head> <title>Hello <?php echo ($name); ?></title> </head> <body> <h1>Hello <?php echo ($name); ?></h1> </body> </html>
µÇ¼ºó¸´ÖÆ
ÕâÀïʹÓÃÁËÄ£°åÒýÇæµÄ»ù±¾Óï·¨£¬¼´ $name Ϊģ°å±êÇ©ÖеıäÁ¿Ãû£¬($name) ΪÊä³ö¸Ã±äÁ¿µÄÓï·¨¡£
¶þ¡¢ Ñ»·Êä³ö
Ñ»·Êä³öÊÇÄ£°åÒýÇæÖг£ÓõÄÓï·¨Ö®Ò»¡£ÎÒÃÇ¿ÉÒÔʹÓà foreach Ñ»·Êä³öPHPÖеÄÊý×é¡£ÀýÈ磺
¼ÙÉèÎÒÃÇÔÚPHP¿ØÖÆÆ÷Öнç˵ÁËÒ»¸öÊý×飺
$data = array( array('name'=>'John Doe', 'age'=>20), array('name'=>'Jane Doe', 'age'=>18) ); $this->assign('data', $data);
µÇ¼ºó¸´ÖÆ
ÔÚHTMLÊÓͼÖУ¬ÎÒÃÇʹÓà foreach Ñ»·ÒÀ´ÎÊä³öÊý×飺
<html> <head> <title>Student List</title> </head> <body> <h1>Student List</h1> <ul> <?php foreach($data as $item): ?> <li><?php echo ($item['name']); ?> - <?php echo ($item['age']); ?></li> <?php endforeach; ?> </ul> </body> </html>
µÇ¼ºó¸´ÖÆ
ÕâÀïʹÓÃÁËÄ£°åÒýÇæÖеÄÁ½¸öÓï¾ä£¬foreach ºÍ endforeach£¬ÔÚÑ»·Óï¾äÖÐʹÓà $item ´ú±íÑ»·Êý×éÖеÄÿһ¸öÔªËØ¡£
Èý¡¢ Ìõ¼þÅжÏ
³ýÁËÑ»·Óï¾ä£¬Ä£°åÒýÇ滹֧³ÖÌõ¼þÅжÏÓï¾ä¡£¿ÉÒÔƾ֤PHPÖбäÁ¿µÄÖµÊä³ö²î±ðµÄHTMLÄÚÈÝ¡£ÀýÈ磺
ÔÚPHP¿ØÖÆÆ÷Öнç˵һ¸ö±äÁ¿ $is_valid£º
$is_valid = true; $this->assign('is_valid', $is_valid);
µÇ¼ºó¸´ÖÆ
ÔÚHTMLÊÓͼÖУ¬Ê¹Óà if-else ÅжϱäÁ¿Öµ£¬²¢Êä³ö²î±ðµÄHTMLÄÚÈÝ£º
<html> <head> <title>Welcome</title> </head> <body> <?php if($is_valid): ?> <h1>Welcome</h1> <?php else: ?> <h1>Access Denied</h1> <?php endif; ?> </body> </html>
µÇ¼ºó¸´ÖÆ
ÕâÀïʹÓÃÁËÄ£°åÒýÇæÖÐµÄ if-else Óï¾äºÍ endif ¿¢ÊÂÓï¾ä£¬Æ¾Ö¤±äÁ¿ $is_valid µÄÖµÊä³ö²î±ðµÄÎÊÌâ¡£
ÒÔÉÏÊÇThinkPHPÄ£°åÒýÇæÖг£Óõļ¸ÖÖÖ´ÐÐÒªÁ죬¿ÉÒÔƾ֤ÏÖʵÐèÇóÑ¡ÔñʹÓá£Ä£°åÒýÇæµÄʹÓÿÉÒÔÌá¸ß¿ª·¢Ð§ÂʺʹúÂë¿Éά»¤ÐÔ£¬½¨Ò鿪·¢Ö°Ô±¶à¼ÓʹÓá£
ÒÔÉϾÍÊÇthinkphpÄ£°åÖ´ÐÐÒªÁìµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡