


PHP type conversion php7 PHP environment construction PHP from entry to proficiency
php is a weakly typed language. During the operation, the data will be converted into data that can be processed by the operator according to the operator.
1. Automatic conversion
- if($a | number) { . . . } //The variables or numbers in the if will be automatically converted to Boolean
- Arithmetic operators will turn the data into numerical processing
<code><span>$a</span> = <span>"3"</span>; <span>$b</span> = <span>"5"</span>; <span>$c</span> = <span>$a</span> + <span>$b</span> ; var_dump(<span>$c</span>);<span>//</span><span>8</span> integer </code>
<code>3. 连接运算符. 转成字符串类型 </code>
<code><span>$str</span> = <span>'123'</span>.<span>true</span>.<span>'abc'</span>;<span>//</span><span>1231</span>abc string </code>
<code>4. 比较运算符,分几种情况 </code>
①Comparison of two numbers – normal situation
②If one of the data is of Boolean type, convert them all to Boolean type for comparison true>false
③If there is no Boolean type in the data, but one of the data is a number, convert it to a number and compare
④ Both sides are numeric strings, convert them into numbers for comparison
⑤Both sides of the comparison operator are string types. ASCII values are compared sequentially starting from the first character. The subsequent comparison stops if the value is larger.
Some conversion rules:
“12” –> 12
“1.2” –> 1.2
"1.2a" - –> 1.2
"a1.2" —> 0
"1.1a1" —> 1.1
"0" —> false
"1" —> true
0 —> false
"0.0" —> true
"abc" —> 0
"" —> 0
"" - >false
<code><span><span><?php </span>header(<span>"content-type:text/html;charset=utf-8"</span>); <span>$name</span> = <span>"tom"</span>; <span>if</span>(<span>$name</span>) { <span>echo</span><span>'$name为真'</span>.<span>'<hr>'</span>; }<span>else</span> { <span>echo</span><span>'$name为假'</span>.<span>'<hr>'</span>; } <span>//1.在算数运算符中的类型转换 :会转换成数字进行计算</span><span>$a</span> = <span>"1.1"</span>;<span>//"1.1a" 会转换成1.1 , "a1.1"会转换成0</span><span>$b</span> = <span>"7"</span>; <span>$c</span> = <span>$a</span> + <span>$b</span> ; <span>echo</span><span>$c</span>,<span>" "</span>,getType(<span>$c</span>),<span>'<hr>'</span>;<span>//12 double</span><span>//连接运算符.其结果是字符串类型</span><span>$a</span> = <span>5</span>; <span>$b</span> = <span>true</span>;<span>// true 转换为 1 , false 转换成 0</span><span>$c</span> = <span>$a</span>.<span>$b</span> ; <span>echo</span><span>$c</span>,<span>" "</span>,getType(<span>$c</span>),<span>'<hr>'</span>;<span>//51 string</span><span>//比较运算符,其结果一定是true或false</span><span>$a</span> = <span>false</span> ; <span>$b</span> = <span>5</span>; <span>//如果两边至少一个布尔值,则把非布尔值数据转为布尔值比较,其结果类型也是布尔</span><span>$c</span> = (<span>$a</span> $b</span>);<span>//这里$b转为true , $c = false <span>echo</span><span>$c</span>,<span>" "</span>,getType(<span>$c</span>),<span>'<hr>'</span>;<span>//1 boolean</span><span>//比较运算符中,两边不存在布尔类型但存在数字,则都转为数字类型比较</span><span>$a</span> = <span>5</span> ; <span>$b</span> = <span>"abc"</span>;<span>//$b 要转为数字比较,这里为0 </span><span>$c</span> = (<span>$a</span> > <span>$b</span>); <span>echo</span><span>$c</span>,<span>" "</span>,getType(<span>$c</span>),<span>'<hr>'</span>;<span>//1 boolean</span><span>//比较运算符中两边都是字符串类型</span><span>$a</span> = <span>"abc"</span> ; <span>$b</span> = <span>"abd"</span>; <span>$c</span> = (<span>$a</span> $b</span>);<span>//从首个字符开始依次比较ASCII值,哪个大就停止后续比较</span><span>echo</span><span>$c</span>,<span>" "</span>,getType(<span>$c</span>),<span>'<hr>'</span>;<span>//1 boolean</span></span></code>
2. Force conversion
<code><span>$a</span> = <span>11.5</span>; <span>$b</span> = (int)<span>$a</span>;<span>//强制转换成int赋值给$b</span><span>echo</span><span>$a</span>.<span>" 类型:"</span>.getType(<span>$a</span>).<span>'<hr>'</span>;<span>//11.5 类型:double</span><span>echo</span><span>$b</span>.<span>" 类型:"</span>.getType(<span>$b</span>).<span>'<hr>'</span>;<span>//11.5 类型:double</span><span>$c</span> = (string)<span>$a</span>; <span>echo</span><span>$c</span>.<span>" 类型:"</span>.getType(<span>$c</span>).<span>'<hr>'</span>;<span>//11.5 类型:11.5 类型:string</span><span>$d</span> = (boolean)<span>$a</span>; <span>echo</span><span>$d</span>.<span>" 类型:"</span>.getType(<span>$d</span>).<span>'<hr>'</span>;<span>//1 类型:boolean</span><span>$e</span> = (float)<span>$b</span>;<span>//强制转换成浮点数</span><span>echo</span><span>$e</span>.<span>" 类型:"</span>.getType(<span>$e</span>).<span>'<hr>'</span>;<span>//11 类型:double</span>setType(<span>$c</span>,<span>"float"</span>); <span>echo</span><span>$c</span>.<span>" 类型:"</span>.getType(<span>$c</span>).<span>'<hr>'</span>;<span>//11.5 类型:double</span></code>
3. Type-related functions
- var_dump(): Output the complete information of a variable
- getType(): Get the type name of a variable, The return is a type name string
- setType(variable, "target class type"); Set the type of a variable
- isset() Returns TRUE if the variable exists and the value is not NULL, otherwise returns FALSE.
- empty Determine whether a variable data is empty
- unset() Destroy a variable
- is_int()
- is_numeric() —1, 1.2, "1.2", "12" are all true
- is_array() is_scalar— Basic data type: string float int boolean
- is_null — Check whether the variable is NULL
The above introduces the type conversion of PHP, including PHP content. I hope it will be helpful to friends who are interested in PHP tutorials.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
