Home  >  Article  >  Backend Development  >  Operation examples of functions in php

Operation examples of functions in php

黄舟
黄舟Original
2017-08-22 09:10:531295browse

The following editor will bring you an operation method based on PHP functions. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.

is as follows:


<?php

//简单函数
function show(){
  echo "hello";  
}
show(); 

//有参数的函数
function show($a){
  echo "$a";  
}
show("world"); 

//有返回值的函数
 function show(){
  return "小V,你好!";  
}
echo show(); 


function show($a,$b){
  return $a+$b;  
}
echo show(10,5); 

//可变参数的函数(特殊用法)
 function sum(){
  $arr = func_get_args();
  $sum = 0;
  for($i=0;$i<count($arr);$i++){
    $sum+=$arr[$i];
  }
  echo $sum;
} 
sum(1,2,5);

//常用函数
echo rand();//获取随机数
echo time();//取当前时间戳
echo date("Y-m-d H:i:s",time());//格式化显示时间
echo strtotime("2017-8-21 10:00:00")//将时间转为时间戳


//字符串函数
$s = "小V,你好!";
$s1 = "hello world";
echo strlen($s1);
echo strtolower($s1);
echo strtoupper($s1);

//拆分字符串
$str = "hello|mike|nice|159";
var_dump(explode("|",$str));


//合并字符串
$arr = [2,5,9,6,3];
echo implode($arr);


//替换字符串
echo substr_replace($str,"###",16,3);
echo str_replace("|","@",$str);


//截取字符串
var_dump(substr($str,10,4));

//正则表达式
  定界符:/开头 /结束
   ^匹配开头 $匹配结尾
   *前面的表达式可以出现n次 +前面的表达式至少出现一次
  ?前面的表达式可以出现0,1次 
  {n}匹配n次 {n,}至少出现n次最多不限 {n,m}至少n次最多m次
  x|y 或的意思 [abc]匹配其中任意一个 [a-z]匹配a到z之间的任意一个
  \d任意一个数字 \S任何可见字符 \w包括下划线的任意单词字符


$s = a1b2c3d4e5;
echo preg_replace("/\d/","*",$s);//替换
var_dump(preg_split("/\d/",$s));//拆分

//匹配第一个满足正则的类型
$s = file_get_contets();//获取内容
$arr = array();
preg_match("",$s,$arr); 
?>

The above is the detailed content of Operation examples of functions in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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