Editing
Print variable types
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
Print the type of the variable or just print the variable == Print the type of the variable == === PHP === PHP gettype()<ref>[http://php.net/manual/en/function.gettype.php PHP: gettype - Manual]</ref>: <pre> # define the variable $var = 3.14159; # print type of the variable echo "type of variable: " . gettype($var); </pre> === 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> # 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 </pre> === Javascript === Javascript typeof()<ref>[https://www.w3resource.com/javascript/operators/typeof.php JavaScript : typeof operator - w3resource]</ref> <pre> # define the variable var a = 3.14159; # print type of the variable typeof a; console.log("type of variable:: " + typeof a); </pre> === 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/> == Related articles == * [https://en.wikipedia.org/wiki/Code_refactoring Code refactoring - Wikipedia] {{Template:Troubleshooting}} [[Category:Programming]]
Summary:
Please note that all contributions to LemonWiki共筆 are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
LemonWiki:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Templates used on this page:
Template:Kbd
(
edit
)
Template:Troubleshooting
(
edit
)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Current events
Recent changes
Random page
Help
Categories
Tools
What links here
Related changes
Special pages
Page information