一个单例
class Singleton
{
//创建静态私有的变量保存该类对象
static private $instance;
//防止使用new直接创建对象
private function __construct(){}
//防止使用clone克隆对象
private function __clone(){}
static public function getInstance()
{
//判断$instance是否是Singleton的对象,不是则创建
if (!self::$instance instanceof self) {
self::$instance = new self();
}
return self::$instance;
}
public function test()
{
echo "我是一个单例模式";
}
}
$sing = Singleton::getInstance();
$sing->test();
写一个函数 遍历文件夹下的文件和子文件夹
<?php
function my_scandir($dir){
$files = array();
if(is_dir($dir)){
if($handle = opendir($dir)){
while(($file = readdir($handle)) !== false){
if ( $file != ".." && $file != "." ) {
if ( is_dir($dir . "/" . $file) ) { //测试文件是否为目录
$files[$file] = my_scandir($dir . "/" . $file);
//scandir($dir) 函数返回一个数组,其中包含指定路径中的文件和目录。
}else {
$files[] = $file;
}
}
}
}
}
closedir($handle);
return $files;
}
?>
//两个大数相加
$a = "242424234241234";
$b = "131239999";
echo add($a, $b);
function add($a, $b){
$arr = str_split(strrev($a));
$brr = str_split(strrev($b));
$alength = count($arr);
$blength = count($brr);
$max = $alength > $blength ? $alength : $blength;
$result = array();
for($i=0;$i<$max;$i++){
$num = intval($arr[$i])+ intval($brr[$i]) + intval($result[$i]);
if($num > 10){
$result[$i] = $num % 10;
$result[$i+1] = 1;
}else{
$result[$i] = $num;
}
}
$str = implode("", $result);
return strrev($str);
}