SlideShare une entreprise Scribd logo
1  sur  11
Le spaceship <=>
operator
Webinar PHParty7 France - 28/11/15
Le spaceship operator <=>
Le spaceship operator <=>
Nom officiel :
Combined Comparison Operator
Le spaceship operator <=>
Déja présent dans d’autres langages :
• le Ruby
• le Perl
• présent en Haskell sous la forme de
la fonction ‘compare’
https://wiki.php.net/rfc/combined-comparison-operator
Le spaceship operator <=>
Avant PHP7
Le spaceship operator <=>
Avec PHP7
Le spaceship operator <=>
Retourne :
• 0 si les deux valeurs sont identiques
• 1 si la valeur de gauche est plus grande
• -1 si la valeur de droite est plus grande
Le spaceship operator <=>
Comparaison d’entier
echo 1 <=> 1 // Affiche 0
echo 6 <=> 2 // Affiche 1
echo 2 <=> 6 // Affiche -1
Le spaceship operator <=>
Compare les valeurs ASCII des caractères
echo ‘x’ <=> ‘x’ // Affiche 0
echo ‘x’ <=> ‘z’ // Affiche 1
echo ‘z’ <=> ‘x’ // Affiche -1
echo ‘z’ <=> ‘Z’ // Affiche -1
Le spaceship operator <=>
Utilisé seul, cet opérateur n’a pas
grand intérêt
Il est préférable de l’utiliser avec usort
ou uasort, qui demandent de faire
une comparaison.
Le spaceship operator <=>
function compare ($a, $b)
{
return $a <=> $b
}
$array = [4, 2, 8, 6, 10];
usort($array, ‘compare’);
$array est trié : [2, 4, 6, 9, 10]

Contenu connexe

En vedette

FASE I REDES LOCALES
FASE I REDES LOCALESFASE I REDES LOCALES
FASE I REDES LOCALESrobert8811
 
Nuevo presentación de microsoft power point
Nuevo presentación de microsoft power pointNuevo presentación de microsoft power point
Nuevo presentación de microsoft power pointMario Ceccardi
 
John deere 9650 cts self-propelled combine parts catalog
John deere 9650 cts self-propelled combine parts catalogJohn deere 9650 cts self-propelled combine parts catalog
John deere 9650 cts self-propelled combine parts catalogPartCatalogs Net
 
Design and Implementation of an Unmanned Ground -ICAM_2014_Paper_submission
Design and Implementation of an Unmanned Ground -ICAM_2014_Paper_submissionDesign and Implementation of an Unmanned Ground -ICAM_2014_Paper_submission
Design and Implementation of an Unmanned Ground -ICAM_2014_Paper_submissionThirumalesh H S
 
Lap trinh c_tu_co_ban_den_nang_cao
Lap trinh c_tu_co_ban_den_nang_caoLap trinh c_tu_co_ban_den_nang_cao
Lap trinh c_tu_co_ban_den_nang_caoBác Luân
 
Bulletin Municipal - Décembre 2015 - Saint-Martial de Valette
Bulletin Municipal - Décembre 2015 - Saint-Martial de ValetteBulletin Municipal - Décembre 2015 - Saint-Martial de Valette
Bulletin Municipal - Décembre 2015 - Saint-Martial de ValetteMairie
 

En vedette (8)

FASE I REDES LOCALES
FASE I REDES LOCALESFASE I REDES LOCALES
FASE I REDES LOCALES
 
Nuevo presentación de microsoft power point
Nuevo presentación de microsoft power pointNuevo presentación de microsoft power point
Nuevo presentación de microsoft power point
 
John deere 9650 cts self-propelled combine parts catalog
John deere 9650 cts self-propelled combine parts catalogJohn deere 9650 cts self-propelled combine parts catalog
John deere 9650 cts self-propelled combine parts catalog
 
Design and Implementation of an Unmanned Ground -ICAM_2014_Paper_submission
Design and Implementation of an Unmanned Ground -ICAM_2014_Paper_submissionDesign and Implementation of an Unmanned Ground -ICAM_2014_Paper_submission
Design and Implementation of an Unmanned Ground -ICAM_2014_Paper_submission
 
