Home Backend Development PHP Tutorial PHP learning (4) - data types

PHP learning (4) - data types

Jul 29, 2016 am 09:04 AM
an float int string

PHP supports 8 primitive data types.

Four scalar types: (scalar type is the basic type)

  • boolean (Boolean)
  • integer (integer)
  • float (floating point type, also called double) (Due to historical reasons, float is also It’s called double, there is no distinction between single precision and double precision in PHP)
  • string (String type is a scalar type in PHP and a class type in Java)

Two composite types:

  • array (array)
  • object (object)

Finally there are two special types:

  • resource (resource)
  • NULL (no type)

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

If you want to check the value and type of an expression, use the var_dump() function.
If you just want a human-readable representation of the type for debugging, use the gettype() function. To check a type, don't use gettype(), use the is_type function.

Example:

<code><span><span><?php </span><span>$a_bool</span> = <span>TRUE</span>;   <span>// a boolean</span><span>$a_str</span>  = <span>"foo"</span>;  <span>// a string</span><span>$a_str2</span> = <span>'foo'</span>;  <span>// a string</span><span>$an_int</span> = <span>12</span>;     <span>// an integer</span><span>$a_float</span> = <span>3.14</span>;  <span>// a float(double)</span><span>echo</span> gettype(<span>$a_bool</span>).<span>"<br>"</span>; <span>// prints out:  boolean</span><span>echo</span> gettype(<span>$a_str</span>).<span>"<br>"</span>;  <span>// prints out:  string</span><span>echo</span> gettype(<span>$an_int</span>).<span>"<br>"</span>;  <span>// prints out:  integer</span><span>echo</span> gettype(<span>$a_float</span>).<span>"<br>"</span>;  <span>// prints out:  double</span><span>// If this is an integer, increment it by four</span><span>if</span> (is_int(<span>$an_int</span>)) {
    <span>echo</span><span>"an_int = "</span>.<span>$an_int</span>.<span>"<br>"</span>;
    <span>$an_int</span> += <span>4</span>;
    <span>echo</span><span>"an_int = "</span>.<span>$an_int</span>.<span>"<br>"</span>;
}

<span>// If $bool is a string, print it out</span><span>// (does not print out anything)</span><span>if</span> (is_string(<span>$a_str</span>)) {
    <span>echo</span><span>"String: $a_str"</span>.<span>"<br>"</span>;
}

<span>echo</span> var_dump(<span>$a_float</span>, <span>$a_bool</span>, <span>$a_str</span>, <span>$an_int</span>);

<span>?></span></span></span></code>
Copy after login

Output:

<code>boolean
<span>string</span>
integer
<span>double</span>
an_int = <span>12</span>
an_int = <span>16</span>
String: foo
<span>float</span>(<span>3.14</span>) <span>bool</span>(<span>true</span>) <span>string</span>(<span>3</span>) <span>"foo"</span><span>int</span>(<span>16</span>)</code>
Copy after login

Explanation of gettype() in the PHP manual (please enlarge to view?):
PHP learning (4) - data types

For the specific use of each type, please refer to the official PHP manual. I am just here Throw a few bricks to attract jade.

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces PHP learning (4) - data types, including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Convert basic data types to strings using Java's String.valueOf() function Convert basic data types to strings using Java's String.valueOf() function Jul 24, 2023 pm 07:55 PM

Convert basic data types to strings using Java's String.valueOf() function In Java development, when we need to convert basic data types to strings, a common method is to use the valueOf() function of the String class. This function can accept parameters of basic data types and return the corresponding string representation. In this article, we will explore how to use the String.valueOf() function for basic data type conversions and provide some code examples to

What is the maximum value of float? What is the maximum value of float? Oct 11, 2023 pm 05:54 PM

Maximum value of float: 1. In C language, the maximum value of float is 3.40282347e+38. According to the IEEE 754 standard, the maximum exponent of the float type is 127, and the number of digits of the mantissa is 23. In this way, the maximum floating point number is 3.40282347 e+38; 2. In the Java language, the maximum float value is 3.4028235E+38; 3. In the Python language, the maximum float value is 1.7976931348623157e+308.

How to convert char array to string How to convert char array to string Jun 09, 2023 am 10:04 AM

Method of converting char array to string: It can be achieved by assignment. Use {char a[]=" abc d\0efg ";string s=a;} syntax to let the char array directly assign a value to string, and execute the code to complete the conversion.

Anbernic confirms release date, specs and pricing for new RG406V retro gaming handheld Anbernic confirms release date, specs and pricing for new RG406V retro gaming handheld Sep 13, 2024 am 09:15 AM

Anbernic appears set to maintain its monthly gaming handheld release schedule for at least another month. Incidentally, it may well introduce a refresh of the RG35XX SP (curr. $89.99 on Amazon) before October rolls around, too.However, its principal

Detailed explanation of the method of converting int type to bytes in PHP Detailed explanation of the method of converting int type to bytes in PHP Mar 06, 2024 pm 06:18 PM

Detailed explanation of the method of converting int type to byte in PHP In PHP, we often need to convert the integer type (int) to the byte (Byte) type, such as when dealing with network data transmission, file processing, or encryption algorithms. This article will introduce in detail how to convert the int type to the byte type and provide specific code examples. 1. The relationship between int type and byte In the computer field, the basic data type int represents an integer, while byte (Byte) is a computer storage unit, usually 8-bit binary data

C++ program to convert double type variable to int type C++ program to convert double type variable to int type Aug 25, 2023 pm 08:25 PM

In C++, variables of type int can only hold positive or negative integer values; they cannot hold decimal values. There are float and double values ​​available for this purpose. The double data type was created to store decimals up to seven digits after the decimal point. Conversion of an integer to a double data type can be done automatically by the compiler (called an "implicit" conversion), or it can be explicitly requested by the programmer from the compiler (called an "explicit" conversion). In the following sections, we'll cover various conversion methods. Implicit conversions The compiler performs implicit type conversions automatically. To achieve this, two variables are required - one of floating point type and the other of integer type. When we simply assign a floating point value or variable to an integer variable, the compiler takes care of all the other things

2w words detailed explanation String, yyds 2w words detailed explanation String, yyds Aug 24, 2023 pm 03:56 PM

Hello everyone, today I will share with you the basic knowledge of Java: String. Needless to say the importance of the String class, it can be said to be the most used class in our back-end development, so it is necessary to talk about it.

Use Java's String.replace() function to replace characters (strings) in a string Use Java's String.replace() function to replace characters (strings) in a string Jul 25, 2023 pm 05:16 PM

Replace characters (strings) in a string using Java's String.replace() function In Java, strings are immutable objects, which means that once a string object is created, its value cannot be modified. However, you may encounter situations where you need to replace certain characters or strings in a string. At this time, we can use the replace() method in Java's String class to implement string replacement. The replace() method of String class has two types:

See all articles