Home headlines PHP variable types and conversions

PHP variable types and conversions

Jun 27, 2018 pm 05:40 PM

1

2

echo &#39;<h3>2.变量类型与转换</h3>&#39;;

echo &#39;<hr color="green">&#39;;

Copy after login

//Scalar: single-valued variable, including integer, floating point, string, and Boolean.

1

2

3

4

$age = 30; //1整型 integer

$salary = 1234.56; //2.浮点 float

$name = &#39;peter&#39;; //3 字符串

$isMarried = true;  //4. 布尔型,true真,false假

Copy after login

//Scalar output echo, print or var_dump() can view the type and value

1

2

3

4

5

echo $name.&#39;的年龄是:&#39;.$age.&#39;,工资是:&#39;.$salary.&#39;,是否已婚:&#39;.$isMarried;

echo &#39;<br>&#39;;

print $name; print &#39;<br>&#39;;

var_dump($name);

echo &#39;<hr color="red">&#39;;

Copy after login

//Composite type: multi-valued variables, including arrays and objects

1

2

3

4

5

$books = [&#39;php&#39;,&#39;mysql&#39;,&#39;html&#39;,&#39;css&#39;,&#39;javascript&#39;]; //数组

$student = new stdClass(); //创建空对象$student

$student->name = &#39;王二小&#39;;  //添加属性name

$student->course = &#39;php&#39;;  //添加属性course

$student->grade = 80;     //添加属性grade

Copy after login

//Composite variable output: print_r() or var_dump()

1

2

3

4

5

6

echo &#39;<pre class="brush:php;toolbar:false">&#39;; //格式化输出结果

print_r($books);

print_r($student);

var_dump($books);

var_dump($student);

echo &#39;<hr color="red">&#39;;

Copy after login

//Special Type: resource type, null

1

2

3

$file = fopen(&#39;demo.php&#39;,&#39;r&#39;) or die(&#39;打开失败&#39;);

echo fread($file, filesize(&#39;demo.php&#39;));

fclose($file);

Copy after login

$price = null;

echo '$price is '.$price;

/**

* Variable type query, setting and detection

* 1. Type query:

* gettype($var)

* 2. Type detection:

* 2.1: is_integer(),

* 2.2: is_float(),

* 2.3: is_string(),

* 2.4: is_bool( ),

* 2.5: is_array(),

* 2.6: is_object(),

* 2.7: is_null(),

* 2.8: is_resource(),

* 2.9: is_numeric()...

* 3. Type conversion:

* 3.1: Force conversion: (int)$val,( string)$val...

* 3.2: Temporary conversion (the value conversion type remains unchanged): intval(), floatval(), strval(), val is value

* 3.3: Permanent conversion: settype($var, type identifier)

* /

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

$price = 186.79;

echo gettype($price);  //float/double浮点型,float和double同义

echo &#39;<hr>&#39;;

echo (int)$price//强制转为integer,186

echo &#39;<hr>&#39;;

echo $price//查看原始数据,仍是浮点型,并无变化

echo &#39;<hr>&#39;;

echo gettype($price);  //原始类型仍为double,并未发生变化

echo &#39;<hr>&#39;;

echo intval($price);  //临时将值转为整型,输出:186

echo &#39;<hr>&#39;;

echo $price; //输出原值,仍为186.79,原值并未发生变化

echo &#39;<hr>&#39;;

settype($price,&#39;integer&#39;);  //永久转为integer,返回布尔值

echo $price//查看值:186

echo &#39;<hr>&#39;;

echo gettype($price);  //类型为integer

echo &#39;<hr>&#39;;

echo is_integer($price)? &#39;Integer&#39; : &#39;Double&#39;; //Integer整型

echo &#39;<hr>&#39;;

//is_numeric():判断是否是数字或数字型字符串

var_dump(is_numeric(100));

var_dump(is_numeric(&#39;200&#39;));

var_dump(is_numeric(&#39;200php&#39;));

Copy after login


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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

PHP Notice: Undefined variable:Solution PHP Notice: Undefined variable:Solution Jun 25, 2023 pm 04:18 PM

In PHP development, we often encounter the error message PHPNotice:Undefinedvariable. This error message means that we have used an undefined variable in the code. Although this error message will not cause the code to crash, it will affect the readability and maintainability of the code. Below, this article will introduce you to some methods to solve this error. 1. Use the error_reporting(E_ALL) function during the development process. In PHP development, we can

Strict mode and error handling of variable types in PHP Strict mode and error handling of variable types in PHP Sep 13, 2023 am 08:48 AM

Strict mode and error handling of variable types in PHP With the development of PHP, more and more developers have begun to pay attention to the quality and stability of PHP code. In the past, PHP was considered a flexible and loose language, with no strict restrictions on the types of variables. This improves development efficiency to a certain extent, but it can also easily lead to some potential errors. To solve this problem, PHP introduced strict mode and error handling mechanism. 1. Strict mode Strict mode can be turned on by setting configuration items. Versions after PHP7

Detection and filtering of variable types in PHP Detection and filtering of variable types in PHP Sep 13, 2023 pm 02:37 PM

Detection and filtering of variable types in PHP require specific code examples. PHP is a powerful server-side scripting language that is widely used in the field of web development. In PHP, variable type detection and filtering are very important, which can effectively prevent security holes and incorrect data processing. This article will introduce some commonly used PHP variable type detection and filtering methods, and provide specific code examples. Detecting variable types In PHP, we can use a variety of methods to detect variable types, including is_int(), is_f

How to use numeric variables in PHP How to use numeric variables in PHP Sep 13, 2023 pm 12:46 PM

How to use numeric variables in PHP In PHP, a numeric variable is a variable type that is used directly without declaration. You can use numeric variables to perform mathematical calculations, data comparisons, and other numerical operations. This article will explain how to use numeric variables in PHP and provide specific code examples. Defining numeric variables In PHP, defining numeric variables is very simple, just assign a number directly to the variable. Here is an example: $number=10; In the above code, we define a value called $numb

PHP Notice: Undefined variable: arr in solution PHP Notice: Undefined variable: arr in solution Jun 22, 2023 am 10:21 AM

Solution to PHPNotice:Undefinedvariable:arrin In PHP programming, we often encounter the error message "Notice:Undefinedvariable". This error message is usually caused by accessing an undefined variable or the variable has not been initialized. For this problem, we need to find the problem and solve it in time. In this article, we will focus on PHPNotice:Undefin

How to quickly eliminate PHP variable undefined errors? How to quickly eliminate PHP variable undefined errors? Dec 17, 2023 am 10:23 AM

How to quickly eliminate PHP variable undefined errors? In PHP development, we often encounter undefined variable errors. This is because an unassigned variable is used in the code. When encountering this kind of error, we need to quickly find the cause of the error and solve it. Here are some ways to quickly troubleshoot PHP variable undefined errors to help you locate and fix errors faster. Turn on error reporting: When we turn on error reporting, PHP will display all error and warning messages, including variable undefined errors. We can do this by opening the code

PHP Notice: Undefined variable: sql solution PHP Notice: Undefined variable: sql solution Jun 23, 2023 am 08:51 AM

When developing a PHP application, if you encounter the "Undefined variable: sql" prompt, it usually means that you are referencing an undefined variable. This can be due to many reasons, such as misspellings of variable names, scoping issues, or syntax errors in the code, etc. In this article, we will explore the various causes of this problem and provide some ways to solve it. 1. Variable name is misspelled In your PHP code, if the variable name is incorrect or misspelled, the system

How to pass PHP variables by reference How to pass PHP variables by reference Aug 26, 2023 am 09:01 AM

In PHP, you can use the ampersand (&) symbol to pass variables by reference instead of by value. This allows the original variable to be modified within a function or method. There are mainly two ways to pass PHP variables by reference: Using ampersand symbol Using ampersand symbol in function/method declaration Using ampersand symbol in function/method declaration When passing variables to function/method In PHP, you can use function/ The ampersand symbol (&) in a method declaration passes variables by reference. Here is the updated explanation: To pass a reference variable by using the & symbol in a function/method declaration, you need to include the & symbol before the parameter name in the function/method definition. This indicates that parameters should be passed by reference, allowing