Inilah 8 makanan terbaik untuk tubuh anda
Inilah 8 makanan terbaik untuk tubuh andaInilah 8 makanan terbaik untuk tubuh anda
Inilah 8 makanan terbaik untuk tubuh anda
 
Lap trinh c_tu_co_ban_den_nang_cao
Lap trinh c_tu_co_ban_den_nang_caoLap trinh c_tu_co_ban_den_nang_cao
Lap trinh c_tu_co_ban_den_nang_cao
 
Fikih kelas x semester ii
Fikih kelas x semester iiFikih kelas x semester ii
Fikih kelas x semester ii
 
Bulletin Municipal - Décembre 2015 - Saint-Martial de Valette
Bulletin Municipal - Décembre 2015 - Saint-Martial de ValetteBulletin Municipal - Décembre 2015 - Saint-Martial de Valette
Bulletin Municipal - Décembre 2015 - Saint-Martial de Valette
 

Similaire à Le spaceship operator

PHP 7 et Symfony 3
PHP 7 et Symfony 3PHP 7 et Symfony 3
PHP 7 et Symfony 3Eddy RICHARD
 
06 - creation-playbook-multi-distributions.pdf
06 - creation-playbook-multi-distributions.pdf06 - creation-playbook-multi-distributions.pdf
06 - creation-playbook-multi-distributions.pdfbibouechristian
 
PHP 5.3, PHP Next
PHP 5.3, PHP NextPHP 5.3, PHP Next
PHP 5.3, PHP NextSQLI
 
Programmation web cours php -- sahmi academy
Programmation web   cours php -- sahmi academyProgrammation web   cours php -- sahmi academy
Programmation web cours php -- sahmi academySoufiane SAHMI
 
PHP - get started
PHP - get startedPHP - get started
PHP - get startedmazenovi
 
Cours3-PHPfgdwfwdffhddfbwdfwdfwdfwdfwfw.pdf
Cours3-PHPfgdwfwdffhddfbwdfwdfwdfwdfwfw.pdfCours3-PHPfgdwfwdffhddfbwdfwdfwdfwdfwfw.pdf
Cours3-PHPfgdwfwdffhddfbwdfwdfwdfwdfwfw.pdfRihabBENLAMINE
 
seance4-1 php.ppt
seance4-1 php.pptseance4-1 php.ppt
seance4-1 php.pptAmineReal
 

Similaire à Le spaceship operator (10)

PHP Training
PHP TrainingPHP Training
PHP Training
 
PHP 7 et Symfony 3
PHP 7 et Symfony 3PHP 7 et Symfony 3
PHP 7 et Symfony 3
 
06 - creation-playbook-multi-distributions.pdf
06 - creation-playbook-multi-distributions.pdf06 - creation-playbook-multi-distributions.pdf
06 - creation-playbook-multi-distributions.pdf
 
Initiation au langage PHP
Initiation au langage PHPInitiation au langage PHP
Initiation au langage PHP
 
PHP 5.3, PHP Next
PHP 5.3, PHP NextPHP 5.3, PHP Next
PHP 5.3, PHP Next
 
Programmation web cours php -- sahmi academy
Programmation web   cours php -- sahmi academyProgrammation web   cours php -- sahmi academy
Programmation web cours php -- sahmi academy
 
Nouveautés php 7
Nouveautés php 7Nouveautés php 7
Nouveautés php 7
 
PHP - get started
PHP - get startedPHP - get started
PHP - get started
 
Cours3-PHPfgdwfwdffhddfbwdfwdfwdfwdfwfw.pdf
Cours3-PHPfgdwfwdffhddfbwdfwdfwdfwdfwfw.pdfCours3-PHPfgdwfwdffhddfbwdfwdfwdfwdfwfw.pdf
Cours3-PHPfgdwfwdffhddfbwdfwdfwdfwdfwfw.pdf
 
