1,数组
1,初始化
$arr = array(); // 新建空数组
$demo = array(
'1' => 'B',
'2' => 'Fuck'
);// key->value形式数组
$sample = array(
'C',
'D'
);// 0->value形式数组
$fruit = array(
"苹果",
"香蕉",
"菠萝"
);// 0->value形式数组
print_r($arr);
print_r($fruit);
print_r($demo);
print_r($sample);
2,被赋值
$fruit[0] = "A";
$fruit['1'] = "B";
print_r($fruit);
3,赋值
$temp = $fruit['0'];
print_r($temp);
2,函数
1,定义函数
testFunction();
function testFunction()
{
echo 'this is a test function';
}
2,函数赋值给变量(将函数作为一个变量看待)
$name = 'testFunction';
$name();
3,系统函数,介绍几个
$str = '苹果很好吃。';
$str = str_replace('苹果', '香蕉', $str); // 替换字符串
echo $str;
echo function_exists('testFunction')."function\n"; // 函数是否存在
echo class_exists('index')."class\n"; // 类是否存在
echo file_exists('index')."file\n"; // 文件是否存在
3,类与面向对象
1,类介绍(注释中已经 写全)
class Car
{
/* ---------------------------访问修饰符--------------------------- */
private $tempA = 'A'; // 本类 ok
protected $tempB = 'B'; // 本类 ok , 子类 ok
public $tempC = 'C'; // 本类 ok , 子类 ok , 其它类 ok
var $name = '汽车'; // 本类 ok , 子类 ok , 其它类 ok , 等同于 public
/* ---------------------------特殊函数--------------------------- */
/**
* new Car() 时调用
* 构造函数
*/
function __construct()
{
echo "Car __construct" . "\n";
}
/**
* unset($car)时,调用
* 析构函数
*/
function __destruct()
{
echo "Car __destruct" . "\n";
}
/**
* 当调用不存在的方法的时候,将会转为参数调用__call方法
*
* @param unknown $name
* @param unknown $arg
*/
public function __call($name, $arg)
{
if ($name == 'speedDown')
{
$this->speed -= 10;
}
}
/**
* 复制一个 car对象
* clone一个类
*/
public function __clone()
{
$obj = new Car();
$obj->name = $this->name;
}
/* ---------------------------静态变量--------------------------- */
private static $fast_speed = 10;
/**
* 静态方法
*
* @return number
*/
public function getFastSpeed()
{
return self::$fast_speed;
}
/**
* 静态方法
*/
public static function fastSpeedUp()
{
return self::$fast_speed += 10;
}
/* ---------------------------常用变量--------------------------- */
public $speed = 0;
function getName()
{
return $this->name;
}
/**
* $speed 不会改变 Car类中 speed 的值
*/
function speedUp()
{
$speed += 10;
}
/**
* $this->speed 会改变 Car类中 speed 的值
*/
function speedUp2()
{
$this->speed += 10;
}
}
/* ---------------------------继承关系--------------------------- */
class BigCar extends Car
{
function __construct()
{
echo "BigCar __construct" . "\n";
parent::__construct();
}
}
有技术上的问题,或者想法,欢迎来交流
QQ联系:957339173@qq.com // 备注 CSDN
github:https://github.com/yline