L'opérateur ternaire en PHP
Advertisement
Advertisement
Advertisement
Advertisement
Advertisement
Read Time:
1 min
() translation by (you can also view the original English article)
A partir de la vidéo
L'opérateur ternaire vous permet de simplifier quelques instructions conditionnelles en PHP. Nous allons voir comment il peut être utilisé, avec le développement et la refactorisation en s'appuyant sur des tests, afin de simplifier le code comme celui-ci :
1 |
<?php
|
2 |
|
3 |
$result = null; |
4 |
|
5 |
if (5>3) { |
6 |
$result = "Bigger"; |
7 |
} else { |
8 |
$result = "Less"; |
9 |
}
|
Ecrit en utilisant un opérateur ternaire, nous pouvons écrire le même code (ci-dessus) comme tel :
1 |
<?php
|
2 |
$result = 5 > 3 ? "Bigger" : "Less"; |
Ceci est évidemment une manière beaucoup plus simple d'écrire et relativement facile à comprendre des instructions conditionnelles et quelque chose que vous devriez prendre en considération lors de la rédaction de vos futur codes.
Advertisement
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Sign upI had my first contact with computers in the mid-80s when I visited my father at work. Probably it was an important moment for what I am doing now. I am a proud member of an agile team working for a company called Syneto.
Through my carrier I programmed in several programming languages and I had the chance to learn and use daily all the major Agile techniques from Scrum to Lean and from TDD to DDD.
Since August 2012 I am sharing my knowledge with the Nettuts+ readers by articles, tutorials and premium courses, all about programming.
Advertisement
Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.