PHP lernen 00

WBOY
Freigeben: 2016-07-29 09:14:25
Original
1128 Leute haben es durchsucht

<code><span><?php</span><span>/*
 * 大写小敏感
 * 变量区分大小写,函数,关键字(if,else),echo...不区分大小写
 * 
 */</span><span>//$str = "hehh";</span><span>//ECHO $str;</span><span>/*
 * 变量作用域
 * 2种作用域
 * 1. local 局部
 * 2. global 全局
 * 在函数之外声明的变量具有全局作用域
 * 在函数之内声明的变量具有局部作用域,只能在函数内部才可以访问
 * global 关键词
 * global 用于在函数内部访问全部变量
 * php在 $GLOBAL[index]数组中保存了所有的<strong>全局变量</strong>,下标是<strong>全局变量</strong>名,在函数内部也可以使用,可以用这个数组直接更新<strong>全局变量</strong>
 * static 关键词
 * 用于在函数内部声明局部变量,此变量在函数调用完成后不会销毁,即存储在堆中,而不是栈中
 */</span><span>$x</span> = <span>5</span>;
<span>$y</span> = <span>10</span>;

<span><span>function</span><span>myTest</span><span>()</span> {</span><span>$y</span> = <span>10</span>;
    <span>echo</span><span>"变量x是:"</span> . <span>$x</span>;
    <span>echo</span><span>"<br/>"</span>;
    <span>echo</span><span>"变量y是:$y"</span>;
}

<span>//myTest();</span><span>//echo "变量x是:" . $x;</span><span>//echo "<br/>";</span><span>//echo "变量y是:$y";</span><span>//print_r($GLOBALS);</span><span><span>function</span><span>myTest1</span><span>()</span> {</span><span>global</span><span>$x</span>, <span>$y</span>;
    <span>echo</span><span>"x变量是:$x"</span>;
    <span>echo</span><span>"<br/>"</span>;
    <span>echo</span><span>"变量是:$y"</span>;
    <span>echo</span><span>"<br/>"</span>;
    <span>$y</span> = <span>$x</span> + <span>$y</span>;
    <span>echo</span><span>"x+y是:$x+$y"</span>;
}

<span>//myTest1();</span><span>//echo  "<br/>";</span><span>//echo "y=$y";</span><span><span>function</span><span>myTest2</span><span>()</span> {</span><span>static</span><span>$x</span> = <span>0</span>;
    <span>echo</span><span>$x</span> . <span>"<br/>"</span>;
    <span>$x</span>++;
}

