search
HomeheadlinesPHP variable types and conversions

echo &#39;<h3 id="变量类型与转换">2.变量类型与转换</h3>&#39;;
echo &#39;<hr color="green">&#39;;

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

$age = 30; //1整型 integer
$salary = 1234.56; //2.浮点 float
$name = &#39;peter&#39;; //3 字符串
$isMarried = true;  //4. 布尔型,true真,false假

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

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;;

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

$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

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

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;;

//Special Type: resource type, null

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

$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)

* /

$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;));


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

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

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.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor