Home > headlines > PHP variable types and conversions

PHP variable types and conversions

无忌哥哥
Release: 2018-06-27 17:40:57
Original
2534 people have browsed it

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.

$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

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

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

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

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

* /

$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


Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template