View casting.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$var1 = 10; | |
$var2 = 6.5; | |
var_dump($var1 + $var2); // Output: float(16.5) | |
$var2 = (int) 6.5; | |
var_dump($var1 + $var2); // Output: int(16) |
View foreach.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$colors = ['blu', 'giallo', 'verde', 'rosso', 'bianco']; | |
// Prima versione | |
foreach ($colors as $color) { | |
echo "$color\n"; | |
} | |
// Seconda versione |
View for-optimized.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$colors = ['blu', 'giallo', 'verde', 'rosso', 'bianco']; | |
for ($i = 0, $count = count($colors); $i < $count; $i++) { | |
echo "$colors[$i]\n"; | |
} | |
/** Output ** | |
/ blu | |
/ giallo | |
/ verde |
View for.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$colors = ['blu', 'giallo', 'verde', 'rosso', 'bianco']; | |
for ($i = 0; $i < count($colors); $i++) { | |
echo "$colors[$i]\n"; | |
} | |
/** Output ** | |
/ blu | |
/ giallo |
View do-while.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$attempts = 1; | |
do { | |
$coinFlip = random_int(0,1); // 0 testa 1 croce | |
echo "Lancio n. $attempts: $coinFlip\n"; | |
$attempts--; | |
} while ($attempts < 10); |
View while.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$money = 10; | |
while ($money > 0) { | |
echo "Valore di \$money = $money\n"; | |
$money--; | |
} |
View switch-statement.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$day = 'sabato'; | |
switch ($day) { | |
case 'lunedì': | |
echo "Oggi è lunedì"; | |
break; | |
case 'martedì': | |
echo "Oggi è martedì"; | |
break; |
View ternary-operator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$year = 2020; | |
echo ($year % 4 == 0) ? "$year è un'anno bisestile" : "$year non è un'anno bisestile"; | |
// Output: 2020 è un'anno bisestile | |
/******* Forma normale ********/ | |
/* | |
/* if ($year % 4 == 0) { |
View elseif-statement.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$day = 'sabato'; | |
if ($day == 'lunedì') { | |
echo "Oggi è lunedì"; | |
} elseif ($day == 'martedì') { | |
echo "Oggi è martedì"; | |
} elseif ($day == 'mercoledì') { | |
echo "Oggi è mercoledì"; | |
} elseif ($day == 'giovedì') { |
View else-statement.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$x = 10; | |
$y = 14; | |
if ($x > $y) { | |
echo "$x è maggiore di $y"; | |
} else { | |
echo "$x è minore o uguale di $y"; // Output: 10 è minore o uguale di 14 | |
} |
NewerOlder