PHP常用函数unset,iterator_to_array,str相关函数

1. unset

    unset — 释放给定的变量.

    unset() 销毁指定的变量。

    unset() 在函数中的行为会依赖于想要销毁的变量的类型而有所不同。

    如果在函数中 unset() 一个全局变量,则只是局部变量被销毁,而在调用环境中的变量将保持调用 unset() 之前一样的值。

function destroy_foo() {

    global $foo;

    unset($foo);

}

$foo = 'bar';

destroy_foo();

echo $foo;

?>

输出:

bar

unset() 一个全局变量 unset($GLOBALS['bar'])

function foo()

{

    unset($GLOBALS['bar']);

}

$bar = "something";

foo();

var_dump($bar);

?>

输出:

PHP Notice:  Undefined variable: bar in /tmp/f7b6ab11-7bda-4739-9287-215ec2f4ab9d/code on line 9

NULL

如果在函数中 unset() 一个通过引用传递的变量,则只是局部变量被销毁,而在调用环境中的变量将保持调用 unset() 之前一样的值。

function foo(&$bar) {

    unset($bar);

    $bar = "blah";

}

$bar = 'something';

echo "$bar\n";

foo($bar);

echo "$bar\n";

?>

输出:

something

something

如果在函数中 unset() 一个静态变量,那么在函数内部此静态变量将被销毁。但是,当再次调用此函数时,此静态变量将被复原为上次被销毁之前的值。

function foo()

{

    static $bar;

    $bar++;

    echo "Before unset: $bar, ";

    unset($bar);

    $bar = 23;

    echo "after unset: $bar\n";

}

foo();

foo();

foo();

?>

输出:

Before unset: 1, after unset: 23

Before unset: 2, after unset: 23

Before unset: 3, after unset: 23

// 销毁单个变量

unset ($foo);

// 销毁单个数组元素

unset ($bar['quux']);

// 销毁一个以上的变量

unset($foo1, $foo2, $foo3);

(unset) 类型强制转换常常和函数 unset() 引起困惑。 为了完整性,(unset) 是作为一个 NULL 类型的强制转换。它不会改变变量的类型。


$name = 'Felipe';

var_dump((unset) $name);

var_dump($name);

输出:

NULL

string(6) "Felipe"

2. iterator_to_array

iterator_to_array — 将迭代器中的元素拷贝到数组

array iterator_to_array ( Traversable $iterator [, bool $use_keys = true ] )

1. iterator

    被拷贝的迭代器。

2. use_keys  

    是否使用迭代器元素键作为索引。

3. 返回值

    一个数组,包含迭代器中的元素。


$iterator = new ArrayIterator(array('recipe'=>'pancakes', 'egg', 'milk', 'flour'));

var_dump(iterator_to_array($iterator, true));

var_dump(iterator_to_array($iterator, false));

输出:

array(4) {

["recipe"]=>

string(8) "pancakes"

[0]=>

string(3) "egg"

[1]=>

string(4) "milk"

[2]=>

string(5) "flour"

}

array(4) {

[0]=>

string(8) "pancakes"

[1]=>

string(3) "egg"

[2]=>

string(4) "milk"

[3]=>

string(5) "flour"

}

3. array_push

array_push — 将一个或多个单元压入数组的末尾(入栈)

array_push() 将 array 当成一个栈,并将传入的变量压入 array 的末尾。array 的长度将根据入栈变量的数目增加。

int array_push ( array &$array , mixed $var [, mixed $... ] )

1. array

    输入的数组。

2. var   

    要压入的值。

3. 返回值

    返回处理之后数组的元素个数。


$stack = array("orange", "banana");

array_push($stack, "apple", "raspberry");

print_r($stack);

输出:

Array

(

    [0] => orange

    [1] => banana

    [2] => apple

    [3] => raspberry

)

4. strtolower

strtolower — 将字符串转化为小写

$str = "Mary Had A Little Lamb and She LOVED It So";

$str = strtolower($str);

echo $str; // 打印 mary had a little lamb and she loved it so

输出:

mary had a little lamb and she loved it so

5. strtoupper

strtoupper — 将字符串转化为大写


$str = "Mary Had A Little Lamb and She LOVED It So";

$str = strtoupper($str);

echo $str; // 打印 MARY HAD A LITTLE LAMB AND SHE LOVED IT SO


输出:

MARY HAD A LITTLE LAMB AND SHE LOVED IT SO

6. strpos

strpos — 查找字符串首次出现的位置

mixed strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )

haystack

    在该字符串中进行查找。

needle

    如果 needle 不是一个字符串,那么它将被转换为整型并被视为字符的顺序值。

offset

    如果提供了此参数,搜索会从字符串该字符数的起始位置开始统计。和 strrpos()、 strripos()不一样,这个偏移量不能是负数。

返回值

    返回 needle 存在于 haystack 字符串起始的位置(独立于 offset)。同时注意字符串位置是从0开始,而不是从1开始的。

如果没找到 needle,将返回 FALSE。

例1:

$mystring = 'abc';

$findme   = 'a';

$pos = strpos($mystring, $findme);

// 注意这里使用的是 ===。简单的 == 不能像我们期待的那样工作,

// 因为 'a' 是第 0 位置上的(第一个)字符。

if ($pos === false) {

    echo "The string '$findme' was not found in the string '$mystring'";

} else {

    echo "The string '$findme' was found in the string '$mystring'";

    echo " and exists at position $pos";

}

输出:

The string 'a' was found in the string 'abc' and exists at position 0

例2:

$mystring = 'abc';

$findme   = 'a';

$pos = strpos($mystring, $findme);

// 使用 !== 操作符。使用 != 不能像我们期待的那样工作,

// 因为 'a' 的位置是 0。语句 (0 != false) 的结果是 false。

if ($pos !== false) {

    echo "The string '$findme' was found in the string '$mystring'";

    echo " and exists at position $pos";

} else {

    echo "The string '$findme' was not found in the string '$mystring'";

}

输出:

The string 'a' was found in the string 'abc' and exists at position 0

例3:

// 忽视位置偏移量之前的字符进行查找

$newstring = 'abcdef abcdef';

$pos = strpos($newstring, 'a', 1); // $pos = 7, 不是 0

echo $pos

输出:

7

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,991评论 19 139
  • number abs ( mixed $number ) 返回参数 number 的绝对值。echo abs(-9...
    张松松阅读 293评论 0 1
  • 要访问一个变量的内容,可以直接使用其名称。如果该变量是一个数组,可以使用变量名称和关键字或索引的组合来访问其内容。...
    恩德_b0c2阅读 406评论 0 0
  • PHP7 已经出来1年了,PHP7.1也即将和大家见面,这么多好的特性,好的方法,为什么不使用呢,也希望PHP越来...
    梦幻_78af阅读 2,133评论 1 10
  • 一、字符串操作 PHP开发中,我们遇到最多的可能就是字符串。 一个字符串 通过下面的3种方法来定义: 1、单引号 ...
    空谷悠阅读 792评论 1 6