# Variables

These may be assigned values during execution e.g.

        -- x may only be assigned integer values
        integer x
        x = 25
        -- a, b and c may be assigned *any* value
        object a, b, c
        a = {}
        b = a
        c = 0

When you declare a variable you (should) give it a sensible name that lets you reason about the contents logically and protects you against making spelling mistakes later on, and specify/restrict what values may legally be assigned to it (said variable) during the execution of your program, via a type, either one of the five builtins (discussed above) or a user defined type (detailed five or[/and] six topics down). Some programming languages are "dynamic" with "duck typing", which can seem great, but fails to catch typos and other small slips, so it can end up taking far longer to locate and correct them. Other languages can have thousands of types, and worse still simply quietly discard fractions and higher binary digits, none of which happens in Phix programs (unless explicitly coded). It is fair to say that some cryptographic functions expressly rely on said automatic discards, making them slightly more awkward to code in Phix (and dynamic languages), but in the vast majority of cases you do not want that kind of behaviour at all. Lastly, some languages permit or require explicit casts between types, but woe betide anyone who slips up on that, and again that is simply not necessary in Phix. It may be that C-style typedefs can trigger some (but not many) compilation errors that Phix may let through, but makes up for with (implicitly triggered) run-time errors, that a C programmer would probably have to explicitly code in a probably tediously consistent and potentially very widespread fashion, and would make a user of a dynamic language quietly sigh and want to die.
Variables may also be assigned on declaration, e.g.

        -- x may only be assigned integer values
        integer x = 25
        -- a, b and c may be assigned *any* value
        object a = {}, b = a, c = 0

Variable declaration also supports multiple assignment syntax (this may make more sense after reading that section) e.g.

        object {x, y, z} = {{},5,1.5}
    --  object x = {}, y = 5, z = 1.5 -- equivalent

Variables may also be declared globally, locally, or private to a routine (see scope), eg

    global atom a = 1       -- visible to the rest of the entire application
    sequence p = {"one"}    -- visible to the rest of the current source file only
    procedure p()
        string s = "s"      -- visible to the rest of the routine p() only
        ?s                  -- ok (ditto a, p)
    end procedure
    p()                     -- declares and displays s
    --?s                    -- illegal (but a, p still ok)

They can also be declared with block-level scope, eg

        if rand(2)=1 then
            string s = "p1" -- visible within this branch/block only
            ?s
        else
            ?"p2"
    --      ?s              -- illegal
        end if
    --  ?s                  -- illegal