Strings

Joining, length and reversing without extensions.

Strings are joined with a dot: $a . $b. The plus sign tries to add numbers and forces conversion on strings. For text, the dot is the honest choice. Assignment with a dot is .=.

The length in bytes is strlen. In UTF-8 a Cyrillic or accented letter takes more than one byte, so strlen lies as a "letter count" for a non-ASCII nickname. The mbstring extension can count characters, but do not rely on it in the training sandbox. To reverse by characters, use preg_split('//u', ...) when you really need Unicode characters.

Single quotes are almost literal. Double quotes expand variables and sequences like \n. A variable name next to letters goes in curly braces: "{$name}_id". That reads better than a mess of dots once there are three or four pieces.

Do not build HTML and SQL by concatenating request data. Joining two words in an exercise is fine. For a database query, this is how an injection appears. Text and a command are different layers, even if both are strings right now.

Practice

  1. What joins strings

    Which operator joins two strings without turning them into numbers?

    • +
    • .
    • &
    • ++
  2. The result

    What does the expression "go" . "java" equal? Write the string.

  3. Reverse

    Write function reverse($s) that returns the characters of the string in reverse order.