1
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?

More than 1 year has passed since last update.

【初心者PHP】この整数値は正、負、ゼロ?

Last updated at Posted at 2022-05-17

(参照:http://www.cc.kyoto-su.ac.jp/~mmina/bp1/hundredKnocksBasic.html)

コード

<?php
$m = intval(fgets(STDIN));
if($m > 0){
    echo 'positive';
} else if($m < 0){
    echo 'negative';
} else if($m == 0){
    echo 'zero';
}    
?>

<ポイント>
☆intval() = 文字列(string)を整数(int)にする
☆is_int() = 整数か否か調べる

★ = は「代入」、 == は「等しい」

         ↓ ご指摘を受けて・・・

<?php
$m = intval(fgets(STDIN));
if($m > 0){
    echo 'positive';
} else if($m < 0){
    echo 'negative';
} else {
    echo 'zero';
}    
?>

こちらでも正しい結果が出ました。
教えていただきありがとうございました。

1
0
2

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
1
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?