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?

PHP学習メモ③

Posted at

変数

変数とは「データの入れ物」

変数の宣言

変数を利用する前に
変数の名前とそこに格納できるデータの種類(データ型)を宣言しないといけない
例:$msg = 'YES'
YESという文字型をmsgという変数に格納

<?php
$msg = 'YES' ;
print $msg ; 

変数の命名規則

変数の命名規則
No. 規則 良い例 悪い例
「$名前」の形式 $name、$msg str、msg
アンダースコア( _ ) $date、$_name $123、$1date
アンダースコア( _ )と数値 $name_1、$_msg2 $name-1
大文字 / 小文字 $val、$Val

変数名に使う文字は、
英数字、アンダースコア( _ )に限定する のが無難

予約語は混乱を招くので、利用しないほうがいい
× $if、$for
予約語とはPHPであらかじめ意味が決められた単語のこと

可変変数

変数名を変数の値によって決めることができる 変数のこと
先頭に$$をつけると、変数名を別の変数の値で動的に決定できる

<?php
$x = 'title' ;
$title='PHP';
print $$x ; //結果:PHP 

ここでは $x の内容が評価された結果、$titleと同じになる
したがって、「$$x」=「${$x}」

練習問題

誤りはどれだ
①$1data ②$HO_F ③$文字列 ④$IF ⑤$date-store
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?