<span>//myTest2();</span><span>//myTest2();</span><span>//myTest2();</span><span>/*
 * echo 和 print
 * echo 能输出至少一个以上的字符串
 * print 只能输出一个字符串,返回1
 * echo 是语句,没有() ,加()也可以
 * echo 比print 稍快,因为没有返回值
 * print 也是语句 没有(),加()也可以
 * 
 * 
 * 
 */</span><span>//echo "ddddd";</span><span>//echo 'I',"am",'xue';</span><span>//print "<br/>";</span><span>//print "I";</span><span>/*
 * php数据类型
 * 1.字符串  'aaa',"aaa"
 * 2.整数,3种格式
 * 十进制
 * 十六进制 0x开头
 * 八进制 0开头
 * 3.浮点型,包含小树,或指数
 * 10.45,2.4e5 ,2.3e-3
 */</span><span>//$x = 123;</span><span>//var_dump($x);</span><span>//$y = 0x456;</span><span>//var_dump($y);</span><span>//$z = 015;</span><span>//var_dump($z);</span><span>//$a = 0.3;</span><span>//var_dump($a);</span><span>//$b = 0.3e2;</span><span>//var_dump($b);</span><span>/*
 * 数组:
 * 
 */</span><span>/*
 * NULL 表示变量值为无值
 * 
 */</span><span>/* 字符串函数
 * 1.strlen() 返回字符串的长度
 * 2.strpos() 查找子字符串或子字符
 * 返回被查找字符或字符串在原字符串中的第一个位置(以0开始)
 * 若没有找到,则返回FALSE
 */</span><span>//$str = "123";</span><span>//$len = strlen($str);</span><span>//echo $len;</span><span>//$str1 = "woshixue";</span><span>//var_dump(strpos($str1,"ee"));</span><span>/*
 * 常量:
 * 常量类似变量,一旦定义就无法撤销或更改定义,定义后为全局作用域
 * 无法在脚本中修改常量
 * define('name','value',可选(默认为false,))
 */</span><span>//define("NAME","xuexinghai",false);</span><span>//echo name;</span><span>/*
 * <strong>循环</strong>:
 * for,foreach,while,do...while
 */</span><span>// $arr = array("apple","orange","banana");</span><span>// foreach($arr as $v)</span><span>//     echo $v."<br/>";</span><span>/*
 * 数组
 * 1.数组的声明
 * 直接声明
 * 关联
 * 2 count 获取数组元素个数
 */</span><span>//直接声明</span><span>//$arr = array();</span><span>//$arr[0] =3;</span><span>//$arr[1]="xue";</span><span>//var_dump($arr);</span><span>////关联定义</span><span>//$arr1 = array("name"=>"xue",age=>25);</span><span>//var_dump($arr1);</span><span>////定义二维数组</span><span>//$arr2 = array(</span><span>//    0=>$arr,</span><span>//    1=>$arr1</span><span>//);</span><span>//var_dump($arr2);</span><span>//$arr3[] = $arr1;</span><span>//$arr3['xxx'] = $arr2;</span><span>//$arr3[7] = 33;</span><span>//var_dump($arr3);</span><span>//</span><span>//$arr4 = array();</span><span>//var_dump($arr4);</span><span>//</span><span>//echo "arr4的元素为:",count($arr4);</span><span>//数组排序</span><span>//1.sort,以升序对数组排序</span><span>//</span><span>/*
 * 超<strong>全局变量</strong>,可以在任何位置使用
 * $GLOBALS 保存全部的<strong>全局变量</strong>
 * $_SERVER 保存报头,路径,和脚本位置
 * $_REQUEST 用户保存HTML表单提交的数据
 * $_POST方式提交的表单
 * $_GET 以get方式提交的表单
 * $_FILES 上传的文件信息
 * $_FILES = array(
 *       'myFile'=>array(
 *          [name] => 医刊通-投审稿-后台demo-20150912.zip
  [type] => application/zip
  [tmp_name] => C:\Windows\Temp\phpDEB7.tmp
  [error] => 0
  [size] => 507713
 *      )
 *    )
 * $_ENV环境变量
 * $_COOKIE
 * $_SESSION
 */</span><span>//session_start();</span><span>//$_SESSION['name'] = "xue";</span><span>//VAR_DUMP($_SESSION);</span><span>//print_r($_SERVER);</span><span>//if(!empty($_POST['sub']))</span><span>//print_r($_FILES['myFile']['tmp_name']);</span><span>/*
 * 日期和时间
 * date(format,timestamp);
 * format:格式化
 * timpstamp:时间戳,默认为当前时间时间戳
 * format:
 *   d:一月中的某天 01-31
 *   
 * 
 *   m:一年中的某月 01-12
 *   M: 英文简写如九月 Sep
 *   
 *   Y: 年,4位数
 *   y: 年 2位数 (不带前面的0)
 * 
 *   l: 周里的某天,用英文表示,比如星期一Monday
 *   L: 周里的某天,用序号表示,比如星期一为0,星期二为1
 *   D: 星期几的简写
 *   
 *   其他字符,比如 "/", "." 或 "-" 也可被插入字符中,以增加其他格式。
 *  时间格式:
 *   h:带有首位0的12小时制的时
 *   H:24小时制
 *   i:带有首位0的份 00-59
 *   s:带有首位0的秒 00-59
 *   a:小写的午前和午后
 * 
 */</span><span>/*
 * 创建日期
 * 1.通过mktime()
 *  mktime() 返回Unix的时间戳,即为从1970年1月1日00:00:00到当期的秒数
 *  mktime(hour,minute,second,month,day,year)
 * 2.strtotime()用字符串来创建日期
 *  将任何英文文本时间解析为Unix时间戳
 *   strotime(string $time [, int $now ] );
 *   $now 为指定的时间,默认为当前时间
 *   本函数预期接受一个包含美国英语日期格式的字符串并尝试将其解析为 Unix 时间戳(自 January 1 1970 00:00:00 GMT 起的秒数),其值相对于 now 参数给出的时间,如果没有提供此参数则用系统当前时间
  strtotime的第一个参数可以是我们常见的英文时间格式,比如“2008-8-20”或“10 September 2000 ”等等。也可以是以参数now为基准的时间描述,比如“+1 day”等等。
 *    


 *  */</span><span>////  $time = mktime("12","20","1","9","10",date('y'));</span><span>// $time = strtotime('next month');</span><span>//  echo date("Y年m月d,H:i:sa",$time);</span><span>/*
 * <strong>include</strong> <strong>require</strong>
 * 服务器端包含 (SSI) 用于创建可在多个页面重复使用的函数、页眉、页脚或元素。
 * 不同:
 *  <strong>require</strong> 如果发生错误会停止当前脚本的运行,给出 E_COMPILE_ERROR
 *  <strong>include</strong> 如果发生错误会继续执行脚本的运行,给出 E_WARNING 警告
 *  因此,如果您希望继续执行,并向用户输出结果,即使包含文件已丢失,那么请使用 <strong>include</strong>。否则,在框架、CMS 或者复杂的 PHP 应用程序编程中,请始终使用 <strong>require</strong> 向执行流引用关键文件。这有助于提高应用程序的安全性和完整性,在某个关键文件意外丢失的情况下。
 *
 */</span><span>/*
 * 文件操作
 * readfile() 读入文件的内容,向输出到输出流,返回字节数 ,读取成功在则返回字节数
 * fopen(文件名,操作方式),
 */</span><span>//readfile("indexa.php");</span><span>//$file = fopen('index.php','r');</span><span>//echo fread($file,filesize('index.php'));</span><span>/*
 * <strong>异常处理</strong>
 * try{
 * }catch(<strong>Exception</strong> $ex){
 * } 
 * 自定义异常类
 */</span><span>//class My<strong>Exception</strong> extends <strong>Exception</strong>{</span><span>//     public function errorMessage(){</span><span>//         echo "错误发生在".$this->file."中的第".$this->getLine()."行<br/>";</span><span>//         echo "错误信息为:".$this->getMessage();</span><span>//     }</span><span>//}</span><span>//function Hander(){</span><span>//    throw new <strong>Exception</strong>("没有捕获的异常");</span><span>//}</span><span>//set_error_handler('Hander');</span><span>//function checkNum($num) {</span><span>//    if ($num > 1) {</span><span>//       throw  new My<strong>Exception</strong>("value must below 1");</span><span>//     }</span><span>//}</span><span>//throw new <strong>Exception</strong>('Uncaught <strong>Exception</strong> occurred');</span><span>/*
 * 过滤
 * 过滤函数
 * filter_var 通过指定一个过滤器过滤单一的变量
 * filter_var_array 通过相同或不同规定过滤器过滤多个变量
 * filter_input 获取一个输入变量并对它过滤
 * filter_input_array 获取多个变量,并通过相同或不同的过滤对他进行过滤
 * 
 */</span><span>//$i = 123;</span><span>//if(filter_var($i,FILTER_VALIDATE_INT)){</span><span>//    echo "是整数";</span><span>//}</span><span>/*
 * MySql 操作
 * 连接:mysql_connect("","","");
 * 执行:mysql_query("",$link);
 * 
 */</span><span>//$link = mysql_connect("localhost","root","root");</span><span>//if($link){</span><span>//    if(mysql_query("CREATE DATEBASE my_db",$link)){</span><span>//        echo "my_db被创建";</span><span>//    }else{</span><span>//        echo "创建失败";</span><span>//        echo mysql_error();</span><span>//    }</span><span>//}else {</span><span>//    echo '链接失败';</span><span>//}</span><span>/*
 * 面向<strong>对象</strong>
 * 1.构造方法
 *  __construct() 在php4中构造方法和类名一直,php5使用__construct,当没有__construct方法时,默认找与类名相同的方法
 * 构造函数默认为Public
 * 
 */</span><span><span>class</span><span>Person</span> {</span><span>//     var $name;//默认为public</span><span>//     var $age;</span><span>var</span><span>$name</span>;
    <span>var</span><span>$age</span>;
    <span>public</span><span><span>function</span><span>__construct</span><span>(<span>$name</span>,<span>$age</span>)</span> {</span><span>echo</span><span>"正在构造$name<br/>"</span>;
        <span>$this</span>->name = <span>$name</span>;
        <span>$this</span>->age = <span>$age</span>;
    }
    <span>public</span><span><span>function</span><span>__set</span><span>(<span>$name</span>,<span>$value</span>)</span>{</span><span>echo</span><span>"正在调用__set方法</br>"</span>;
        <span>$this</span>-><span>$name</span> = <span>$value</span>;
    }
    <span>public</span><span><span>function</span><span>__get</span><span>(<span>$name</span>)</span>{</span><span>echo</span><span>"正在调用__get方法<br/>"</span>;
        <span>if</span>(<span>isset</span>(<span>$this</span>-><span>$name</span>)){
            <span>return</span><span>$this</span>-><span>$name</span>;
        }<span>else</span>{
            <span>return</span><span>NULL</span>;
        }
    }
    <span>public</span><span><span>function</span><span>say</span><span>()</span>{</span><span>echo</span><span>$this</span>->name.<span>"在说话他的年龄是:"</span>.<span>$this</span>->age.<span>"<br/>"</span>;
    }
    <span>public</span><span><span>function</span><span>__destruct</span><span>()</span>{</span><span>echo</span><span>"再见$this->name"</span>.<span>"<br/>"</span>;
    }
}
<span>//$p1 = new Person("张三",20);</span><span>//$p2 = new Person("李四",23);</span><span>//$p3 = new Person("王五",25);</span><span>//$p2->name = "薛兴海";</span><span>//echo $p1->name."<br/>";</span><span>//$p2->say();</span><span>//class Student extends Person{</span><span>//      var $college;</span><span>//      public function __construct($name,$age,$college) {</span><span>//            parent::__construct($name, $age);</span><span>//            $this->college = $college;</span><span>//       }</span><span>//      public function say(){</span><span>////          Person::say();</span><span>//          parent::say();</span><span>//          echo "他的学校是:".$this->college,"<br/>";</span><span>//      }</span><span>//}</span><span>//$stu = new Student("张三",23,"清华大学");</span><span>//echo $stu->say();</span><span><span>class</span><span>MyClass</span> {</span><span>public</span><span>$public</span> =<span>"Public"</span>;
    <span>protected</span><span>$protected</span> =<span>'Protected'</span>;
    <span>private</span><span>$private</span> = <span>'Private'</span>;
    <span>public</span><span><span>function</span><span>printHello</span><span>()</span>{</span><span>echo</span><span>$this</span>-><span>public</span>,<span>"<br/>"</span>;
        <span>echo</span><span>$this</span>-><span>protected</span>,<span>"<br/>"</span>;
        <span>echo</span><span>$this</span>-><span>private</span>,<span>"<br/>"</span>;

    }
    <span>private</span><span><span>function</span><span>privated</span><span>()</span>{</span>    }
}
<span><span>class</span><span>Sub</span><span>extends</span><span>MyClass</span>{</span><span>public</span><span><span>function</span><span>say</span><span>()</span>{</span><span>parent</span>::<span>$this</span>->privated();        
    }

}

