0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PHP 配列と条件

Posted at

#配列

$a = []; ←配列の記述
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
例
<?php
$week_name = ['日','月','火','水','木','金','土'];  ←配列の中身が曜日
print($week_name[1]);
print($week_name[date('w')]);
?>
→月
→入力したその日の曜日が出る

上記のdate('w')は
dateのフォーマットがあり、そこから日にちを持ってきている!!

#連想配列 foreach文

$a = ['key_1'=>'value_1','key_2'=>'value_2'];  連想配列

foreach(配列の変数 as 配列の引数){
  処理したい中身を記述
}
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
例
<?php
$fruits = [
  'apple'=>'りんご',
  'grape'=>'ぶどう',
  'lemon'=>'レモン',
  'tomato'=>'トマト',
  'peach'=>'もも'
];
foreach($fruits as $english => $japan){
  print($english. ':'. $japan. "\n");
}
?>
→apple:りんご
→grape:ぶどう
省略

#if構文

if(出力の条件){
  条件にあった出力
}else{
  条件以外の出力
}
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
例
<?php
if(date('G') < 9) {
print('営業時間です');
}else{
  print('営業時間外です');
}
?>
→時間帯により出力が変わりif文

#小数点

<?php print(100/3000*100). "\n"; ?>
<?php print(floor(100/3000*100)); ?>  floor小数点切り捨て
<?php print(ceil(100/3000*100). "\n"); ?> ceil小数点切り上げ
<?php print(round(100/3000*100, 3)); ?>  round四捨五入,少数の位置を指定
→3.3333333333333
→3 
→4
→3.333
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?