Print variable types: Difference between revisions

From LemonWiki共筆
Jump to navigation Jump to search
Line 39: Line 39:


== Print the variable ==
== Print the variable ==
PHP
=== PHP ===
* [http://php.net/manual/en/function.print-r.php PHP: print_r - Manual] or  
* [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]
* [http://php.net/manual/en/function.var-dump.php PHP: var_dump - Manual]
Line 50: Line 50:
</pre>
</pre>


Python
=== Python ===
* [https://www.python-course.eu/python3_print.php Python3 Tutorial: Output with Print]
* [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]
* [https://stackoverflow.com/questions/1987694/how-to-print-the-full-numpy-array python - How to print the full NumPy array? - Stack Overflow]
Line 58: Line 58:


print(var)) # python v. 3
print(var)) # python v. 3
</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 ===
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>
</pre>



Revision as of 17:44, 27 October 2018

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

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