Print variable types

From LemonWiki共筆
Revision as of 17:47, 27 October 2018 by Planetoid (talk | contribs) (→‎Python)
Jump to navigation Jump to search

Print the type of the variable or just print the variable

Print the type of the variable

PHP

PHP gettype()[1]:

# define the variable
$var = 3.14159;

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

Python

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

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

R: typeof()

Print the variable

PHP

# define the variable
$var = 3.14159;

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

Python

# define the variable
var = 3.14159

print(var)) # python v. 3

Iterate over elements of an array or list

PHP

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

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

Related articles


Troubleshooting of ...

Template