博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
php 类型介绍(四中标量类型+数组)
阅读量:4115 次
发布时间:2019-05-25

本文共 4911 字,大约阅读时间需要 16 分钟。

 

php支持八种原始类型:

 

四种标量类型:

1、 boolean 布尔型

2、 integer 整型

3、 float 浮点型(也做double)

4、string (字符串)

 

两种符合类型:

 array 数组

object 对象

 

最后是两种特殊类型:

resource 资源

null 

 

为了确保代码的易读性,本手册还介绍了一些:

 

注: 如果想查看某个的值和类型,用 。

注: 如果只是想得到一个易读懂的类型的表达方式用于调试,用 。要查看某个类型,不要用 ,而用 is_type 函数。以下是一些范例:

// If this is an integer, increment it by four

if (is_int($int)) {
    
$int += 4;
}
// If $bool is a string, print it out
// (does not print out anything)
if (is_string($bool)) {
    echo
"String: $bool";
}
 

一、布尔类型

当转换为 时,以下值被认为是 FALSE

  • FALSE

  • 值 0(零)

  • 值 0.0(零)

  • 空白和 "0"

  • 没有成员变量的

  • 没有单元的(仅适用于 PHP 4)

  • 特殊类型 (包括尚未设定的变量)

<?php

var_dump((bool) "");        // bool(false)

var_dump((bool) 1);         // bool(true)
var_dump((bool) -2);        // bool(true)
var_dump((bool) "foo");     // bool(true)
var_dump((bool) 2.3e5);     // bool(true)
var_dump((bool) array(12)); // bool(true)
var_dump((bool) array());   // bool(false)

var_dump((bool) "false");   // bool(true)

?>

二、整型

一个 是集合 Z = {..., -2, -1, 0, 1, 2, ...} 中的一个数。

参见,和。

整型值可以用十进制,十六进制或八进制符号指定,前面可以加上可选的符号(- 或者 +)。

如果用八进制符号,数字前必须加上 0(零),用十六进制符号数字前必须加上 0x

 

例子:

<?php

$a = 1234; // 十进制数

$a = -123; // 一个负数
$a = 0123; // 八进制数(等于十进制的 83)
$a = 0x1A; // 十六进制数(等于十进制的 26)

?>

 

如果向八进制数传递了一个非法数字(即 8 或 9),则后面其余数字会被忽略。

例子 11-2. 八进制数的怪事

<?php
var_dump
(01090); // 010 octal = 8 decimal
?>

 

PHP 中没有整除的运算符。1/2 产生出 0.5。可以总是舍弃小数部分,或者使用 函数。 --(四舍五入)

<?php
var_dump
(25/7);         // float(3.5714285714286)
var_dump((int) (25/7)); // int(3)
var_dump(round(25/7));  // float(4)
?>

强制转换为整形:

 

转换为整形:

int or interger() 强制转换   或者  调用函数intval("37899") 转换

 

从布尔值转换到整形

FALSE  将生出 0(零) 

True 将产生出1 (壹)

 

example:

 var_dump((int)(1>9)); //int 0

 var_dump((int) (4>2));//int 1

 

从浮点数转换为整形时,数字将被取整(丢弃小数位)

var_dump((int)9.38475856); //int 9

 

三、字符串型

 

单引号

双引号

定界符

 

1、单引号  

要表示一个单引号,需要用反斜线(\)转义,和很多其它语言一样。如果在单引号之前或字符串结尾需要出现一个反斜线,需要用两个反斜线表示。注意如果试图转义任何其它字符,反斜线本身也会被显示出来!所以通常不需要转义反斜线本身。

ex:

 

// Outputs: Arnold once said: "I'll be back"

echo 'Arnold once said: "I\'ll be back"';
// Outputs: You deleted C:\*.*?
echo 'You deleted C:\\*.*?'
;
// Outputs: You deleted C:\*.*?
echo 'You deleted C:\*.*?'
;
// Outputs: This will not expand: \n a newline
echo 'This will not expand: \n a newline'
;
// Outputs: Variables do not $expand $either
echo 'Variables do not $expand $either'
;

 

关于字符串的例子:

截取字符串:

<?php

// Get the first character of a string  ---- 得到字符串的第一个字符
$str = 'This is a test.';
$first = $str{
0};
// Get the third character of a string  ----  得到字符串的第三个字符
$third = $str{
2};
// Get the last character of a string.  ------------ 得到字符串的最后一个字符
$str = 'This is still a test.';
$last = $str{
strlen($str)-1};
// Modify the last character of a string
$str = 'Look at the sea';
$str{
strlen($str)-1} = 'e';
//输出结果:

