Wissensportal
PHP
<?php
$tiere = ["Wal", "Gans", "Affe", "Nashorn", "Zebra", "Esel"];
$counter = 0;
// foreach ($tiere as $tier) {
// $counter += 1;
// echo "$counter " . "ist " . $tier . "\n";
// }
foreach ($tiere as $tier) {
$counter += 1;
echo "Nr. $counter " . "ist " . $tier . "\n";
}
?>
PHP (Alternative Syntax)
<?php
$tiere = ["Wal", "Gans", "Affe", "Nashorn", "Zebra", "Esel"];
$counter = 0;
?>
<?php foreach ($tiere as $tier): ?>
<?php
// $counter += 1;
// echo "$counter " . "ist " . $tier . "\n";
?>
<?php endforeach; ?>
<?php foreach ($tiere as $tier): ?>
<?php
$counter += 1;
echo "Nr. $counter " . "ist " . $tier . "\n";
?>
<?php endforeach; ?>
Python
tiere = ["Wal", "Gans", "Affe", "Nashorn", "Zebra", "Esel"]
#0 #1 #2 #3 #4 #5
counter = 0
#for tier in tiere:
# counter += 1
# print(counter)
for tier in tiere:
counter += 1
print(f"Nr. {counter} ist {tier}")