https://github.com/heig-vd-progserv1-course
Support de cours · Présentation (web) · Présentation (PDF)
L. Delafontaine, avec l'aide de GitHub Copilot.
Ce travail est sous licence CC BY-SA 4.0.
Cette présentation est un résumé du support de cours. Pour plus de détails, consultez le support de cours.
for
while
do...while
foreach
[]
array()
<?php $fruits = ['apple', 'banana', 'orange', 'kiwi']; echo $fruits[0] . "<br>"; // Affiche 'apple' echo $fruits[1] . "<br>"; // Affiche 'banana' echo $fruits[2] . "<br>"; // Affiche 'orange' echo $fruits[3] . "<br>"; // Affiche 'kiwi'
public class Main { public static void main(String[] args) { String[] fruits = {"apple", "banana", "orange", "kiwi"}; System.out.println(fruits[0]); // Affiche 'apple' System.out.println(fruits[1]); // Affiche 'banana' System.out.println(fruits[2]); // Affiche 'orange' System.out.println(fruits[3]); // Affiche 'kiwi' } }
Ce tableau indexé peut être représenté sous la forme d'une table, composée de paires de clé-valeur :
0
'apple'
1
'banana'
2
'orange'
3
'kiwi'
<?php $mixed = ['apple', 123, true, 3.14]; echo $mixed[0] . "<br>"; // Affiche 'apple' echo $mixed[1] . "<br>"; // Affiche 123 echo $mixed[2] . "<br>"; // Affiche true echo $mixed[3] . "<br>"; // Affiche 3.14
// Équivalent en Java // Il n'est pas possible de créer un tableau // contenant des types différents en Java.
Imaginons maintenant que nous souhaitons représenter une personne à l'aide d'un tableau indexé. Nous pourrions créer un tableau $person qui contient le nom, l'âge et la ville de la personne :
$person
<?php $person = ['John Doe', 30, 'New York']; echo $person[0] . "<br>"; // Affiche le nom de la personne echo $person[1] . "<br>"; // Affiche l'âge de la personne echo $person[2] . "<br>"; // Affiche la ville de la personne
'John Doe'
30
'New York'
Ce n'est pas très intuitif... Solution : les tableaux associatifs.
<?php $person = [ // Les caractères `=>` sont utilisés pour associer // une clé à une valeur 'name' => 'John Doe', 'age' => 30, 'city' => 'New York', ]; echo $person['name'] . "<br>"; // Affiche 'John Doe' echo $person['age'] . "<br>"; // Affiche 30 echo $person['city'] . "<br>"; // Affiche 'New York'
Ce tableau associatif peut être représenté sous la forme d'une table, composée de paires de clé-valeur :
name
age
city
Plus intuitif que le tableau indexé !
// Équivalent en Java // Il n'est pas possible de créer un tableau associatif // en Java, mais nous pouvons utiliser une `HashMap` pour // obtenir un résultat similaire (non décrit ici).
<?php // Un tableau multidimensionnel contenant des tableaux indexés $matrix = [ [1, 2, 3], // Un premier tableau indexé [4, 5, 6], // Un deuxième tableau indexé [7, 8, 9], // Un troisième tableau indexé ]; echo $matrix[0][0] . "<br>"; // Affiche 1 echo $matrix[1][1] . "<br>"; // Affiche 5 echo $matrix[2][2] . "<br>"; // Affiche 9
// Équivalent en Java public class Main { public static void main(String[] args) { // Un tableau multidimensionnel contenant des tableaux indexés int[][] matrix = { {1, 2, 3}, // Un premier tableau indexé {4, 5, 6}, // Un deuxième tableau indexé {7, 8, 9} // Un troisième tableau indexé }; System.out.println(matrix[0][0]); // Affiche 1 System.out.println(matrix[1][1]); // Affiche 5 System.out.println(matrix[2][2]); // Affiche 9 } }
<?php // Un tableau multidimensionnel contenant des tableaux associatifs $users = [ // `'john'` est une clé complètement arbitraire // représentant un premier utilisateur 'john' => [ // Un premier tableau associatif 'name' => 'John Doe', 'age' => 30, 'city' => 'New York', ],
// `'jane'` est une clé complètement arbitraire // représentant un second utilisateur 'jane' => [ // Un deuxième tableau associatif 'name' => 'Jane Doe', 'age' => 25, 'city' => 'Los Angeles', ], ]; echo $users['john']['name'] . "<br>"; // Affiche 'John Doe' echo $users['jane']['age'] . "<br>"; // Affiche 25 echo $users['john']['city'] . "<br>"; // Affiche 'New York'
<?php // Affiche les nombres de 0 à 9 for ($i = 0; $i < 10; $i++) { echo "$i<br>"; }
$i = 0
$i < 10
$i++
// Équivalent en Java public class Main { public static void main(String[] args) { for (int i = 0; i < 10; i++) { System.out.println(i); } } }
<?php $i = 0; // Affiche les nombres de 0 à 9 while ($i < 10) { echo "$i<br>"; $i++; }
public class Main { public static void main(String[] args) { int i = 0; while (i < 10) { System.out.println(i); i++; } } }
<?php $randomNumber = null; do { // La fonction `rand()` génère un nombre aléatoire entre 1 et 10 $randomNumber = rand(1, 10); echo "The random number is $randomNumber<br>"; } while ($randomNumber < 8);
public class Main { public static void main(String[] args) { int randomNumber = null; do { // La fonction `Math.random()` génère un nombre aléatoire // entre 1 et 10 randomNumber = (int) (Math.random() * 10) + 1; System.out.println("The random number is " + randomNumber); } while (randomNumber < 5); } }
<?php $fruits = ['apple', 'banana', 'orange']; // L'ordre des champs ici est inversé par rapport à Java ! foreach ($fruits as $fruit) { echo "$fruit<br>"; }
import java.util.Arrays; import java.util.List; public class Main { public static void main(String[] args) { List<String> fruits = Arrays.asList("apple", "banana", "orange"); // L'ordre des champs ici est inversé par rapport à PHP ! for (String fruit : fruits) { System.out.println(fruit); } } }
<?php $users = [ 'john' => [ 'name' => 'John Doe', 'age' => 30, 'city' => 'New York', ], 'jane' => [ 'name' => 'Jane Doe', 'age' => 25, 'city' => 'Los Angeles', ], ];
// `$user` contient la valeur de l'élément du tableau foreach ($users as $user) { echo "Name: {$user['name']}<br>"; echo "Age: {$user['age']}<br>"; echo "City: {$user['city']}<br>"; echo "<br>"; }
print()
print_r()
echo
<?php $fruits = ['apple', 'banana', 'orange', 'kiwi']; print_r($fruits);
Array ( [0] => apple [1] => banana [2] => orange [3] => kiwi )
count()
<?php $fruits = ['apple', 'banana', 'orange', 'kiwi']; for ($i = 0; $i < count($fruits); $i++) { echo "$fruits[$i]<br>"; }
array_push()
<?php $fruits = ['apple', 'banana', 'orange']; array_push($fruits, 'kiwi', 'pear'); print_r($fruits);
Array ( [0] => apple [1] => banana [2] => orange [3] => kiwi [4] => pear )
Est-ce que vous avez des questions ?
Pour le mini-projet ou les exercices, n'hésitez pas à vous entraidez si vous avez des difficultés !
URLs
Illustrations