PHPの出力方法

print

戻り値として常に1を返す

echoとは違い複数の引数は指定できない

 

var_dump

変数に関する情報を出す。変数の型も出してくれる。

<?php
$test_str  = 'test';
$test_int  = 1;
$test_bool = true;

$test_array = array(
    'test' => array(1, '2', 'aaa', false, 'true')
);

var_dump($test_str);
// 出力結果:string(4) "test" //文字列

var_dump($test_int);
// 出力結果:int(1)  //整数型

var_dump($test_bool);
// 出力結果:bool(true)  //trueかfalseかのデータ型のこと

var_dump($test_array);
/*
出力結果
array(1) {
  ["test"]=>
  array(5) {
    [0]=>
    int(1)
    [1]=>
    string(1) "2"
    [2]=>
    string(3) "aaa"
    [3]=>
    bool(false)
    [4]=>
    string(4) "true"
  }
}
*/

/*
この書き方も使えます!
var_dump($test_str, $test_int, $test_bool, $test_array);
*/

 

print_r

指定した変数に関する情報をわかり易く表示する。

自動的に改行されないので、"\n"を、この出力の下にechoで表示させる必要がある。

引数は一つしか選択できない。

 

<?php
$test_str  = 'test';
$test_int  = 1;
$test_bool = true;

$test_array = array(
    'test' => array(1, '2', 'aaa', false, 'true')
);

print_r($test_str);
echo "\n";
// 出力結果:test

print_r($test_int);
echo "\n";
// 出力結果:1

print_r($test_bool);
echo "\n";
// 出力結果:1

print_r($test_array);
echo "\n";
/*
出力結果
Array
(
    [test] => Array
        (
            [0] => 1
            [1] => 2
            [2] => aaa
            [3] => 
            [4] => true
        )