カテゴリー: Uncategorized
EXCEL CONCAT関数
PHP 数学
<?php
$n = 5.6283;
// echo ceil($n) . PHP_EOL; //6
// echo floor($n) . PHP_EOL; //5
// echo round($n) . PHP_EOL; // 6
// echo round($n, 2) . PHP_EOL; //6
// echo mt_rand(1,6) . PHP_EOL;
// echo max(3,9,4) . PHP_EOL;
// echo min(3,9,4) . PHP_EOL;
echo M_PI . PHP_EOL;
echo M_SQRT2 . PHP_EOL;
PHP 基本的な書き方
PHP マルチバイト文字列
<?php
$input = ‘ こんにちは ‘;
$input = trim($input);
echo mb_strlen($input) . PHP_EOL; //5
echo mb_strpos($input, ‘に’) . PHP_EOL; //2
$input = str_replace(‘にち’, ‘ばん’, $input); //こんばんは
echo $input . PHP_EOL;
PHP sprintf
<?php
$name = ‘Apple’;
$score = 32.246;
//$info = “[$name][$score]”;
//echo $info . PHP_EOL;
//$info = sprintf(“[%15s][%10.2f]”, $name, $score);
//echo $info . PHP_EOL;
//$info = sprintf(“[%-15s][%10.2f]”, $name, $score);
//echo $info . PHP_EOL;
printf(“[%-15s][%10.2f]” . PHP_EOL, $name, $score);
PHP 複数の返り値
<?php
function getStats(…$numbers)
{
$total = 0;
foreach ($numbers as $number) {
$total += $number;
}
return [$total, $total / count($numbers)];
}
//print_r(getStats(1,3,5));
//list($sum, $average) = getStats(1,3,5);
[$sum, $average] = getStats(1,3,5);
echo $sum . PHP_EOL;
echo $average .PHP_EOL;
PHP 可変長引数
<?php
//function sum($a, $b, $c)
function sum(…$numbers)
{
//return $a + $b + $c;
$total = 0;
foreach($numbers as $number){
$total += $number;
}
return $total;
}
echo sum(1,3,5) . PHP_EOL;
echo sum(4,2,5,1) . PHP_EOL;
PHP 配列の要素
<?php
$moreScores = [
55,
72,
‘perfect’,
[90, 42,88],
];
$scores = [
90,
40,
…$moreScores,
100,
];
//print_r($scores);
echo $scores[5][2] . PHP_EOL;
PHP foreach
<?php
$scores = [
‘first’ => 90,
‘second’ => 40,
‘third’ => 100,
];
//foreach($scores as $value){
//foreach($scores as $score){
// echo $score . PHP_EOL;
//}
foreach($scores as $key => $score){
echo $key .’ – ‘. $score . PHP_EOL;
}