Les 100 fonctions PHP que vous devez savoir – Partie 1
Dans ce tutoriel nous allons découvrir la liste des 100 fonctions natives les plus utilisées en PHP.
Count
Retourne le nombre d’éléments dans un tableau.
<?php $colors=array("Blue","Orange","red"); echo count($colors); ?>
La Sortie:
3
Is_array
Est utilisé pour déterminer si une variable est un tableau ou non.
<?php $tab = array('X','Y','X'); if (is_array($tab)) echo "Ceci est un tableau...."; else echo "Ceci n'est pas un tableau...."; ?>
La sortie:
Ceci est un tableau....
Substr
Renvoie une partie d’une chaîne.
<?php echo substr("Welcom to WayToLearnX", 10); ?>
La sortie:
WayToLearnX
In_array
Recherche une valeur spécifique dans un tableau.
<?php $colors = array("Blue", "Orange", "Red"); if (in_array("Red", $colors)) { echo "Red was found"; } if (in_array("Green", $colors)) { echo "Green was found"; } ?>
La sortie:
Red was found
Explode
Diviser une chaîne en un tableau.
<?php $str = "Welcom to WayToLearnX"; print_r (explode(" ",$str)); ?>
La sortie:
Array ( [0] => Welcom [1] => to [2] => WayToLearnX )
Implode
Renvoie une chaîne à partir des éléments d’un tableau.
<?php $arr = array('Hello', 'World!', 'How', 'are', 'you?'); echo implode(" ",$arr); ?>
La sortie:
Hello World!, How are you?
Str_replace
Diviser une chaîne en un tableau.
<?php echo str_replace("Welcom", "Hello", "Welcom to WayToLearnX!"); ?>
La sortie:
Hello to WayToLearnX!
Strlen
Renvoie la longueur d’une chaîne.
<?php echo strlen("WayToLearnX"); ?>
La sortie:
11
Array_merge
Fusionne un ou plusieurs tableaux en un seul tableau.
<?php $c1=array("blue","orange"); $c2=array("green","red"); print_r(array_merge($c1,$c2)); ?>
La sortie:
Array ( [0] => blue [1] => orange [2] => green [3] => red )
Strpos
Trouve la position du première occurrence d’une chaîne dans une autre chaîne.
<?php echo strpos("I love english, I love english too!","english"); ?>
La sortie:
7Voir aussi la Partie 2.