Look at the see

?>

 

 

五、数组:

定义

可以用 语言结构来新建一个 。它接受一定数量用逗号分隔的 key => value 参数对。

 

array( [key =>]value     , ...     )// key 可以是  或者 // value 可以是任何值

<?php

$arr = array("foo" => "bar", 12 => true);
echo
$arr["foo"]; // bar
echo $arr[12];    // 1
?>

foo , 12 是key值

bar ,true  是value值

 

嵌套数组形式:

 

<?php

$arr = array("somearray" => array(6 => 5, 13 => 9, "a" => 42));
echo
$arr["somearray"][6];    // 5
echo $arr["somearray"][13];   // 9
echo $arr["somearray"]["a"];  // 42
?>

 

========================

数组的添加和删除元素

========================

<?php

$arr = array(5 => 1, 12 => 2);
$arr[] = 56;    // This is the same as $arr[13] = 56;  
                // at this point of the script

supplements:

$arr =  array(6=>5, 20=>9,"a"=>42);

  $arr[]=45;
  print_r($arr);//Array ( [6] => 5 [20] => 9 [a] => 42 [21] => 45 )  在最后一个是integer的key值加1

 

 

$arr["x"] = 42; // This adds a new element to                              为数组添加一个key=“x”的新元素
                // the array with key "x"

 

===========results:===================

 

Array ( [6] => 5 [20] => 9 [a] => 42 [21] => 45 [x] => 42 )

 

====================================

unset($arr[5]); // This removes the element from the array   删除key=5的指定元素
unset($arr);    // This deletes the whole array

?>

 

 

数组的简单操作

--遍历数组1:  for-each:

<?php
// 创建一个简单的数组
$array = array(1, 2, 3, 4, 5);
print_r($array);
// 现在删除其中的所有单元,但保持数组本身的结构
foreach ($array as $i => $value) {
    unset(
$array[$i]);
}
print_r($array);
// 添加一个单元(注意新的键名是 5,而不是你可能以为的 0)
$array[] = 6;
print_r($array);
// 重新索引:
$array = array_values($array);
$array[] = 7;
print_r($array);
?>

Array(    [0] => 1    [1] => 2    [2] => 3    [3] => 4    [4] => 5)Array()Array(    [5] => 6)Array(    [0] => 6    [1] => 7)

 

 

2、遍历数组2:  for

下标自增长0---

$array = array(1, 2);

$count = count($array);
for (
$i = 0; $i < $count; $i++) {
   
    echo  $array[$i] . "\n";
}

key值为下标自增长的简单数组:

 

<?php
$colors
= array('red', 'blue', 'green', 'yellow');
foreach (
$colors as $color) {
    echo
"Do you like $color?\n";
}
?>

上例将输出:

Do you like red?Do you like blue?Do you like green?Do you like yellow?

for-each:

$colors = array('red', 'blue', 'green', 'yellow');

foreach ($colors as $key => $color) {

    echo $colors[$key];
}

 

//复制数组

 

*******

和java以往不同,php的数组的复制时联动的,复制的原始数组会随着被复制的新数组的添加或是删除元素也相应的做出改变

*******

<?php

$arr1 = array(2, 3);
$arr2 = $arr1;
$arr2[] = 4; // $arr2 is changed,
             // $arr1 is still array(2,3)
$arr3 = &$arr1;
$arr3[] = 4; // now $arr1 and $arr3 are the same
?>

 

///数组的简单操作  end//

 

 

转载地址:http://uqupi.baihongyu.com/

你可能感兴趣的文章
剑指offer算法题分析与整理(三)
查看>>
带WiringPi库的交叉笔译如何处理二之软链接概念
查看>>
Java8 HashMap集合解析
查看>>
自定义 select 下拉框 多选插件
查看>>
fastcgi_param 详解
查看>>
poj 1976 A Mini Locomotive (dp 二维01背包)
查看>>
MODULE_DEVICE_TABLE的理解
查看>>
db db2_monitorTool IBM Rational Performace Tester
查看>>
postgresql监控工具pgstatspack的安装及使用
查看>>
【JAVA数据结构】双向链表
查看>>
【JAVA数据结构】先进先出队列
查看>>
乘法逆元
查看>>
Objective-C 基础入门(一)
查看>>
Flutter Boost的router管理
查看>>
iOS开发支付集成之微信支付
查看>>
C++模板
查看>>
【C#】如何实现一个迭代器
查看>>
【C#】利用Conditional属性完成编译忽略
查看>>
DirectX11 光照演示示例Demo
查看>>
VUe+webpack构建单页router应用(一)
查看>>