search

PHP basic types

Oct 20, 2016 pm 02:58 PM
phptype

PHP supports 8 primitive data types.


Four scalar types:

boolean (Boolean)

integer (integer)

float (floating point, also called double)

string (string)


two Specific composite type: aArray For readability, this manual also introduces some pseudo-types:

mixed (mixed type)

number (numeric type)

callback (callback type)

and pseudo-variables $….


You may also read some references to the "double" type. In fact, double and float are the same. For some historical reasons, these two names exist at the same time.

The type of a variable is usually not set by the programmer. Rather, it is determined by PHP at runtime based on the context in which the variable is used.

Note: If you want to check the value and type of an expression, use the var_dump() function. If you just want to get an easy-to-understand type expression for debugging, use the gettype() function. To check a type, don't use gettype(), use the is_type function. Here are some examples:

<?php
$a_bool = TRUE;   // a boolean
$a_str  = "foo";  // a string
$a_str2 = &#39;foo&#39;;  // a string
$an_int = 12;     // an integer
 
echo gettype($a_bool); // prints out:  boolean
echo gettype($a_str);  // prints out:  string
 
// If this is an integer, increment it by four
if (is_int($an_int)) {
    $an_int += 4;
}
 
// If $bool is a string, print it out
// (does not print out anything)
if (is_string($a_bool)) {
    echo "String: $a_bool";
}
?>


If you want to force a variable to a certain type, you can use cast or settype() function.


Note that variables will exhibit different values ​​on specific occasions depending on their type at the time. See Type Conversion Discrimination for more information. In addition, you can also refer to the PHP type comparison table for examples of how different types compare to each other.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use