php学习【2】

 

1:运算符

 

<?php
$x=1;
echo 1+1;//算术运算符
echo $x+=5;//赋值运算符
echo "<br/>";
echo $x++;
echo $x++;
echo ++$x;//自增运算符,--:自减
var_dump ($x>1);//比较运算符
echo $x>1 or $x<1 ;//逻辑运算符
$x=array("name"=>"zs","sex"=>"man");
$y=array("name"=>"ls","sex"=>"woman");
#$z=$x+$y;
var_dump($x <> $y);//数组运算符
$q="hellow world";
$z=$q ?: 'none';echo $z,PHP_EOL;//三元运算符
?>

 

2:控制语句

 

if,else if,else

 

<?php
$x=date("H");
if ($x>6 and $x<8){
    echo "you have get up";
}
else if($x>9 and $x<12){
    echo "you have work";
}
else{
    echo "you have game";
}
?>

switch

 

<?php
$ageaArry=array(19,20,1,25);
$age=19;
switch ($age) {
    case $ageaArry[0]:
    echo "you age is 19";
    break;
    case $ageaArry[1]:
    echo "you age is 20";
    break;
    case $ageaArry[2]:
    echo "you age is 1";
    default:
    echo "you age is 25";
}

?>

 

3:数组排序

 

<?php
$ageaArry
=array(19,20,1,25); echo count($ageaArry); //sort($ageaArry);//升序 rsort($ageaArry);//降序 for($x=0;$x<count($ageaArry);$x++){ echo $ageaArry[$x]; } $money=array("beat"=>20,"wine"=>100,"mac"=>6); asort($money);//根据键升序 ksort($money);//根据键升序 arsort($money);//根据值降序 krsort($money);//根据键降序 foreach($money as $x=>$x_value){ echo $x_value; echo $x; }
?>

 

 

 4:超级全局变量


<form method="POST"action="<?php echo $_SERVER['PHP_SELF'];?>">//$_SERVER为php的内置函数,用于返回一些列信息 <input type="text"name="names"> <input type="submit"> </form> <?php <!--$name=$_REQUEST["names"];//$_REQUEST和$_POST都用于搜集表单信息 --> $name=$_POST["names"]; echo $name; ?>

 

 5:for循环

$sum=0;
for($x=0;$x<=100;$x++){
     $sum=$sum+$x;
     $x++;
     echo $sum;
}

 

 

6while和do ...while循环

do{
    $x=1;    
    $x++;
    echo $x;    
    echo"<br/>";
}while(true);

$x=1;
while (true){
    $x++;
    echo $x;    
    echo"<br/>";
}

 

7:函数

<?php
echo "下面求$x和$y的和".myFuction(1,9);
function myFuction($x,$y){
    echo $x+$y;
}
?>

 

8:魔术常量

<?php
/*//echo "下面求$x和$y的和".myFuction(1,9);
class myClass{
function myFuction($x,$y){
echo __LINE__.__FILE__.__DIR__;//返回当前所在的行数,所在的位置
echo $x+$y;
echo __FUNCTION__;//当前的函数名
echo __CLASS__;//类名
echo __METHOD__;
}
}
#myFuction(1,2);
$x=new myClass();
$x->myFuction(1,2);
*/
?>

 

9:面向对象

class myClass{
    var $url;//类变量   
    var $auth;
    /*function  setUrl ($par){
        $this->url=$par;

    }*/
    function __construct($par1,$par2)//类方法,构造函数
    {
        $this->url=$par1;
        $this->auth=$par2;
    }
    function getUrl(){
        echo $this->url.PHP_EOL;
    }
    /*function setAuth($par){
        $this->auth=$par;
    }*/
    function getAuth(){
        echo $this->auth.PHP_EOL;
    }
}

$x=new myClass("www.xiaomo.com","lp");
#$x->setUrl("www.xiaomohk.cn");
$x->getUrl();
//echo "hellow".PHP_EOL."world";
/*
declare(encoding='UTF-8');
namespace project;
echo __NAMESPACE__;
*/
?>






<?php
class myClassTow{
function __construct(){                          //析构函数
$this->name="hellow world";
}
function __destruct(){
print $this->name;
}
}
$obj=new myClassTow();
?>
 
 
 
 
 
 
 
 
class myChildClass extends myClass{      //继承类

var $number;
function setNumber($par){
$this->number=$par;
}
 
 
 
 
 
 
 
 
 
 
 
class myClass{                               //类属性和方法访问控制的
public $public="public";//公有,全部能访问
protected $protected="protected";//保护,只有子父类可以访问
private $private="private";//私有,只有定义的类可以访问
private function myFunction(){
echo $this->public;
echo $this->protected;
echo $this->private;
}
}
$obj=new myClass();
echo $obj->public;
//echo $obj->private;
echo $obj->myFunction();
?>
 
 
 
<?php 
interface iTemple{                           //接口
public function setInfomation($name,$sex);
public function getInfomation($myClass);
}
class myClass implements iTemple{    //在类中调用接口必须要使用的方法
private $vars=array();
public function setInfomation($name,$sex){
$this->value[$name]=$sex;
}
public function getInfomation($myClass){
foreach($this->vars as $name=>$value){
$myClass=str_replace('{'.$name.'}',$value,$myClass);
}
return $myClass;
}
}
?>
 
 
class myClassTow{            // 在类中使用常量
const constant='常量';
function myFunction(){
echo self::constant.PHP_EOL;
}
}
echo myClassTow::constant;
?>
 
 
class myClassTow{
public static $age=18;   //static声明关键字,类中方法和属性的特殊调用
const constant='常量';
function myFunction(){
echo self::constant.PHP_EOL;
}
}
echo myClassTow::constant;
echo myClassTow::$age;

 

posted on 2018-08-12 11:36  activecode  阅读(252)  评论(0编辑  收藏  举报

导航