Print variable types

From LemonWiki共筆
Jump to: navigation, search

Print the type of the variable or just print the variable

Print the type of the variable[edit]

PHP[edit]

PHP gettype()[1]:

# define the variable
$var = 3.14159;

# print type of the variable
echo "type of variable: " . gettype($var);

Python[edit]

Python type():[2][3] or print(type(var))

# define the variable
var = 3.14159

# print type of the variable
type(var) # return <class 'type'>
print(type(var))
print("type of variable: " + str(type(var))) # python v. 3 cast type to string

Javascript[edit]

Javascript typeof()[4]

# define the variable
var a = 3.14159;

# print type of the variable
typeof a;
console.log("type of variable:: " + typeof a);

R[edit]

R: typeof()

Print the variable[edit]

PHP[edit]

# define the variable
$var = 3.14159;

echo print_r($var, true) . PHP_EOL;
var_dump($var);

Python[edit]

# define the variable
var = 3.14159

print(var)) # python v. 3

JavaScript[edit]

Using the HTML DOM console.log() Method.

var a = 3.14159
console.log(a);
# print: 3.14159

Iterate over elements of an array or list[edit]

PHP[edit]

PHP: Using foreach to iterate over elements of an array or public properties of an object[5].

Code:

# type: array
$animals = ["bird", "dog", "bird"];
foreach ($animals as $index => $animal) {
  print "$index $animal"  . PHP_EOL;
}

Result:

0 bird
1 dog
2 bird

Python[edit]

Python: Using for loops and enumerate function. Code:

# type: list
animals = ['cat', 'dog', 'bird'] 
for index, animal in enumerate(animals):
    print (index, animal)

Result:

0 bird
1 dog
2 bird

References[edit]

Related articles[edit]


Troubleshooting of ...

Template