PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Booleans> <Σχόλια
Last updated: Sun, 25 Nov 2007

view this page in

Τύποι

Table of Contents

Εισαγωγή

Η PHP υποστηρίζει οχτώ πρωταρχικούς τύπους.

Για βαθμωτούς τύπους:

  • boolean (δυαδικός)
  • integer (ακέραιοι)
  • float (αριθμοί κινητής υποδιαστολής, γνωστοί και ως 'double')
  • string
Δύο σύνθετους τύπους:
  • array (πίνακες)
  • object (αντικείμενα)
Και τέλος δυο ειδικούς τύπους: Αυτό το εγχειρίδιο επίσης σας εισάγει μερικούς pseudo-types (ψευδοτύπους) για λόγους αναγνωσιμότητας: Ίσως βρείτε και κάποιες αναφορές στο τύπο "double". Θεωρείστε το double όμοιο με τον τύπο float, και τα δυο ονόματα υπάρχουν μόνο για ιστορικούς λόγους.

Ο τύπος μιας μεταβλητής δεν καθορίζεται συνήθως από τον προγραμματιστή, αλλά μάλλον καθορίζεται κατά τη διαρκεια εκτέλεσης από την 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. Επίσης, ίσως σας ενδιαφέρει να δείτε τους πίνακες σύγκρισης τύπων, καθώς δείχνουν παραδείγματα από συγκρίσεις σχετικές με διάφορους τύπους.



Booleans> <Σχόλια
Last updated: Sun, 25 Nov 2007
 
add a note add a note User Contributed Notes
Τύποι
jonah_whalehosting_ca
12-Apr-2007 10:33
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.
arjini at gmail dot com
06-Dec-2005 12:32
Note that you can chain type castng:

var_dump((string)(int)false); //string(1) "0"
shahnaz khan
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
Trizor of www.freedom-uplink.org
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.

Booleans> <Σχόλια
Last updated: Sun, 25 Nov 2007
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites