Get the App
SLTechnology News&Howtos  ›  Development  › 

How to convert data types in php

Shulou Source: shulou.com Published: 2022-06-01 13:10:43 10月03日 Update

This article will explain in detail how to convert data types in php. Xiaobian thinks it is quite practical, so share it with you for reference. I hope you can gain something after reading this article.

Php data type conversion

Note: data types in php can be converted to each other. For example, integer 1 converts string 1

There are two types of conversion in PHP:

Automatic conversion:

When given a specific type, it changes the original type.

//type auto-conversion $num1 = 1;$num2 = 2 ;$num3 = $num1 . $num2; //autoconvert var_dump($num3) //string:12if($num1){ //autoconvert boolean true; echo "Qingyu is handsome";} Forced conversion:

There are special ways to enforce conversion types

Permitted casts are:

· (int), (integer) -convert to integer

· (bool), (boolean) -convert to boolean

· (float), (double), (real) -convert to float

· (string) -converts to string

·array (array) â € "convert to array

· (object) -convert to object

· (unset) -converts to NULL (PHP 5)

Cast is mostly used when comparing types

//type coercion $num = 1;var_dump((int)$num1); //int:1$str = "hello";var_dump((int)$str); //int:0//coercion is mostly used for type comparison. About "how to convert data types in php" This article is shared here. I hope the above content can be helpful to everyone, so that you can learn more knowledge. If you think the article is good, please share it for more people to see.

Tags: Type mandatory data article character string more good practical special hello content writing object Boolean mandatory form array article method Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Microsoft Shulou Technology Huawei OPPO Reno