example
example

Php Php 语法

  • 主题发起人 主题发起人 Yann
  • 开始时间 开始时间

Yann

站长
管理成员
GTAOS管理组
认证卖家
认证用户
黄金
192.55G
1.变量声明和使用:
PHP:
$name = "John";
$age = 25;
echo "My name is $name and I am $age years old.";

2.条件语句(if-else):
PHP:
$score = 85;
if ($score >= 60) {
echo "You passed the exam!";
} else {
echo "You failed the exam.";
}

3.循环语句(for):
PHP:
for ($i = 1; $i <= 5; $i++) {
echo "Number: $i<br>";
}

4.数组声明和访问:
PHP:
$fruits = array("apple", "banana", "orange");
echo $fruits[0]; // 输出 "apple"

5.函数声明和调用:
PHP:
function greet($name) {
echo "Hello, $name!";
}
greet("Alice"); // 输出 "Hello, Alice!"

6.类声明和实例化:
PHP:
class Person {
private $name;

public function __construct($name) {
$this->name = $name;
    }

public function greet() {
echo "Hello, my name is $this->name!";
    }
}

$person = new Person("John");
$person->greet(); // 输出 "Hello, my name is John!"

7.文件包含:
PHP:
// 文件1.php
$variable = "Hello from file 1!";

// 文件2.php
include '1.php';
echo $variable; // 输出 "Hello from file 1!"

8.异常处理:
PHP:
function divide($numerator, $denominator) {
if ($denominator == 0) {
throw new Exception("Division by zero error!");
    }
return $numerator / $denominator;
}

try {
echo divide(10, 0);
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
 
最后编辑:
后退
顶部