In reply to Philip, form data could also be an array. so there are two types you can expect from $_REQUEST (and it's associates): string and array.
Τύποι
Table of Contents
- Booleans
- Integers (Ακέραιοι)
- Αριθμοί κινητής υποδιαστολής
- Strings
- Arrays
- Objects
- Resource
- NULL
- Ψευδο-τύποι που χρησιμοποιήθηκαν σ'αυτό το documentation
- Type Juggling
Εισαγωγή
Η PHP υποστηρίζει οχτώ πρωταρχικούς τύπους.
Για βαθμωτούς τύπους:
- boolean (δυαδικός)
- integer (ακέραιοι)
- float (αριθμοί κινητής υποδιαστολής, γνωστοί και ως 'double')
- string
- array (πίνακες)
- object (αντικείμενα)
Ο τύπος μιας μεταβλητής δεν καθορίζεται συνήθως από τον προγραμματιστή, αλλά μάλλον καθορίζεται κατά τη διαρκεια εκτέλεσης από την PHP ανάλογα με το περιεχόμενο με το οποίο χρησιμοποιείται αυτή η μεταβλητή.
Note: Αν θέλετε να ελένξετε τον τύπο και την τιμή μιας συγκεκριμένης έκφρασης, χρησιμοποιείστε την var_dump(). Αν θέλετε απλά μια αναπαράσταση του τύπου, που να μπορεί εύκολα να διαβαστεί για το debugging, χρησιμοποιείστε την gettype(). Για τον έλεγχο ενός συγκεκριμένο τύπου, μην χρησιμοποιείτε την gettype(), αλλά τις is_typeσυναρτήσεις. Μερικά παραδείγματα:
<?php
$bool = TRUE; // a boolean
$str = "foo"; // a string
$int = 12; // an integer
echo gettype($bool); // prints out "boolean"
echo gettype($str); // prints out "string"
// If this is an integer, increment it by four
if (is_int($int)) {
$int += 4;
}
// If $bool is a string, print it out
// (does not print out anything)
if (is_string($bool)) {
echo "String: $bool";
}
?>
Αν θέλετε να αναγκάσετε μια μεταβλητή να μετατραπεί σε έναν συγκεκριμένο τύπο, μπορείτε είτε να χρησιμοποιήσετε την cast στην μεταβλητή ή την settype() συνάρτηση πάνω της.
Σημειώστε ότι η μεταβλητή μπορεί να υπολογιστεί με διαφορετικές τιμές σε συγκεκριμένες περιπτώσεις, ανάλογα με το τι τύπου είναι κάθε φορά. Για περισσότερες πληροφορίες, δείτε το κομμάτι σχετικά με το Type Juggling. Επίσης, ίσως σας ενδιαφέρει να δείτε τους πίνακες σύγκρισης τύπων, καθώς δείχνουν παραδείγματα από συγκρίσεις σχετικές με διάφορους τύπους.
Τύποι
12-Apr-2007 10:33
06-Dec-2005 12:32
Note that you can chain type castng:
var_dump((string)(int)false); //string(1) "0"
18-Mar-2005 04:40
if we use gettype() before initializinf any variable it give NULL
for eg.
<?php
$foo;
echo gettype($foo);
?>
it will show
NULL
29-Jun-2004 06:14
The differance of float and double dates back to a FORTRAN standard. In FORTRAN Variables aren't as loosly written as in PHP and you had to define variable types(OH NOES!). FLOAT or REAL*4 (For all you VAX people out there) defined the variable as a standard precision floating point, with 4 bytes of memory allocated to it. DOUBLE PRECISION or REAL*8 (Again for the VAX) was identical to FLOAT or REAL*4, but with an 8 byte allocation of memory instead of a 4 byte allocation.
In fact most modern variable types date back to FORTRAN, except a string was called a CHARACHTER*N and you had to specify the length, or CHARACHTER*(*) for a variable length string. Boolean was LOGICAL, and there weren't yet objects, and there was support for complex numbers(a+bi).
Of course, most people reading this are web programmers and could care less about the mathematical background of programming.
NOTE: Object support was added to FORTRAN in the FORTRAN90 spec, and expanded with the FORTRAN94 spec, but by then C was the powerful force on the block, and most people who still use FORTRAN use the FORTRAN77.