<span>//$s = new Sub();</span><span>//echo $s->public;</span><span>//echo $s->say();</span><span>/*
 * final 只用来定义类和方法, 不能用来定义成员属性
 */</span><span>//使用final修饰的类不能被继承</span><span><span>class</span><span>A</span>{</span><span>public</span><span>final</span><span><span>function</span><span>say</span><span>()</span>{</span>    }
}
<span>//使用final修饰的方法不能被子类覆盖</span><span>//static 静态成员方法和静态成员属性</span><span><span>class</span><span>ST</span> {</span><span>static</span><span>$a</span> = <span>3</span>;
    <span>var</span><span>$msg</span>;
    <span>const</span> CON = <span>34324</span>;
    <span>public</span><span><span>function</span><span>__construct</span><span>(<span>$msg</span>)</span> {</span><span>$this</span>->msg = <span>$msg</span>;
    }
    <span>public</span><span><span>function</span><span>say</span><span>()</span>{</span><span>echo</span><span>$this</span>->msg;
    }
    <span>public</span><span><span>function</span><span>__toString</span><span>()</span> {</span><span>RETURN</span><span>'ST'</span>;
    }
    <span>public</span><span><span>function</span><span>__clone</span><span>()</span> {</span><span>echo</span><span>"正在克隆.."</span>;
        <span>$this</span>->msg = <span>"我是假的"</span>;

    }
}
<span>//__call 当调用类的不存在的方式时,调用__call</span><span>//class Call {</span><span>//    public function __call($name,$arg){</span><span>//        echo $name,"方法不存在","参数为:",print_r($arg);</span><span>//    }</span><span>//}</span><span>//$p = new Call();</span><span>//$p->h("hah",3);</span><span>/*
 * 抽象方法
 * abstract function func();
 * 没有{},后面必须加括号
 * 只要类中有一个方法声明为抽象方法,那么此类就成为了抽象类,也必须用abatract修饰,抽象类可以包含非抽象方法,但至少有一个方法是抽象方法
 * 抽象类不能实例化,只能为子类继承,子类必须实现父类所有的抽象方法后才能被实例化,否则子类任然为抽象类
 * 
 */</span><span>abstract</span><span><span>class</span><span>Parents</span> {</span><span>const</span> CONT = <span>3</span>;
    <span>public</span><span>abstract</span><span><span>function</span><span>func1</span><span>()</span>;</span><span>abstract</span><span><span>function</span><span>func2</span><span>()</span>;</span><span>public</span><span><span>function</span><span>func3</span><span>()</span>{</span><span>echo</span><span>"Parent"</span>;
    }
}
<span><span>class</span><span>Subs</span><span>extends</span><span>Parents</span>{</span><span><span>function</span><span>func1</span><span>()</span>{</span>    }
    <span><span>function</span><span>func2</span><span>()</span>{</span>    }

}
<span>//$s = new Subs();</span><span>//$s->func3();</span><span>/*
 * 接口
 * 接口是一种特殊的抽象类,抽象类是一种特殊的类,所以,接口是一种特殊的类,如果一个抽象类的所有方法都为抽象方法,则此类就为接口
 * 接口里面不能声明变量,可以声明常量
 * 接口声明的关键字为interface ,不是class
 */</span><span><span>interface</span><span>JK</span>{</span><span>const</span> JJ =  <span>244</span>;
     <span><span>function</span><span>func1</span><span>()</span>;</span><span>//默认为public abstract </span><span><span>function</span><span>func2</span><span>()</span>;</span>
}
<span><span>class</span><span>J</span><span>implements</span><span>JK</span>{</span><span><span>function</span><span>func1</span><span>()</span>{</span>    }
    <span><span>function</span><span>func2</span><span>()</span>{</span>    }
}
<span>$t</span> = <span>new</span> J();
<span>$str</span> =serialize(<span>$t</span>);
 unserialize(<span>$str</span>);
