LoginSignup
0
0

More than 5 years have passed since last update.

PHP初学者備忘録

Last updated at Posted at 2018-10-29

0埋めについて

sprintf文字列を返す
printf文字列を出力する

<?php
$int = 315;
printf('%05d', $int);
?>

「%」から始まる部分を特殊な文字として定義しており、
「%d」は10進数の整数、「%s」は文字列を表すことになっている。
%05dで0埋め5桁00315の表示になる。

  • %dや%sのみで使う場合もある。


 <?php
 $num = 5;
 $location = 'tree';

 $format = 'There are %d monkeys in the %s';
 echo sprintf($format, $num, $location);
 ?>
結果
 "There are 5 monkeys in the tree"

  • %1というのは何番目のパラメータを表示させるかということ


 <?php
 $format = 'The %2$s contains %1$d monkeys';
 echo sprintf($format, $num, $location);
 ?>
結果
 'The tree contains 5 monkeys'

その他まとめ

  • PHPはHTMLへの埋め込みが可能
    ソース上ではphpは表示されない。

    ようこそ<?php print $name; ?>さん。

  • echoとprintの違い
    echoはデバイス(プリンターやUSB)に文字を送ることができる。様々な機能に使えるが、
    printは画面表示のみ。使い分けてわかりやすくするためにも、画面表示の時のみはprintを使うのがベスト。

  • PHPはJSと違って、変数宣言は必要ない
    $hp = 200;
    と直接入力すればいい。

  • データ型について
    整数型、文字列型、NULL、論理型
    ※同じ文字列型でも['']を使うと変数や改行の\nも文字として認識される
    [""]は変数や\nもきちんと認識してくれる。文字の場合は''がベスト

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