Skip to content
AgileLogics

Programming glossary

String

A value representing text, typically written inside quotes.

Every piece of text a program handles, from a user's name to an error message or an entire web page, is stored as a string. The name comes from characters strung together in order, like beads on a thread. Because it is a sequence, you can measure its length, pick out single characters, cut out sections and join pieces together.

Example

$first = "Grace";
$last = 'Hopper';

$full = $first . " " . $last;           // join with the . operator
echo strlen($full);                     // 12
echo strtoupper($full);                 // GRACE HOPPER
echo "Hello, $first!";                  // Hello, Grace!
echo str_replace("Grace", "G.", $full); // G. Hopper

PHP treats the two quote styles differently: double quotes replace variables such as $first with their values, while single quotes keep the text exactly as typed. Other languages have their own version of this feature, like template literals in JavaScript (`Hello, ${first}`) and f-strings in Python (f"Hello, {first}").

Common pitfalls

Text that looks like a number is not a number. The string "42" and the integer 42 are different values, and mixing them causes surprises. In JavaScript, "4" + 2 gives "42", because + joins the two as text. Convert explicitly with intval() in PHP, int() in Python or Number() in JavaScript.

Strings in Python, Java and JavaScript are immutable, so operations like toUpperCase() hand back a new string and leave the original untouched. Length can also mislead with accented letters and emoji. PHP's strlen() counts bytes, so "café" reports 5 in UTF-8, whereas mb_strlen() counts characters and returns 4.

Handy tools

Last updated