Foo, Bar, And Baz: Placeholder Names In Programming
They are not PHP keywords and have no built-in behavior. A PHP variable named $foo follows exactly the same rules as $customer, $total, or any other valid variable name.
Why Placeholder Names Exist
A small example may need names without wanting to introduce a business domain:
<?php
$foo = 'first value';
$bar = 'second value';
This can keep attention on syntax, ordering, substitution, or a protocol rule. A sequence of familiar placeholders also lets a writer refer to several otherwise anonymous parts consistently.
The usual order begins with foo, then bar, then baz, followed by names such as qux and quux. There is no language rule requiring that order.
Early Computing Usage
The terms circulated in early engineering and computing communities before modern PHP existed. Digital Equipment Corporation documentation helped spread foobar in the 1960s and 1970s, while early Internet RFCs repeatedly used foo, bar, and foobar as sample names.
RFC 3092, published on April 1, 2001, collected this history under the title "Etymology of Foo." It is an Informational RFC, not an Internet standard, and its tone includes deliberate humor.
By the time PHP was created, these names were already part of general programmer culture. PHP tutorials inherited them from that wider tradition.
The Origin Is Not One Simple Story
foobar is often connected to the Second World War military slang acronym FUBAR. That association probably helped the computing term spread, but RFC 3092 describes evidence that foo had earlier life in cartoons, popular culture, military usage, and the MIT Tech Model Railroad Club's hacker vocabulary.
The safest summary is:
fooexisted before its widespread programming use- military slang influenced the surrounding
foo/foobarstory - early hacker and computer documentation established the placeholder convention
- no single neat acronym expansion explains every occurrence
Avoid presenting one colorful etymology as settled fact.
When Foo And Bar Help
Placeholders can be reasonable when:
- demonstrating the grammar of a function call
- showing a generic substitution or mapping
- documenting a protocol field with no domain meaning
- creating a disposable scratch example
- discussing the placeholder convention itself
Even then, use enough context that a beginner can tell which value is which.
When They Hurt
In application code, placeholder names hide intent:
<?php
function foo(int $bar, int $baz): int
{
return $bar >= $baz ? 0 : 500;
}
The syntax is valid, but the reader cannot know whether the values represent ages, stock levels, distances, or money thresholds.
Domain names expose the rule:
<?php
declare(strict_types=1);
function calculateDeliveryCharge(
int $basketTotalPence,
int $freeDeliveryThresholdPence,
): int {
return $basketTotalPence >= $freeDeliveryThresholdPence ? 0 : 500;
}
echo calculateDeliveryCharge(5200, 5000) . PHP_EOL;
// Prints:
// 0
The second version communicates units, purpose, and the meaning of the comparison. That matters in production code, tests, reviews, logs, and incident diagnosis.
Teaching Examples Need Intent Too
Placeholder-heavy examples can make introductory material harder. A learner must decode the programming idea and guess what each anonymous name represents.
Prefer names such as:
$namefor string operations$productsfor array examples$requestand$responsefor HTTP examples$expectedand$actualfor assertions$subtotalPencefor integer money
Use foo only when the absence of domain meaning is intentional.
Placeholder Domains And Addresses
Do not invent realistic domains, email addresses, or IP addresses that may belong to somebody. Internet standards reserve names for documentation, including example.com, example.net, and example.org.
Use:
https://api.example.com/products
alice@example.com
A name such as foo.com is not automatically reserved merely because foo is a programming placeholder.
For IPv4 examples, RFC 5737 reserves three TEST-NET blocks:
192.0.2.0/24
198.51.100.0/24
203.0.113.0/24
These blocks are for documentation, not private application networks or real public endpoints.
Searchability And Maintenance
Names such as $foo and doThing() are difficult to search because they reveal no concept. Meaningful names let a developer locate delivery calculations, authorization rules, invoice totals, or tenant state across a codebase.
Temporary placeholders also have a habit of surviving. Rename them before an experiment becomes shared production code.
Review Questions
When a placeholder appears, ask:
- Is the example truly domain-neutral?
- Would a meaningful name explain the behavior faster?
- Are the units visible?
- Could two placeholders be accidentally swapped?
- Will the name help a future search or failure message?
- Is a sample domain or address actually reserved for documentation?
A short example does not need long names, but it still needs enough meaning to prevent mistakes.
Source
- RFC 3092: Etymology of "Foo" records the convention and several possible historical paths. Read it as an Informational and partly humorous historical document, not as a definitive proof of one origin.
- IANA: Example Domains explains the purpose of the reserved example-domain hosts.
- RFC 5737: IPv4 Address Blocks Reserved for Documentation defines the three TEST-NET ranges.
What To Remember
foo, bar, and baz are conventional metasyntactic placeholders inherited from older computing culture. They can remove irrelevant domain detail from tiny examples, but meaningful names are normally better in teaching and application code. The link between foobar, older popular usage, and military slang is historical context, not a single certain origin story.
Before moving on, make sure you can explain when a placeholder clarifies an example and when it conceals the rule the code is meant to express.
Practice
Task: Replace Placeholder Names
<?php
declare(strict_types=1);
function foo(int $bar, int $baz): int
{
return $bar >= $baz ? 0 : 500;
}
echo foo(4200, 5000) . PHP_EOL;
echo foo(5100, 5000) . PHP_EOL;
// Prints:
// 500
// 0
The rule is: delivery costs 500 pence until the basket reaches the free-delivery threshold.
Choose names that communicate:
- what the function calculates
- the meaning and units of both parameters
- the meaning and units of the return value
Then explain why foo, bar, and baz would still be acceptable in a tiny example whose subject was placeholder substitution itself.
Show solution
<?php
declare(strict_types=1);
function calculateDeliveryChargePence(
int $basketTotalPence,
int $freeDeliveryThresholdPence,
): int {
return $basketTotalPence >= $freeDeliveryThresholdPence ? 0 : 500;
}
echo calculateDeliveryChargePence(4200, 5000) . PHP_EOL;
echo calculateDeliveryChargePence(5100, 5000) . PHP_EOL;
// Prints:
// 500
// 0
The behavior is unchanged, but the function and parameters now expose the delivery rule and monetary unit. The return type alone says only int; including Pence in the function name makes the result's unit visible to callers.
foo, bar, and baz can still be appropriate when those anonymous positions are the subject of the example, such as demonstrating that one placeholder is replaced before another. In domain code, the delivery names carry information the placeholders cannot.