Version: v0.7.0 - Beta.  We welcome contributors & feedback.

Variables

Basics 

A variable is like a box that contains information (i.e. data).

Each variable has a label that starts with $.

You can assign data to variables with =.

$location = 'Tokyo'
$numTrainStations = 900
$hasGodzilla = true

Case Matching

Variable and function names are case-sensitive. They must match exactly.

$numTacos = 10

// ✕ ERROR
print($numtacos)
          ^--- should be uppercase 'T'

// ✕ ERROR
Print('Hello!')
^--- should be lowercase 'p'

Basic Data Types 

There are 3 basic types of data that can go into a variable:

Examples of each:

// Number
42     // positive
-62    // negative
1.618  // fraction (aka 'floating point' or 'float')

// String (surrounded with single-quotes)
'abc123'
'This is a string!'
'123'  <-- looks like a number, but still a string

// Boolean
true
false

Initializing Variables 

New variables must always be given a value before they are used.

If you’re not sure what to use, pick the “empty” version of the type you need:

$myString = ''
$myNumber = 0
$myBoolean = false

Null 

PHP and other languages have a null or undefined value that represents “nothing”.

For added safety, THT does not have a null value. (See “The Billion Dollar Mistake”).

If you need a way to represent “nothing”, consider using a Result object, or an empty value like false.

Variable Names 

Variables and function names are always written in camelCase format:

Examples:

$user
$userId
$firstName
$isDeleted
$numRecords
$httpVersion
$isAjaxRequest
$userHasIphone

8 Tips for Good Names 

Coming up with good variable names is one of the most important (and difficult) skills in programming.

Good names provide clarity. They make your code easier to understand — for yourself and for others.

Bad, unclear variable names will slow you down and can lead to hard-to-find bugs.

Tips

Tip 1: Avoid names that have no meaning, or that try to be cute or funny.

✕ All Bad
---------------
$a1
$foo
$temp
$blah
$frodo
$imAwesome

Tip 2: Use full words. Don’t drop random letters. (It’s 2 hrd 2 rd and rmmbr the crct splng.)

$usr       -->  $user
$blgCmnt   -->  $blogComment
$grpTitle  -->  $groupTitle

Tip 3: It’s ok to truncate (remove the end of) very long words to 1 or 2 syllables, as long as the meaning is still clear.

$administrator       -->  $admin
$moderator           -->  $mod
$organizationId      -->  $orgId
$configurationKey    -->  $configKey
$temporaryDirectory  -->  $tempDir

Tip 4: Numeric counts are prefixed with num- and are plural.

$comments   -->  $numComments
$noUsers    -->  $numUsers
$nbrPosts   -->  $numPosts
$cntClick   -->  $numClicks

Tip 5: Booleans (true/false) are prefixed with is-, has-, can-, etc.

$banned       -->  $isBanned
$userBio      -->  $userHasBio
$deletePosts  -->  $canDeletePosts

Tip 6: Joiner words like “of” and “the” should be dropped.

$numOfDaysInTheWeek  --> $numDaysInWeek
$theColorOfTheTheme  --> $themeColor

Tip 7: Include units of measurement or time.

$elapsed   -->  $timeElapsedHours
$distance  -->  $distToHomeKm

Tip 8: Don’t include the data type in the name.

$nameString      -->  $name
$boolDeleted     -->  $isDeleted
$activeUserList  -->  $activeUsers
$userScoresMap   -->  $scorePerUser
$blogPostObject  -->  $blogPost