Comment vérifier si un array est vide en PHP ?

Réponses rédigées par Antoine
Dernière mise à jour : 2021-01-28 14:20:01
Thèmes : webmaster - php - array
Question

Comment puis-je vérifier si un tableau array est vide en PHP ?

Réponse

Il existe plusieurs méthodes pour vérifier si un tableau PHP, du type array, est vide :

  • La fonction PHP empty().
  • La fonction PHP count().
  • La fonction PHP sizeof().

Vérifier si un tableau est vide avec la fonction empty() :

<?php 
$tableau = array();   
if(empty($tableau)) 
{
    echo "Le tableau est vide";
}
?> 

Vérifier si un tableau est vide avec la fonction count() :

<?php 
$tableau = array();   
if(count($tableau) == 0)
{
    echo "Le tableau est vide";
}
?> 

Vérifier si un tableau est vide avec la fonction sizeof() :

<?php
$tableau = array();   
if(sizeof($tableau) == 0) 
{
    echo "Le tableau est vide";
}
?>