/***************************by
garcon1986********************************/
<?php
//php avancé 5 example
$a = 'hello';
$a .= 'world';
$table = 'users';
$id = 5;
$sql = 'SELECT * from'.$table. "WHERE ID = '$id'";
//date() G显示24小时的格式,i显示分钟
echo 'il est'.date('G \h i').'il fait beau<p>';
// '=', '==', '===' 三者区别: '='是赋值,右边的值赋给左边;'=='是值相等,类型不一定等;'==='是恒等,值相等类型也相等;'!=='是值不等,或类型不等。
$a = '34';
$b = 34;
$a == $b;
//echo (boolean)($a == $b);
var_dump((bool) $a == $b);
echo '<br>';
$a === $b;
//echo (boolean)($a === $b);
var_dump((bool) $a === $b);
echo '<br>';
var_dump((bool)($a !== $b));
echo '<p>';
//arithmetic operator
echo 5*"", '<br>'; //0
echo 5+false, '<br>'; //5
echo 5/NULL, '<br>'; //Warning: Division by zero
//assignment operator
$a = ($b = 4) + 5; // $a 现在成了 9,而 $b 成了 4。
echo "$a,$b<br>";
$a = ......
/***************************by
garcon1986********************************/
<?php
// -> 是指对象的方法或者属性
class Cart{
public function add_item($a,$b){
echo $a+$b.'<br>';
}
}
//$cart = new Cart; 两句意义相同
$cart = new Cart();
$cart->add_item("10", 1);
// => 数组的元素赋值
//键值one对应的值是1, 键值two对应的值是2,键值对应的值是3.
$a = array("one"=>1, "two"=>2, "three"=>3);
echo $a["one"].'<br>';
echo $a["two"].'<br>';
echo $a["three"].'<br>';
//$key=>$value 指键值对应的值
foreach($a as $key => $value){
echo "The \$key is: $key, The \$value is: $value<br>";
}
?>
......
/***************************by
garcon1986********************************/
<?php
// variable name is sensitive
$var = "sjg";
$Var = "wl";
echo $var.' loves '.$Var.'<br>';
echo "$var, $Var<p>";
//naming conventions for variables
//$4site = 'not yet'; // invalid; starts with a number
$_4site = 'not yet'; // valid; starts with an underscore
$täyte = 'mansikka'; // valid; '洄 is (Extended) ASCII 228.
echo $_4site.'<br>';
echo $täyte.'<p>';
// pass the address
$ref1 = 'ref1';
$ref2 = &$ref1;
echo $ref1.'<br>'; //ref1
echo $ref2.'<br>'; //ref1
$ref2 = "My name is $ref2";
echo $ref1.'<br>'; //ref1
echo $ref2.'<p>'; //ref1
//$ref2 = &(24*7); //Invalid reference.
function test1()
{
return 25;
}
//$bar = &test(); // Invalid reference.
//global scope ;
$a1 = 1; /* global scope */
function test2()
{
$a1 = 2; // local variable
echo $a1.'& ......
/***************************by
garcon1986********************************/
<?php
//example1
$makefoo = true;
bar();
if($makefoo){
function foo(){
echo "doesn't exist.<br>";
}
}
if($makefoo)foo();
function bar(){
echo "exist<br>";
}
//example2
function sjg(){
function wl(){
echo 'must call sjg() before wl()!<br>';
}
}
sjg();
wl();
//wl();
//sjg();
//example3 - 递归
$a = 10;
function recursion($a){
if($a <= 20){
echo "$a\n";
recursion($a+1);
}
}
recursion($a);
echo "<br>";
//example4 - 向函数传递数组
$input1=array('hi','ni','hao','ya');
function record($input1){
echo $input1[0],$input1[1],$input1[2];
}
record($input1);
//pass the function's parameter by reference
function add_some_extra(&$string){
$string .= 'wl wo ai ni.';
}
$str1 = 'wo xiang shuo,';
add_some_extra($str1);
echo $str1.'<br>'; //output: wo xiang shuo, wl wo ai ni.
//default parameter 默认参数
......
/***************************by
garcon1986********************************/
<?php
//简单示例
class SimpleClass
{
public $var = 'a default value';
public function displayVar(){
echo $this->var;
}
}
// create an object创建一个对象
$A = new SimpleClass;
//调用方法
$A -> displayVar();
echo '<p>';
//example2
class A{
function sjg(){
if(isset($this)){
echo '$this is defined.<br>\n';
echo get_class($this); //返回对象所属的类的名字
echo ")<br>";
}else {
echo "\$this is not defined.<br> \n";
}
}
}
class B{
function bar(){
A::sjg();
}
}
$a = new A();
$a -> sjg();
A::sjg();
$b = new B();
$b -> bar();
B::bar();
echo '<p>';
//example3
//创建一个实例
$instance = new SimpleClass();
//对象赋值
$assigned = $instance;
$reference =& $instance;
$instance->var = '$assigned will have this value';
$instance = null;
var_dump($instance);
echo "<br>";
var_dump($reference);
e ......
/***************************by
garcon1986********************************/
<?php
// simple assgin the values
$arr1 = array(12 => 10, 'sjg' => 'yaya');
echo $arr1[12].'<br>'; // 10
echo $arr1['sjg']."<br>"; //yaya
echo "wo ai ni !<p>"; // wo ai ni!
// place an array into another array
$arr2 = array("somearray" => array(6=>5, 13=>9, "a"=>42));
echo $arr2["somearray"][6]."<br>"; //5
echo $arr2["somearray"][13]."<br>"; //9
echo $arr2["somearray"]["a"]."<br>"; //42
// re-define array
$arr3 = array(1=>2, 3=>4, 5=>6);
echo $arr3[1]."<br>"; //2
echo $arr3[3]."<br>"; //4
$arr3 = array(1=>3, 2=>5, 3=>6, 4=>7);
echo $arr3[1]."<br>"; //3
echo $arr3[3]."<br>"; //6
$arr4=array(1,2,3,4, ......