PHP print()関数
例
出力にテキストを書き込みます。
<?php
print "Hello world!";
?>
定義と使用法
print()関数は、1つ以上の文字列を出力します。
注: print()関数は実際には関数ではないため、括弧を使用する必要はありません。
ヒント: print()関数はecho()よりも少し遅いです。
構文
print(strings)
パラメータ値
Parameter | Description |
---|---|
strings | Required. One or more strings to be sent to the output |
技術的な詳細
戻り値: | 常に1を返します |
---|---|
PHPバージョン: | 4歳以上 |
その他の例
例
文字列変数($ str)の値を出力に書き込みます。
<?php
$str = "Hello world!";
print $str;
?>
例
HTMLタグを含む文字列変数($ str)の値を出力に書き込みます。
<?php
$str = "Hello world!";
print $str;
print "<br>What a nice day!";
?>
例
2つの文字列変数を結合します。
<?php
$str1="Hello world!";
$str2="What a nice day!";
print $str1 . " " . $str2;
?>
例
配列の値を出力に書き込みます。
<?php
$age=array("Peter"=>"35");
print "Peter is " . $age['Peter'] . " years old.";
?>
例
出力にテキストを書き込みます。
<?php
print "This text
spans multiple
lines.";
?>
例
一重引用符と二重引用符の違い。一重引用符は、値ではなく変数名を出力します。
<?php
$color = "red";
print "Roses are $color";
print "<br>";
print 'Roses are $color';
?>
❮PHP文字列リファレンス