Skip to content
Home · Sonraki hedefe 100 XP
Son hareketler
Henüz bir öğrenme hareketi yok. İlk derse başlayın.

Programming Fundamentals

PHP string operations: UTF-8 and character count

When processing text, byte length and number of characters may not be the same. In an application containing Turkish characters, the coding convention must be consistent across all layers.

Bu derste neler var?

SIBERSALDIRI.COM · FOCUSED LESSON

What you will learn

  • Explain the relationship between UTF-8 text and Character operation.
  • “Why does ü make a difference between the two lengths?” To give a reasoned answer to the question.
  • Changing the example and comparing expected and observed behavior.

How it works and where the trust boundary lies

strlen returns the number of bytes, mb_strlen returns the number of characters in the specified encoding. Specify which measure you use when setting limits. Case conversion can affect language conventions; Include examples specifically for I and I. Converting text does not make it safe HTML, SQL or URL; these contexts are treated separately.

PHP string operations: UTF-8 and character count flow: UTF-8 text → Character operation → Notation
UTF-8 text → Character processing → Representation. The diagram shows the relationship; It does not replace the entire application.

Worked example

The following passage makes the concept visible. Evaluate samples containing commands or code in your own separate testing environment, not in the production system. Architectural diagrams and policy examples are not executable programs.

<?php
$text = 'güven';
echo strlen($text), PHP_EOL;
echo mb_strlen($text, 'UTF-8'), PHP_EOL;

Expected result and interpretation

In the UTF-8 file, the result is 6 bytes and 5 characters.

Compare the scenarios

StatusRecommended approachWhy?
ASCII textByte and character can be the sameThey are single-byte characters
UTF-8 TurkishMeasure separate lengthMulti-byte characters are present
User searchTest with language samplesI and I can transform differently

Practice task

Compare the two lengths for ş, İ and blank text; Explain what metric is appropriate for the username limit.

  1. Write down the expected outcome and acceptance condition before the action.
  2. Change a single variable; Record the observation and the medium you used.
  3. Evaluate the result with the comparison table above. Explain which assumption might be wrong if it does not fit.

Common mistake and how to correct it

Do not assume the visible character set and byte size are the same.

Test your understanding

Why does it make a difference between the two lengths?

Answer explained: UTF-8 representation is multiple bytes; Just because it's a single character doesn't mean it's a single byte.

Continue with related lessons

Technical reference

The explanation and examples of this course have been re-prepared for the academy. The link below is a technical reference to verify details of the technology or security approach used.

Bir adım daha ilerlediniz.

Dersi bitirdiyseniz tamamlandı olarak işaretleyin. İlerlemeniz bu cihazda saklanır.

Academy menu

Akademide ara

En az 2 karakter yazın.