Jump to content

[PHP] Tipo da Variável


PHP

Recommended Posts

<?php
$goldpieces = 2500;
print "I have $goldpieces gold pieces. Although, I don't know what they are. What is their type? <br><br>";
/*Are the gold pieces an array?*/
$array = is_array($goldpieces);
if ($array == 1) {$array = "Yes, they are.";}
else {$array = "No, they aren't.";}
/*Are the gold pieces an integer?*/
$integer = is_integer($goldpieces);
if ($integer == 1) {$integer = "Yes, they are.";}
else {$integer = "No, they aren't.";}
/*Are the gold pieces a number?*/
$number = is_numeric($goldpieces);
if ($number == 1) {$number = "Yes, they are.";}
else {$number = "No, they aren't.";}
/*Let's tell this to the user, then.*/
print "Question: Are the gold pieces an array? <br> Answer: $array <br><br>";
print "Question: Are the gold pieces an integer? <br> Answer: $integer <br><br>";
print "Question: Are the gold pieces a number? <br> Answer: $number <br><br>";
print "But most important, if I don't know their type, neither I want to ask a lot of questions, I can ask just one question:<br><br>";
$gettype = gettype($goldpieces); // What's the variable's type?
$gettypeanswer = "The gold pieces are a(n) ".$gettype.".";
print "Question: What are the gold pieces? <br> Answer: $gettypeanswer <br><br>"; 
?> 

Isto foi feito por mim, enquanto aprendia PHP e MySQL (estou a ler um livro muito grande sobre isso). Sei que o código não é nada de especial, mas foi uma forma fixe de eu aprender os tipos das variáveis. Se alguém quiser, que pegue no código. 😉 Comentários e sugestões são muito apreciados.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.