Print variable types: Difference between revisions

From LemonWiki共筆
Jump to navigation Jump to search
No edit summary
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
Print type of the variable
Print the type of the variable or just print the variable


* PHP gettype()<ref>[http://php.net/manual/en/function.gettype.php PHP: gettype - Manual]</ref>:
== Print the type of the variable ==
=== PHP ===
PHP gettype()<ref>[http://php.net/manual/en/function.gettype.php PHP: gettype - Manual]</ref>:
<pre>
<pre>
# define the variable
# define the variable
Line 9: Line 11:
echo "type of variable: " . gettype($var);
echo "type of variable: " . gettype($var);
</pre>
</pre>
* Python type():<ref>[https://docs.python.org/3.6/library/functions.html?highlight=type#type type]</ref><ref>[https://waoewaoe.wordpress.com/2009/05/18/type-cast-variable-to-string-in-python/ Type cast variable to String in python | Ploy's Blog | A Blog About Techinical Stuff]</ref> or {{kbd | key=<nowiki>print(type(var))</nowiki>}}
 
=== Python ===
Python type():<ref>[https://docs.python.org/3.6/library/functions.html?highlight=type#type type]</ref><ref>[https://waoewaoe.wordpress.com/2009/05/18/type-cast-variable-to-string-in-python/ Type cast variable to String in python | Ploy's Blog | A Blog About Techinical Stuff]</ref> or {{kbd | key=<nowiki>print(type(var))</nowiki>}}
<pre>
<pre>
# define the variable
# define the variable
Line 19: Line 23:
print("type of variable: " + str(type(var))) # python v. 3 cast type to string
print("type of variable: " + str(type(var))) # python v. 3 cast type to string
</pre>
</pre>
* Javascript typeof()<ref>[https://www.w3resource.com/javascript/operators/typeof.php JavaScript : typeof operator - w3resource]</ref>
 
=== Javascript ===
Javascript typeof()<ref>[https://www.w3resource.com/javascript/operators/typeof.php JavaScript : typeof operator - w3resource]</ref>
<pre>
<pre>
# define the variable
# define the variable
Line 28: Line 34:
console.log("type of variable:: " + typeof a);
console.log("type of variable:: " + typeof a);
</pre>
</pre>
* R: [https://stat.ethz.ch/R-manual/R-devel/library/base/html/typeof.html typeof()]


== references ==
=== R ===
R: [https://stat.ethz.ch/R-manual/R-devel/library/base/html/typeof.html typeof()]
 
== Print the variable ==
=== PHP ===
* [http://php.net/manual/en/function.print-r.php PHP: print_r - Manual] or
* [http://php.net/manual/en/function.var-dump.php PHP: var_dump - Manual]
<pre>
# define the variable
$var = 3.14159;
 
echo print_r($var, true) . PHP_EOL;
var_dump($var);
</pre>
 
=== Python ===
* [https://www.python-course.eu/python3_print.php Python3 Tutorial: Output with Print]
* [https://stackoverflow.com/questions/1987694/how-to-print-the-full-numpy-array python - How to print the full NumPy array? - Stack Overflow]
<pre>
# define the variable
var = 3.14159
 
print(var)) # python v. 3
</pre>
 
=== JavaScript ===
Using the [https://www.w3schools.com/jsref/met_console_log.asp HTML DOM console.log() Method].
<pre>
var a = 3.14159
console.log(a);
# print: 3.14159
</pre>
 
== Iterate over elements of an array or list ==
=== PHP ===
PHP: Using [http://php.net/manual/en/control-structures.foreach.php foreach] to iterate over elements of an array or public properties of an object<ref>[http://www.zentut.com/php-tutorial/php-foreach/ Using PHP foreach Loop to Iterate over Elements of an Array]</ref>.
 
Code:
<pre>
# type: array
$animals = ["bird", "dog", "bird"];
foreach ($animals as $index => $animal) {
  print "$index $animal"  . PHP_EOL;
}
</pre>
 
Result:
<pre>
0 bird
1 dog
2 bird
</pre>
 
=== Python ===
Python: Using [https://www.w3schools.com/python/python_for_loops.asp for loops] and [http://book.pythontips.com/en/latest/enumerate.html enumerate] function.
Code:
<pre>
# type: list
animals = ['cat', 'dog', 'bird']
for index, animal in enumerate(animals):
    print (index, animal)
</pre>
 
Result:
<pre>
0 bird
1 dog
2 bird
</pre>
 
== References ==
<references/>
<references/>
== Related articles ==
* [https://en.wikipedia.org/wiki/Code_refactoring Code refactoring - Wikipedia]


{{Template:Troubleshooting}}
{{Template:Troubleshooting}}


[[Category:Programming]]
[[Category:Programming]]

Latest revision as of 14:05, 28 October 2018

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