Overview
Different than in common programming languages, variables in B4P may appear outlandish, but there is a reason for this:
Base Variable Name | [ | ] |
- String value or expression | | |
as variable name | | |
All variable names must follow with 2 bracket symbols [ ]. For simple variables, no additional info needs to be spefified inside the
brackets. For for structures and arrays, member names or array index numbers need to be specified inside them. This format provides the freedom
to use any variable name, even with special symbols, and you can reference variables indirectly by picking up variable names from other variables
or expressions. And the contents inside the bracket allows for additional freedoms other programming languages do not provide.
Naming variables:
- The naming rule applies to base variables as well as member variables in structures.
- Table names must be of type string and may take practially any contents, even spaces, special symbols and foreign and non-ANSI characters
- Table names may be referenced as string constants (with or without single or double quotation marks) or as expressions returing strings (e.g. variables, string calculations, etc.)
- Multiple consecutive spaces (and tabs) in names not put into quotation marks will collapse to 1 space. Example: Last Name collapses to Last Name.
- Quotation marks must be used for table name if they contain special symbols which confuse with B4P code syntax, e.g. hyphens misinterpreted as minus signs.
- Names may or end with numbers, even with spaces inbetween, e.g. 4200 Pennsylvania Ave..
- Blank variable names are also allowed, e.g. ''[].
- Any expression returning a string is allowed, e.g. ("Hel" + "lo")[] are allowed.
- Indirect variable referencingis are upported,
- In LHS expressions where parentheses are mandatory. Example: (a[])[] = 1; where 1 is written to the variable name as stored in variable a[].
- in RHS expressions where parentheses are optional. Example: echo( a[][] ); meaning writing 1 to variable with name retrieved from variable a.
Declaring variables:
- No explicit variable declarations are required, unless you want to fully initialize arrays or structures using dim(), array(), structure() or similar functions.
- Variables are declared with their first assignment
- If no variable protections apply, the values of different (i.e. changing) basic data types can be written.
See also next section on variable scopes (global, regional, local and sysstem variables).