15
14

More than 5 years have passed since last update.

条件分岐中にechoは使えない

Posted at

でもprintは使える。

<?php
    for($i=0;$i++<10;echo  $i); // Parse error: syntax error, unexpected 'echo'
    for($i=0;$i++<10;print $i); // 12345678910

    while($i++<10 && echo  $i); // Parse error: syntax error, unexpected 'echo'
    while($i++<10 && print $i); // 12345678910

    if(echo  $i=1); // Parse error: syntax error, unexpected 'echo'
    if(print $i=1); // 1

    switch(echo  $i=1){} // Parse error: syntax error, unexpected 'echo'
    switch(print $i=1){} // 1

    switch($i=1){
        case echo  $i; // Parse error: syntax error, unexpected 'echo'
    }
    switch($i=1){
        case print $i; // 1
    }

    $i = echo  1 ? true : false; // Parse error: syntax error, unexpected 'echo'
    $i = print 1 ? true : false; // 1
    echo 1 ? true : false; // 1 代入しなければ動く

    $i = echo  1 <=> 1; // Parse error: syntax error, unexpected 'echo'
    $i = print 1 <=> 1; // 1

なぜなのか。

15
14
3

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
15
14