<span><span>class</span><span>Person1</span>
{</span><span>//下面是人的成员属性</span><span>var</span><span>$name</span>; <span>//人的名子</span><span>var</span><span>$sex</span>;  <span>//人的性别</span><span>var</span><span>$age</span>;  <span>//人的年龄</span><span>//定义一个构造方法参数为属性姓名$name、性别$sex和年龄$age进行赋值</span><span><span>function</span><span>__construct</span><span>(<span>$name</span> = <span>""</span>, <span>$sex</span> = <span>""</span>, <span>$age</span> = <span>""</span>)</span> {</span><span>$this</span>->name = <span>$name</span>;
        <span>$this</span>->sex = <span>$sex</span>;
        <span>$this</span>->age = <span>$age</span>;
    }

    <span>//这个人可以说话的方法, 说出自己的属性</span><span><span>function</span><span>say</span><span>()</span> {</span><span>echo</span><span>"我的名子叫:"</span> . <span>$this</span>->name . <span>" 性别:"</span> . <span>$this</span>->sex . <span>" 我的年龄是:"</span> . <span>$this</span>->age . <span>"<br>"</span>;
    }

    <span>//指定串行化时把返回的数组中$name和$age值串行化,忽略没在数组中的属性$sex</span><span><span>function</span><span>__sleep</span><span>()</span> {</span><span>$arr</span> = <span>array</span>(<span>"name"</span>, <span>"age"</span>); <span>// 此时,属性$sex将被删除!!!</span><span>return</span>(<span>$arr</span>);
    }

    <span>//重新生成<strong>对象</strong>时,并重新赋值$age为40</span><span><span>function</span><span>__wakeup</span><span>()</span> {</span><span>$this</span>->age = <span>40</span>;
    }
}

<span>$p1</span> = <span>new</span> Person1(<span>"张三"</span>, <span>"男"</span>, <span>20</span>);

<span>//把一个<strong>对象</strong>串行化,返一个字符串,调用了__sleep()方法,忽略没在数组中的属性$sex</span><span>$p1_string</span> = serialize(<span>$p1</span>);
<span>echo</span><span>$p1_string</span> . <span>"<br>"</span>; <span>//串行化的字符串我们通常不去解析</span><span>$p2</span> = unserialize(<span>$p1_string</span>); <span>//反串行化形成<strong>对象</strong>$p2重新赋值$age为40</span><span>$p2</span>->say();
<span><span>function</span><span>__autoload</span><span>(<span>$classname</span>)</span>{</span><span><strong>require</strong></span><span>$classname</span>.<span>'.php'</span>;

}
<span>new</span> DM();</code>
Nach dem Login kopieren

Das Obige hat PHP-Lernen 00 vorgestellt, einschließlich verschiedener Aspekte. Ich hoffe, es wird für Freunde hilfreich sein, die sich für PHP-Tutorials interessieren.

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage