php function

本文介绍了PHP中常用的几个函数,包括检查数组中是否存在特定值的in_array()、使用全局变量的方法、判断文件或目录是否存在的file_exists()、查找字符串首次出现位置的strpos()、格式化字符串的sprintf()、替换字符串的str_replace()以及正则表达式的搜索替换函数preg_replace()。

in_array()

:checks if a value exists in an array;

 

example

:

var $public_query_vars = array('m', 'p');

 

if ( !in_array($qv, $this->public_query_vars) )
   $this->public_query_vars[] = $qv;

 

 

global variable

 

the method of use  the global variable  in a function

 

method 1:

<?php
$a 
1;
$b 2;

function 
Sum()
{
    global 
$a$b;

    
$b $a $b;


Sum();
echo 
$b;
?>

 

method 2:

<?php
$a 
1;
$b 2;

function 
Sum()
{
    
$GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];


Sum();
echo 
$b;
?>
 
 
static variable
 
example
 
<?php
function test()
{
    static 
$count 0;

    
$count++;
    echo 
$count;
    if (
$count 10) {
        
test();
    }
    
$count--;
}
?>

 

 

file_exists()

check if  a file or directory  exists

 

 

strpos ()

find position of first occurence of a string .

 

if it is not found ,it will return false;

 

sprintf(string format,)

return a formatted string.

 

 

<?php
$format 
'There are %d monkeys in the %s';
printf($format$num$location);
?>
<?php
$format 
'The %2$s contains %1$d monkeys';
printf($format$num$location);
?>

 

 str_replace

 

str_replace( "'%s'", '%s', $query )

 

Replace all occurrences of the search string with the replacement string

 

 

preg_replace

 

preg_replacePerform a regular expression search and replace

 preg_replace( '|(?<!%)%s|', "'%s'", $query );s

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值