Type Object in PHP

4

I'm practicing some code in PHP when I come across this:

<?php
$a = (object) ["a" => "b"];
$b = (object) ["a"  => "c"];

$y = $a <=> $b;
echo $y;
$v=[1,2,3] <=> [1,2,3];
echo $v;
$a= (object) ["a" => "b"];
$b = (object) ["a" => "b"];
echo $a <=> $b;
echo ($y == -1)? 1:0;
?>

As long as I do not practice PHP my doubts are as follows:

  • Because the outputs of the first three echo are, respectively, -1,0,0 ?

  • How do I do this% type conversion to object?

  • What is the name of this ["a" => "b"] ? comparison operator? Equality? I searched in <=> I did not find anything about it.

  • asked by anonymous 14.11.2018 / 22:26

    1 answer

    4
      

    Why are the outputs of the first three echo being respectively -1,0,0 ?

    According to the definition of the comparison operator (see below) are the expected results. In the first the left operand is smaller, that is, the letter b is smaller than the letter c . In the others the values are identical, so the result is 0.

      

    How do I do this% type conversion to object?

    He did the right thing, though with questionable utility.

      

    What is the name of this ["a" => "b"] ? comparison operator? Equality? I looked at <=> I did not find anything about it.

    See: What is the purpose of the "spaceship operator" < = > of PHP7? .

    $a = (object)["a" => "b"];
    $b = (object)["a"  => "c"];
    $y = $a <=> $b;
    echo $y;
    $v=[1,2,3] <=> [1,2,3];
    echo $v;
    $a= (object)["a" => "b"];
    $b = (object)["a" => "b"];
    echo $a <=> $b;
    echo ($y == -1)? 1:0;
    var_dump($a);
    

    See running on ideone . And in Coding Ground . Also put it in GitHub for future reference .

        
    14.11.2018 / 22:34