📢 Nuovo Corso Laravel API disponibile!

Esercizi Funzioni PHP

Ecco degli esercizi semplici con soluzione per praticare le basi sulla creazione di funzioni in PHP.

Esercizio 1

Scrivere una funzione PHP che accetta due numeri interi come parametri e restituisce la loro somma.
<?php
function somma($num1, $num2) {
return $num1 + $num2;
}
$risultato = somma(5, 3);
echo "La somma è: " . $risultato;
?>

Esercizio 2

Scrivere una funzione PHP che accetta una stringa come parametro e restituisce la sua lunghezza.
<?php
function lunghezza($stringa) {
return strlen($stringa);
}
$lunghezzaStringa = lunghezza("Ciao mondo");
echo "La lunghezza della stringa è: " . $lunghezzaStringa;
?>

Esercizio 3

Scrivere una funzione PHP che accetta un array di numeri come parametro e restituisce la somma di tutti gli elementi.
<?php
function sommaArray($array) {
return array_sum($array);
}
$numeri = [1, 2, 3, 4, 5];
$risultato = sommaArray($numeri);
echo "La somma degli elementi dell'array è: " . $risultato;
?>

Esercizio 4

Scrivere una funzione PHP che accetta un numero intero come parametro e restituisce true se il numero è pari, false altrimenti.
<?php
function isPari($numero) {
return $numero % 2 == 0;
}
$numero = 4;
if (isPari($numero)) {
echo "Il numero è pari";
} else {
echo "Il numero è dispari";
}
?>

Esercizio 5

Scrivere una funzione PHP che accetta una stringa come parametro e restituisce la stringa invertita.
<?php
function invertiStringa($stringa) {
return strrev($stringa);
}
$stringa = "Ciao mondo";
$invertita = invertiStringa($stringa);
echo "La stringa invertita è: " . $invertita;
?>

Esercizio 6

Scrivere una funzione PHP che accetta un array di stringhe come parametro e restituisce un array con le stringhe in maiuscolo.
<?php
function maiuscoloArray($array) {
$risultato = [];
foreach ($array as $stringa) {
$risultato[] = strtoupper($stringa);
}
return $risultato;
}
$stringhe = ["ciao", "mondo"];
$risultato = maiuscoloArray($stringhe);
echo "Le stringhe in maiuscolo sono: " . implode(", ", $risultato);
?>

Esercizio 7

Scrivere una funzione PHP che accetta un numero intero come parametro e restituisce il fattoriale di quel numero.
<?php
function fattoriale($numero) {
if ($numero <= 1) {
return 1;
} else {
return $numero * fattoriale($numero - 1);
}
}
$numero = 5;
$risultato = fattoriale($numero);
echo "Il fattoriale di " . $numero . " è: " . $risultato;
?>

Esercizio 8

Scrivere una funzione PHP che accetta una stringa come parametro e restituisce true se la stringa è un palindromo, false altrimenti.
<?php
function isPalindromo($stringa) {
$stringaInvertita = strrev($stringa);
return strtolower($stringa) == strtolower($stringaInvertita);
}
$stringa = "Anna";
if (isPalindromo($stringa)) {
echo "La stringa è un palindromo";
} else {
echo "La stringa non è un palindromo";
}
?>

Esercizio 9

Scrivere una funzione PHP che accetta un array di numeri come parametro e restituisce l'elemento più grande.
<?php
function elementoMax($array) {
return max($array);
}
$numeri = [5, 2, 8, 4, 9];
$max = elementoMax($numeri);
echo "L'elemento più grande dell'array è: " . $max;
?>

Esercizio 10

Scrivere una funzione PHP che accetta una stringa come parametro e restituisce una nuova stringa senza spazi.
<?php
function rimuoviSpazi($stringa) {
return str_replace(" ", "", $stringa);
}
$stringa = "Ciao a tutti";
$stringaSenzaSpazi = rimuoviSpazi($stringa);
echo "La stringa senza spazi è: " . $stringaSenzaSpazi;
?>