文字数や桁数をチェックする

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>文字数と桁数をチェックしたい</title>
</head>
<body>
<?php
# 変数に$_POSTの値を代入します。
$example = @$_POST['example'];

if (strlen($example) > 0) {
  if (ctype_digit($example)) {
    echo '<p>「' . h($example) . '」は' . strlen($example) . '桁です。</p>';
  } else {
    echo '<p>「' . h($example) . '」は' . mb_strlen($example) . '文字です。</p>';
  }
}
function h($string) { // HTMLでのエスケープ処理をする関数
  return htmlspecialchars($string, ENT_QUOTES);
}
?>
<form method="post" action="">
<p>文字か数字を入力してください</p>
<input type="text" name="example" value="" />
<input type="submit" value="送信" />
</form>
</body>
</html>