Skip to content

Instantly share code, notes, and snippets.

View flaviors200's full-sized avatar

Flavio Biscaldi flaviors200

View GitHub Profile
@flaviors200
flaviors200 / casting.php
Created June 12, 2019 15:29
Casting esplicito in PHP
<?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)
@flaviors200
flaviors200 / foreach.php
Created June 10, 2019 14:17
Loop statement: foreach
<?php
$colors = ['blu', 'giallo', 'verde', 'rosso', 'bianco'];
// Prima versione
foreach ($colors as $color) {
echo "$color\n";
}
// Seconda versione
@flaviors200
flaviors200 / for-optimized.php
Created June 10, 2019 13:23
Loop statement: for optimized
<?php
$colors = ['blu', 'giallo', 'verde', 'rosso', 'bianco'];
for ($i = 0, $count = count($colors); $i < $count; $i++) {
echo "$colors[$i]\n";
}
/** Output **
/ blu
/ giallo
/ verde
@flaviors200
flaviors200 / for.php
Last active June 10, 2019 13:09
Loop statements: for
<?php
$colors = ['blu', 'giallo', 'verde', 'rosso', 'bianco'];
for ($i = 0; $i < count($colors); $i++) {
echo "$colors[$i]\n";
}
/** Output **
/ blu
/ giallo
@flaviors200
flaviors200 / do-while.php
Last active June 9, 2019 23:02
Loop statements: do-while
<?php
$attempts = 1;
do {
$coinFlip = random_int(0,1); // 0 testa 1 croce
echo "Lancio n. $attempts: $coinFlip\n";
$attempts--;
} while ($attempts < 10);
@flaviors200
flaviors200 / while.php
Created June 8, 2019 23:31
Loop statements: while
<?php
$money = 10;
while ($money > 0) {
echo "Valore di \$money = $money\n";
$money--;
}
@flaviors200
flaviors200 / switch-statement.php
Last active June 6, 2019 14:18
Conditional statement: switch case
<?php
$day = 'sabato';
switch ($day) {
case 'lunedì':
echo "Oggi è lunedì";
break;
case 'martedì':
echo "Oggi è martedì";
break;
@flaviors200
flaviors200 / ternary-operator.php
Last active June 4, 2019 18:04
Conditional statement: the ternary operator
<?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) {
@flaviors200
flaviors200 / elseif-statement.php
Last active June 6, 2019 14:21
Conditional statements: elseif
<?php
$day = 'sabato';
if ($day == 'lunedì') {
echo "Oggi è lunedì";
} elseif ($day == 'martedì') {
echo "Oggi è martedì";
} elseif ($day == 'mercoledì') {
echo "Oggi è mercoledì";
} elseif ($day == 'giovedì') {
@flaviors200
flaviors200 / else-statement.php
Last active June 4, 2019 13:49
Conditional statement: else
<?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
}