在PHP7中引入了一个新特性太空船操作符。它用于比较两个表达式。当第一个表达式分别小于、等于或大于第二个表达式时,它返回-1、0或1。
<?php //integer comparison print( 1 <=> 1);print("\n"); print( 1 <=> 2);print("\n"); print( 2 <=> 1);print("\n"); print("\n"); //float comparison print( 1.5 <=> 1.5);print("\n"); print( 1.5 <=> 2.5);print("\n"); print( 2.5 <=> 1.5);print("\n"); print("\n"); //string comparison print( "a" <=> "a");print("\n"); print( "a" <=> "b");print("\n"); print( "b" <=> "a");print("\n"); ?>
输出结果
0 -1 1 0 -1 1 0 -1 1