search
HomeBackend DevelopmentPHP TutorialPHP learning (4) - data types

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>

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>

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
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
使用java的String.valueOf()函数将基本数据类型转换为字符串使用java的String.valueOf()函数将基本数据类型转换为字符串Jul 24, 2023 pm 07:55 PM

使用Java的String.valueOf()函数将基本数据类型转换为字符串在Java开发中,当我们需要将基本数据类型转换为字符串时,一种常见的方法是使用String类的valueOf()函数。这个函数可以接受基本数据类型的参数,并返回对应的字符串表示。在本文中,我们将探讨如何使用String.valueOf()函数进行基本数据类型转换,并提供一些代码示例来

怎么把char数组转string怎么把char数组转stringJun 09, 2023 am 10:04 AM

char数组转string的方法:可以通过赋值来实现,使用{char a[]=" abc d\0efg ";string s=a;}语法,让char数组对string直接赋值,执行代码即可完成转换。

2w字 详解 String,yyds2w字 详解 String,yydsAug 24, 2023 pm 03:56 PM

大家好,今天给大家分享java基础知识之String。String类的重要性就不必说了,可以说是我们后端开发用的最多的类,所以,很有必要好好来聊聊它。

使用java的String.replace()函数替换字符串中的字符(串)使用java的String.replace()函数替换字符串中的字符(串)Jul 25, 2023 pm 05:16 PM

使用Java的String.replace()函数替换字符串中的字符(串)在Java中,字符串是不可变的对象,这意味着一旦创建了一个字符串对象,就无法修改它的值。但是,你可能会遇到需要替换字符串中的某些字符或者字符串的情况。这时候,我们可以使用Java的String类中的replace()方法来实现字符串的替换。String类的replace()方法有两种重

使用java的String.length()函数获取字符串的长度使用java的String.length()函数获取字符串的长度Jul 25, 2023 am 09:09 AM

使用Java的String.length()函数获取字符串的长度在Java编程中,字符串是一种非常常见的数据类型,我们经常需要获取字符串的长度,即字符串中字符的个数。在Java中,我们可以使用String类的length()函数来获取字符串的长度。下面是一个简单的示例代码:publicclassStringLengthExample{publ

Java String中的split方法如何使用Java String中的split方法如何使用May 02, 2023 am 09:37 AM

String中split方法使用String的split()方法用于按传入的字符或字符串对String进行拆分,返回拆分之后的数组。1、一般用法用一般的字符,例如@或,等符号做分隔符时:Stringaddress="上海@上海市@闵行区@吴中路";String[]splitAddr=address.split("@");System.out.println(splitAddr[0]+splitAddr[1]+splitAddr[2]+splitAddr[3

java的String类如何使用java的String类如何使用Apr 19, 2023 pm 01:19 PM

一、认识String1.JDK中的String首先我们看看JDK中的String类源码,它实现了很多接口,可以看到String类被final修饰了,这就说明String类不可以被继承,String不存在子类,这样所有使用JDK的人,用到的String类都是同一个,如果String允许被继承,每个人都可以对String进行扩展,每个人使用的String都不是同一个版本,两个不同的人使用相同的方法,表现出不同的结果,这就导致代码没办法进行开发了继承和方法覆写在带来灵活性的同时,也会带来很多子类行为不

使用java的String.toLowerCase()函数将字符串转换为小写使用java的String.toLowerCase()函数将字符串转换为小写Jul 24, 2023 pm 11:52 PM

String.toLowerCase()函数是Java中一个非常有用且常见的字符串处理函数,它可以将一个字符串转换为小写形式。在本文中,我们将介绍该函数的使用方法,并给出一些相关的代码示例。首先,让我们来看一下String.toLowerCase()函数的基本语法。它没有任何参数,只需要调用它即可。下面是示例代码:Stringstr=&quot;Hel

See all articles

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 agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!