seance4-1 php.ppt
seance4-1 php.pptseance4-1 php.ppt
seance4-1 php.ppt
 

Plus de Darkmira

Null Coalescing Operator
Null Coalescing OperatorNull Coalescing Operator
Null Coalescing OperatorDarkmira
 
Return Type Declaration
Return Type DeclarationReturn Type Declaration
Return Type DeclarationDarkmira
 
Uniform Variable Syntax
Uniform Variable SyntaxUniform Variable Syntax
Uniform Variable SyntaxDarkmira
 
Scalar Types Hints
Scalar Types HintsScalar Types Hints
Scalar Types HintsDarkmira
 
Anonymous classes
Anonymous classesAnonymous classes
Anonymous classesDarkmira
 
BC Breaks - Incompatibilites PHP7
BC Breaks - Incompatibilites PHP7BC Breaks - Incompatibilites PHP7
BC Breaks - Incompatibilites PHP7Darkmira
 
Benchmark php7
Benchmark php7Benchmark php7
Benchmark php7Darkmira
 
Webinar PHParty7 - Errors handlings
Webinar PHParty7 - Errors handlingsWebinar PHParty7 - Errors handlings
Webinar PHParty7 - Errors handlingsDarkmira
 

Plus de Darkmira (8)

Null Coalescing Operator
Null Coalescing OperatorNull Coalescing Operator
Null Coalescing Operator
 
Return Type Declaration
Return Type DeclarationReturn Type Declaration
Return Type Declaration
 
Uniform Variable Syntax
Uniform Variable SyntaxUniform Variable Syntax
Uniform Variable Syntax
 
Scalar Types Hints
Scalar Types HintsScalar Types Hints
Scalar Types Hints
 
Anonymous classes
Anonymous classesAnonymous classes
Anonymous classes
 
BC Breaks - Incompatibilites PHP7
BC Breaks - Incompatibilites PHP7BC Breaks - Incompatibilites PHP7
BC Breaks - Incompatibilites PHP7
 
Benchmark php7
Benchmark php7Benchmark php7
Benchmark php7
 
Webinar PHParty7 - Errors handlings
Webinar PHParty7 - Errors handlingsWebinar PHParty7 - Errors handlings
Webinar PHParty7 - Errors handlings
 

Le spaceship operator

  • 1. Le spaceship <=> operator Webinar PHParty7 France - 28/11/15
  • 3. Le spaceship operator <=> Nom officiel : Combined Comparison Operator
  • 4. Le spaceship operator <=> Déja présent dans d’autres langages : • le Ruby • le Perl • présent en Haskell sous la forme de la fonction ‘compare’ https://wiki.php.net/rfc/combined-comparison-operator
  • 5. Le spaceship operator <=> Avant PHP7
  • 6. Le spaceship operator <=> Avec PHP7
  • 7. Le spaceship operator <=> Retourne : • 0 si les deux valeurs sont identiques • 1 si la valeur de gauche est plus grande • -1 si la valeur de droite est plus grande
  • 8. Le spaceship operator <=> Comparaison d’entier echo 1 <=> 1 // Affiche 0 echo 6 <=> 2 // Affiche 1 echo 2 <=> 6 // Affiche -1
  • 9. Le spaceship operator <=> Compare les valeurs ASCII des caractères echo ‘x’ <=> ‘x’ // Affiche 0 echo ‘x’ <=> ‘z’ // Affiche 1 echo ‘z’ <=> ‘x’ // Affiche -1 echo ‘z’ <=> ‘Z’ // Affiche -1
  • 10. Le spaceship operator <=> Utilisé seul, cet opérateur n’a pas grand intérêt Il est préférable de l’utiliser avec usort ou uasort, qui demandent de faire une comparaison.
  • 11. Le spaceship operator <=> function compare ($a, $b) { return $a <=> $b } $array = [4, 2, 8, 6, 10]; usort($array, ‘compare’); $array est trié : [2, 4, 6, 9, 10]