在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
PHP快速入门(1)——基本语法,控制结构和简单的面向对象
本文提供的所有程序已在PHP5下经过测试 。转载请注明出处。
# 快速开始
<?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", "[email protected]"); $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>"; ?>
|
2022-08-18
2022-08-15
2022-08-17
2022-11-06
2022-08-17
请发表评论