Apacheは2種類存在する。
Apache ⇒ Webサーバー
Apache ⇒ ライセンス
switch文
switch( 変数 ){
case 値:
実行する処理
break;( 処理を終える場合 )
case 値:
実行する処理
break;( 処理を終える場合 )
default:
実行する処理
break;( 処理を終える場合 )
}
変数とcase:値が一致したとこを実行しますが、break;を書かないと全てのcaseの処理をしてしまう。
問題:switch文を使って春夏秋冬
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>switch文で春夏秋冬</title>
</head>
<body>
<?php
$month = date('g'); // 時。12時間単位。先頭にゼロを付けない。
switch($month){
case 12:
case 1:
case 2:
print "冬です。<br>";
break;
case 3:
case 4:
case 5:
print "春です。<br>";
break;
case 6:
case 7:
case 8:
print "夏です。<br>";
break;
case 9:
case 10:
case 11:
print "秋です。<br>";
break;
}
?>
</body>
</html>
⇩ 配列・連想配列について下記サイトが詳しいです。
配列
array関数を使います。
array()関数はPHPにあらかじめ定義されている関数です。関数とは「ある特定の処理をまとめたもの」なのですが、このarray()関数の場合は配列を定義する役割を持ちます。
例:
$a = array( 'もうすぐ春です' , 'もうすぐ夏です' , 'もうすぐ秋です' , 'もうすぐ冬です' );
連想配列
$fruit = array( "apple" => "リンゴ" , "orange" => "みかん" , "grape" => "ぶどう" );
連想配列の構造
変数 = array( "キー(添字)" => "値" , "キー(添字)" => "値" , "キー(添字)" => "値" );
連想配列とは、このように 「 A
に対応する値は B
, C
に対応する値は D
, ...」 といったデータ構造を意味します。PHPにおける実装は上記の結果の通りです。
この A
C
のことを キー または 添字 、 B
D
のことを 値 と呼びます。
=>(ダブルアロー演算子) = 配列の生成に使われる演算子。連想配列に使う。
<?php
$fruit[ ] = "りんご" ;
$fruit[ ] = "みかん" ;
$fruit[ ] = "ぶどう" ;
/* [ ]の中身を指定しない場合、上から順に、0、1、2が自動で入る。*/
echo $fruit[ 0 ];
echo $fruit[ 1 ];
echo $fruit[ 2 ];
?>
<?php
$fruit = array( "りんご" , "みかん" , "ぶどう" );
echo $fruit[ 0 ];
echo $fruit[ 1 ];
echo $fruit[ 2 ];
?>
<?php
$fruit = array( "apple" => "りんご" , "orange" => "みかん" , "grape" => "ぶどう" );
echo $fruit["apple"]."<br>";
echo $fruit["orange"]."<br>";
echo $fruit["grape"]."<br>";
?>
foreach文
<?php
$fruit = array( "apple" => "りんご" , "orange" => "みかん" , "grape" => "ぶどう" );
foreach( $fruit as $key => $value ){
print "{$key} => {$value} <br>\n";
} // ページのソース上(テキストとして)の改行をするのが『\n』
?>
乱数を使う
rand
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>乱数 rand();</title>
</head>
<body>
<?php
for( $i = 0; $i < 100; $i++ ){
print "乱数=".rand( 10 , 20 )."<br>";
}
?>
</body>
</html>
配列とrand( )関数でおみくじ
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>おみくじ rand( );</title>
</head>
<body>
<h1>乱数</h1>
<?php
$i = rand( 0 ,10 );
$omikuji = array( "大吉" , "中吉" , "小吉" , "吉" , "末吉" , "末小吉" , "凶" , "小凶" , "半凶" , "末凶" , "大凶" );
foreach( $omikuji as $value ){
print $value.">";
}
print "<br><br>あなたの今日の運勢は" .$omikuji[$i]. "です。";
print '<img src=" '.$i.'.jpg">';
?>
</body>
</html>
for文を使った関数でブラウザ上に三角形を表示
↓ 参考関数
// 引数が$cntのasta( );関数を定義
function asta($cnt){
for( $i = 0; $i < $cnt; $i++ ){ // for文で$iが0から$++で1ずつ増え、$cntになるまで{ }の中の処理(ここでは『print "*";』と『print "<br>";』)を繰り返す
print "*"; // *をブラウザ上に表示
print "<br>"; // 改行する
} // forの締め
} // functionの締め
上の関数の引数$cntに適当な値(10とか)を入れて実行すると *が縦に並んでいくと思います。
解答例:1
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>for文を使った関数</title>
</head>
<body>
<?php
print "<tt>";
// ↓ここから引数$cntのasta_1( ); 、引数$cntのasta_2( );関数を定義
function asta_1( $cnt ){
for( $i = 1; $i < $cnt; $i++){
print str_repeat( '*', $i );
print "<br>";
} // function asta_1の中のforの締め
} // function asta_1の締め
function asta_2( $cnt ){
for( $i = $cnt; $i > 0; $i-- ){
print str_repeat( '*', $i );
print "<br>";
} // function asta_2のなかのforの締め
} // function asta_2の締め
// ↑ここまでが関数を定義してるとこ
// ↓ 関数を呼び出します
asta_1(10); // 関数asta_1の引数$cntを10にして実行
asta_2(10); // 関数asta_2の引数$cntを10にして実行
print "</tt>";
?>
</body>
</html>
解答例:2
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>for文を使った関数</title>
</head>
<body>
<?php
print "<tt>";
$size = 10;
for( $i =1; $i < $size; $i++ ){
for( $j =0; $j < $i; $j++ ){
print "*";
}
print "<br>\n";
}
for( $i = $size; $i > 0; $i-- ){
for( $j = 0; $j < $i; $j++ ){
print "*";
}
print "<br>\n";
}
print "</tt>";
?>
</body>
</html>
for文を使った関数でブラウザ上に三角形(逆向き)を表示
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>for文を使った関数</title>
</head>
<body>
<?php
print "<tt>";
// ↓ここから関数を定義していきます。
function asta( $cnt ){
for( $i = 0; $i < $cnt; $i++ ){
print "*";
}
print "<br>";
}
$size = 10;
function sp( $cnt ){
for( $i = 0; $i < $cnt; $i++ ){
print " ";
}
} // ↑ ここまでが関数を定義してるとこ
// ↓関数を呼び出します
$i = 0;
for( $i < 1; $i <= $size; $i++ ){
sp( $size - $i ); // 上で作った関数( function sp($cnt);のほうで、引数$cntを$size-$iにして )の実行
asta( $i ); // 同じく関数( function asta($cnt);のほうで、引数$cntを$iにして )の実行
}
for( $i = 0; $i < $size; $i++ ){
sp( $i ); // 上で作った関数( function sp($cnt);のほうで、引数$cntを$iにして)の実行
asta( $size - $i ); // 同じく関数( function asta($cnt);のほうで、引数$cntを$size-$iにして )の実行
}
print "</tt>";
?>
</body>
</html>