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

search for in the

Booléen> <Commentaires
Last updated: Fri, 10 Oct 2008

view this page in

Les types

Sommaire

Introduction

PHP supporte 8 types basiques.

4 types scalaires :

2 types composés :

Et finalement, 2 types spéciaux :

Ce manuel introduit également quelques pseudo-types pour des raisons de lisibilités :

Et la pseudo-variable $... .

Ce manuel peut toujours contenir des références au type "double". Considérez ce type comme étant un type "float". Les deux noms n'existent que pour des raisons historiques.

Le type d'une variable n'est pas toujours définit par le programmeur ; il peut être définit par PHP au moment de l'exécution, suivant le contexte dans lequel la variable est utilisée.

Note: Pour vérifier le type et la valeur d'une expression, utilisez la fonction var_dump(). Pour afficher une représentation humainement lisible d'un type aux fins de déboguage, utilisez la fonction gettype(). Pour vérifier un certain type, n'utilisez pas la fonction gettype(), mais plutôt les fonctions is_type. Voici quelques exemples :

<?php
$a_bool 
TRUE;   // un booléen
$a_str  "foo";  // une chaîne de caractères
$a_str2 'foo';  // une chaîne de caractères
$an_int 12;     // un entier

echo gettype($a_bool); // affiche :  boolean
echo gettype($a_str);  // affiche :  string

// Si c'est un entier, incrément de 4
if (is_int($an_int)) {
    
$an_int += 4;
}

// Si $bool est une chaîne de caractères, on l'affiche
if (is_string($a_bool)) {
    echo 
"String: $a_bool";
}
?>

Pour forcer la conversion d'une variable en un certain type, vous pouvez transtyper (cast) la variable ou utiliser la fonction settype().

Notez qu'une variable peut être évaluée avec différentes valeurs dans certaines situations, suivant son type à un moment donné. Pour plus d'informations, lisez la section sur le transtypage. La table de comparaison des types peut également être très utile, montrant différents exemples.



Booléen> <Commentaires
Last updated: Fri, 10 Oct 2008
 
add a note add a note User Contributed Notes
Les types
jonah_whalehosting_ca
12-Apr-2007 08: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 10:32
Note that you can chain type castng:

var_dump((string)(int)false); //string(1) "0"
shahnaz khan
18-Mar-2005 02: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
30-Jun-2004 04: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.

Booléen> <Commentaires
Last updated: Fri, 10 Oct 2008
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites