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_replace — Perform a regular expression search and replace
preg_replace( '|(?<!%)%s|', "'%s'", $query );s
本文介绍了PHP中常用的几个函数,包括检查数组中是否存在特定值的in_array()、使用全局变量的方法、判断文件或目录是否存在的file_exists()、查找字符串首次出现位置的strpos()、格式化字符串的sprintf()、替换字符串的str_replace()以及正则表达式的搜索替换函数preg_replace()。

3164

被折叠的 条评论
为什么被折叠?



