HOMEへ
本HPはhttp://webyoutility.web.fc2.com/に移設しました!(現状7割)
PHP講座
PHPとは
PHPの基本 PHP関数
文字列関数 数値関数
正規表現 ファイル操作

目次

max min ceil floor round
rand number_format count array_unshift array_push
array_shift array_pop sort rsort ksort
krsort asort arsort list array_search
array_slice

数値関数

■max()
最大値を出力する
max(数値1,数値2,・・・);
max(配列);
■min()
最小値を出力する
min(数値1,数値2,・・・);
min(配列);
■ceil()
小数切り上げ
$test = 100.33

echo ceil($test);
結果:101
■floor()
小数切り捨て
$test = 100.33

echo floor($test);
結果:100
■round()
四捨五入する round(数値,[小数点以下の桁数]);
$test = 100.33

echo round($test);
結果:100
■rand()
乱数を発生させる
echo rand(1,1000);
結果:1から1000までの乱数
■number_format()
数値のフォーマットに使用
number_format(数値 , [少数点以下の桁数 , 小数点の区切り文字 , 桁区切り文字]);
$test = 12345.6789

echo number_format($test)
結果:12,345

echo number_format($test , 3 , "+" , "*")
結果:12*345+678

配列関数

■count()
配列に含まれている数を調べる
$test = array(10 , 20 , 30);

echo count($test);
■array_unshift()
配列の先頭に要素を追加
$test = array("あ" , "い" , "う");

$result = array_unshift($test , "か" , "き");
結果:かきあいう
■array_push()
配列の最後に要素を追加
$test = array("あ" , "い" , "う");

$result = array_push($test , "え" , "お");
結果:あいうえお
■array_shift()
配列の先頭から要素を削除
$test = array("あ" , "い" , "う");

$result = array_shift($test);
結果:いう
■array_pop()
配列の最後から要素を削除
$test = array("あ" , "い" , "う");

$result = array_pop($test);
結果:あい
■sort()
添字配列の昇順ソート
$test = array("1" , "3" , "2");

sort($test);
結果:1,2,3
■rsort()
添字配列の降順ソート
$test = array("1" , "3" , "2");

rsort($test);
結果:3,2,1
■ksort()
連想配列のキーを昇順ソート
$test = array("1"=>3 , "3"=>2 , "2"=>1);

ksort($test);
結果:2,3,1
■krsort()
連想配列のキーを昇順ソート
$test = array("1"=>3 , "3"=>2 , "2"=>1);

krsort($test);
結果:1,3,2
■asort()
連想配列の値を昇順ソート
$test = array("1"=>3 , "3"=>2 , "2"=>1);

asort($test);
結果:1,2,3
■arsort()
連想配列の値を降順ソート
$test = array("1"=>3 , "3"=>2 , "2"=>1);

asort($test);
結果:3,2,1
※ソート関数($test , [ソート方法])にソート方法を追加できる
ソート方法 内容
SORT_REGULAR 標準比較ソート
SORT_NUMERIC 数値比較ソート
SORT_STRING 文字列比較ソート
SORT_LOCAKE_STRING システムロケール比較ソート
■list()
配列を変数に代入する
$test = array("あ" , "い" , "う");

list($result1 , $result2 , $result3) = $test;

結果:$result1 = あ , result2 = い , result3 = う
■array_search()
配列内の検索
$test = array("あ" , "い" , "う" , "え" , "お");

$result = array_search("う" , $test);
結果:2
■array_slice()
配列内の指定した位置から文字を取り出す array_slice(元配列 , 開始位置 , 終了位置);
$test = array("あ" , "い" , "う" , "え" , "お");

$result = array_slice($test , 2 , 3);
結果:うえ

 

SEO [PR] !uO z[y[WJ Cu