0
0

More than 1 year has passed since last update.

PHPで定数を扱っていこう!

Last updated at Posted at 2021-11-11

PHPで定数を定義していきましょう!

変数は中の値を何度も代入できるけど、

定数にすると値を変える事が出来なくなります!

定数は大文字で書いていきます。

index.php
<?php

const NAME = 'かな';
echo NAME . PHP_EOL;

macターミナルに以下を出力します!

~$ php index.php

以下が出力されました!

~ $ php main.php
かな
~ $ 

同じ定数名に値を代入しようとすると...

index.php
<?php

const NAME = 'かな';
const NAME = 'さやか';
echo NAME . PHP_EOL;

ターミナルにエラーで表示されます!

変数と定数の使い分けを上手くできると良いですね!

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