Learning a New Programming Language Part 3: Syntax Differences - Variable Syntax
(Page 3 of 7 )
Another minor difference in syntax can be found in the way you reference a variable in a particular language. For instance, when you declare a variable in Visual Basic you use the keyword "Dim" (meaning dimension).
Dim mynumber as integer
var $size = 0;
var $weight = 0;
Notice also the dollar sign before each property name. This is used for variables in PHP, Perl and Ruby (only for global variables). In Perl the dollar sign is used for scalar variables (single values) and an "@" symbol is used in front of array names. If you are addressing an element of an array, however, you use the dollar sign, as this element carries a single value.
@simpsons = (Homer, Marge, Bart, Lisa, Maggy);
print "Doh! $simpsons !";
tagbegin = "<strong>";
message = "Give me all your lupins!";
tagend = "</strong>";
alert(tagbegin + message + tagend);
/* Output would be <strong>
Give me all your lupins!</strong>*/
Ruby and Python use the "+" operator as well. Ruby can also repeat a string by using other operators.
str = "baby" * 3
# Would output baby baby baby
In PHP and Perl the operator used to concatenate strings is a period or "dot". If you are concatenating values returned from function calls with strings or variables, using the "dot" syntax in both languages is the way to go rather than putting things inside strings. Below is an example in PHP.
return "We are the knights who say NEE!"
$tagbegin = "<strong>";
$message = "and we want a shrubbery!";
$tagend = "</strong>";
The TCL language uses a procedure (function) call to tcl_concat to concatenate strings. The arguments are the strings to concatenate.
More Web Hosting How-Tos Articles
More By Chris Root