php快速入门

本文将为您介绍PHP的基础知识,包括快速开始、注释、链接字符串、条件判断、分支结构、三元运算、数组、循环、函数和递归、简单面向对象等内容,帮助您快速掌握PHP编程。

# 快速开始

<?php
    echo "<p>Hello World </p>";
?>
# 注释

<?php
    // 介是单行注释
    /* 介是多行注释... */
    echo "<p>This is something about php </p>";
?>
# 链接字符串

<?php
    $str1 = "You will see: ";
    $str2 = "How to concat strings.";
    echo "<p>".$str1.$str2."</p>";
?>
# 条件判断

<?php
    $flag = true;
    if($flag)
    {
        echo "<p>Under the condition...</p>";
    }
    else
    {
        echo "<p>Under other conditions...</p>";
    }

    $val = 100;
    if($val == 0)
    {
        echo "<p>It is zero.</p>";
    }
    elseif($val < 0)
    {
        echo "<p>It is a negative.</p>";
    }
    else
    {
        echo "<p>It is a positive.</p>";
    }
?>
# 分支结构

<?php
    $case = 10;
    $result = "";
    switch($case)
    {
        case 1:
            $result = "In case one.";
            break;
        case 2:
            $result = "In case two.";
            break;
        case 3:
            $result = "In case three.";
            break;
        default:
            $result = "In other case.";
            break;
    }
    echo "<p>".$result."</p>";
?>
# 三元运算

<?php
    $val = 1;
    $exp = ($val == 0) ? "<p>The value is zero</p>" : "<p>The value is not zero</p>";
    echo $exp;
?>
# 数组array

<?php
    echo "<ul>";
    $arr1 = array("John", "Jane", "Julie");
    echo "<li>People in array #1: $arr1[0], $arr1[1], $arr1[2]</li>";

    $arr2 = array(0 => "Jodan", 1 => "Jack", 2 => "James");
    echo "<li>People in array #2: $arr2[0], $arr2[1], $arr2[2]</li>";

    $arr3 = array(1 => "Tom", "Sam", "Kim");
    echo "<li>People in array #3: $arr3[1], $arr3[2], $arr3[3]</li>";
   
    $arr4[3] = "Avril";
    $arr4[2] = "Taylor";
    $arr4[1] = "Miley";
    echo "<li>People in array #4: $arr4[1], $arr4[2], $arr4[3]</li>";
    echo "</ul>";
?>
# for循环和foreach

<?php
    $fib[0] = 1;
    $fib[1] = 1;
    for($i = 2; $i < 10; $i++)
    {
        $fib[$i] = $fib[$i-1] + $fib[$i-2];       
    }

    echo "<p>The Fibonacci: ";
    foreach($fib as $index => $value)
    {
        echo (string)$value." ";
    }
    echo "</p>";
?>
# while循环

<?php
    $n = 6;
    $c = 1;
    $r = 1;
    while($c <= $n)
    {
        $r *= $c;
        $c += 1;
    }
    echo "<p>$n != $r </p>";
?>
# 函数和递归

<?php
    function factorial($val)
    {
        if($val == 0) return 1;
        return $val * factorial($val - 1);
    }
    $n = 7;
    echo "<p>$n! = ".(string)factorial($n)."</p>";
?>
# 简单的面向对象

<?php
    class User
    {
        // fields
        private $name;
        private $email;
        private $status;

        // constructor
        function User($name, $email)
        {
            $this->name = $name;
            $this->email = $email;
            $this->status = "";
        }
       
        // getters
        function getName() { return $this->name; }
        function getEmail() { return $this->email; }
        function getStatus() { return $this->status; }
       
        // setters
        function setStatus($value) { $this->status = $value; }

        // methods
        function SignOut()
        {
            echo "User $this->name signed out.";
        }
    }

    $u = new User("John", "johnsmith@gmail.com");
    $u->setStatus("online");
    echo "<p>";
    echo "Name: ".$u->getName()."<br />";
    echo "Email: ".$u->getEmail()."<br />";
    echo "Status: ".$u->getStatus()."<br />";
    echo "</p>";
    echo "<p>".$u->SignOut()."</p>";
?>
# 更简单的Getter和Setter

<?php
    class Rectangle
    {
        // fields
        private $offsetX;
        private $offsetY;
        private $width;
        private $height;

        // constructor & destructor
        function __construct($x, $y)
        {
            $this->offsetX = $x;
            $this->offsetY = $y;
        }

        function __destruct()
        {
            unset($this->offsetX);
            unset($this->offsetY);
            unset($this->width);
            unset($this->height);
        }
       
        // getters
        function __get($property)
        {
            if(isset($this->$property)) { return($this->$property); }
            else return(NULL);
        }
       
        // setters
        function __set($property, $value) { $this->$property = $value; }
       

        // methods
        function GetArea()
        {
            return $this->width * $this->height;
        }
    }

    $rect = new Rectangle(10, 20);
    $rect->width = 48;
    $rect->height = 72;
    echo "<p>";
    echo "Offset X: ".$rect->offsetX."<br />";
    echo "Offset Y: ".$rect->offsetY."<br />";
    echo "Width: ".$rect->width."<br />";
    echo "Height: ".$rect->height."<br />";
    echo "Area: ".$rect->GetArea();
    echo "</p>";   
?>
转载自http://www.cnblogs.com/tracydj/archive/2010/10/19/1855481.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值