fizzbuzz.php
<?php /*** * * phpで配列「num_list」に1~100までの整数をセット * tplで「if」と「foreach」を使ってFIZZBUZZを表示 * 3の倍数はFIZZ * 5の倍数はBUZZ * 3と5の公倍数はFIZZBUZZ * それ以外は数字を表示 * */ require("../libs/Smarty.class.php"); $smarty = new Smarty(); for ($i = 1; $i <= 100; $i++) { $num_list[$i] = $i ; } $smarty -> assign("num_list", $num_list); $smarty -> assign("fizz", "FIZZ"); $smarty -> assign("buzz", "BUZZ"); $smarty -> display("fizzbuzz.tpl"); ?>
fizzbuzz.tpl
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> {foreach from = $num_list item = num_i key = num} {if $num is div by 3} {if $num is div by 5} {$fizz}{$buzz}<br /> {else} {$fizz}<br /> {/if} {else} {if $num is div by 5} {$buzz}<br /> {else} {$num}<br /> {/if} {/if} {/foreach} </body> </html>
0 件のコメント:
コメントを投稿