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);
$fruit[0] = "A";
$fruit['1'] = "B";
print_r($fruit);
$temp = $fruit['0'];
print_r($temp);
2,函數
1,定義函數
testFunction();
function testFunction()
{
echo 'this is a test function';
}
$name = 'testFunction';
$name();
$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();
}
}