Bifalgorithm post · Computer science one · String Functions

How can you determine the length of a PHP string?

How can you determine the length of a PHP string? (Computer science one · String Functions) — "PHP provides dozens of convenient functions that allow you to pr…

Ideas in this flowchart

  • PHP fournit de nombreuses fonctions pour traiter et modifier les chaînes de caractères.
  • Les fonctions de chaînes permettent notamment d’accéder aux caractères et de modifier leur contenu.
  • La documentation standard contient la liste complète des fonctions prises en charge.
  • En raison de l’histoire de PHP, plusieurs fonctions de la bibliothèque de chaînes du langage C sont également disponibles.
  • Pour accéder aux caractères individuels, il faut connaître la longueur de la chaîne.
  • Un accès à un caractère invalide ne provoque pas d’erreur, mais renvoie null.
  • La fonction strlen() renvoie un entier représentant le nombre de caractères de la chaîne.
  • Pour « Hello World! », strlen() renvoie 12.
  • Pour une chaîne vide, strlen() renvoie 0.
  • Pour une chaîne NULL, strlen() renvoie également 0.
  • La fonction is_null() permet de distinguer une chaîne vide d’une chaîne NULL.
  • La longueur et l’indexation permettent d’itérer sur chaque caractère d’une chaîne.
  • Une boucle for utilise strlen() comme limite et affiche chaque caractère avec son index.
Feed/

How can you determine the length of a PHP string?

No cover

Channel

Computer science one

1 page

"PHP provides dozens of convenient functions that allow you to process and modify strings. We highlight a few of the more common ones here. A full list of supported functions can be found in standard documentation. Because of the history of PHP, many of the same functions defined in the C string library can also be used in PHP.\n\nWhen accessing individual characters in a string, it is necessary that we know the length of the string so that we do not access invalid characters (though doing so is not an error, it just results in null). The strlen() function returns an integer that represents the number of characters in the string.\n\n1 $s = \"Hello World!\";\n\n2 $x = strlen($s); //x is 12\n\n3 $s = \"\";\n\n4 $x = strlen($s); //x is 0\n\n5\n\n6 //careful:\n\n7 $s = NULL\n\n8 $x = strlen($s); //x is 0\n\nAs demonstrated in the last example, strlen() will return 0 for null strings. Recall that we can distinguish between these two situations by using is_null(). Using this function we can iterate over each individual character in a string.\n\n1 $fullName = \"Tom Waits\";\n\n2 for($i=0; $i<strlen($fullName); $i++) {\n\n3 printf(\"fullName[%d] = %s\n\n\", $i, $fullName[$i]);\n\n4 }"

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.