本文档介绍折言语言的基础语法和特性。
.zy- 折言脚本文件.php- PHP 兼容脚本文件
<?php
// 命名空间声明(可选)
namespace myapp;
// 代码内容
echo "Hello World";// 基本变量声明
$name = "Alice";
$age = 25;
// 类型声明
string $name = "Alice";
int $age = 25;
bool $isStudent = true;
float $height = 1.75;int- 整数类型string- 字符串类型bool- 布尔类型float- 浮点数类型
array- 数组类型object- 对象类型class- 类类型
null- 空值void- 无返回值
?string $name = null; // 可空字符串
?int $age = null; // 可空整数$a = 10;
$b = 3;
$sum = $a + $b; // 加法: 13
$diff = $a - $b; // 减法: 7
$product = $a * $b; // 乘法: 30
$quotient = $a / $b; // 除法: 3.333...
$remainder = $a % $b; // 取余: 1$a = 5;
$b = "5";
$a == $b; // 相等: true
$a === $b; // 严格相等: false
$a != $b; // 不等: false
$a !== $b; // 严格不等: true
$a < $b; // 小于
$a > $b; // 大于
$a <= $b; // 小于等于
$a >= $b; // 大于等于$a = true;
$b = false;
$a && $b; // 逻辑与: false
$a || $b; // 逻辑或: true
!$a; // 逻辑非: false$age = 18;
$status = $age >= 18 ? "成年" : "未成年";$name = $userName ?? "匿名用户";
$config = $userConfig ?? $defaultConfig;$a = 10;
$a += 5; // $a = 15
$a -= 3; // $a = 12
$a *= 2; // $a = 24
$a /= 4; // $a = 6
$a %= 4; // $a = 2// if 语句
if ($score >= 90) {
echo "优秀";
} elseif ($score >= 80) {
echo "良好";
} else {
echo "需要努力";
}
// 简写形式
if ($isValid) echo "有效";// 传统 for 循环
for (int $i = 0; $i < 10; $i++) {
echo "Count: {$i}\n";
}
// for...in 循环
array $items = ["apple", "banana", "orange"];
for ($item in $items) {
echo "Item: {$item}\n";
}
// 无限循环
for (;;) {
// 循环体
if ($condition) break;
}int $count = 0;
while ($count < 5) {
echo "Count: {$count}\n";
$count++;
}array $fruits = ["apple", "banana", "orange"];
foreach ($fruits as $fruit) {
echo "I like {$fruit}\n";
}
// 带键的 foreach
array $person = ["name" => "Alice", "age" => 25];
foreach ($person as $key => $value) {
echo "{$key}: {$value}\n";
}switch ($status) {
case 200:
echo "OK";
break;
case 404:
echo "Not Found";
break;
default:
echo "Unknown";
break;
}
// 不带括号的 switch
switch $day {
case "Monday":
echo "Start of week";
break;
case "Friday":
echo "End of week";
break;
default:
echo "Mid week";
break;
}$result = match ($value) {
0 => "zero",
1 => "one",
2 => "two",
default => "many"
};// break - 跳出循环
for (int $i = 0; $i < 10; $i++) {
if ($i == 5) break;
echo "{$i}\n";
}
// continue - 跳过当前迭代
for (int $i = 0; $i < 10; $i++) {
if ($i % 2 == 0) continue;
echo "{$i}\n";
}
// return - 返回函数值
function getValue(): int {
return 42;
}// 基本函数
function greet(string $name): string {
return "Hello, {$name}!";
}
// 带默认值的函数
function greet(string $name = "World"): string {
return "Hello, {$name}!";
}
// 无返回值的函数
function logMessage(string $message): void {
echo "[LOG] {$message}\n";
}
// 可变参数函数
function sum(...$numbers): int {
$total = 0;
foreach ($numbers as $num) {
$total += $num;
}
return $total;
}// 基本调用
$message = greet("Alice");
// 命名参数调用
$result = calculate(a: 10, b: 20, operation: "add");
// 可变参数调用
$total = sum(1, 2, 3, 4, 5);class Person {
// 属性
private string $name;
private int $age;
// 构造函数
public function __construct(string $name, int $age) {
$this->name = $name;
$this->age = $age;
}
// 方法
public function introduce(): string {
return "I'm {$this->name}, {$this->age} years old.";
}
// Getter
public function getName(): string {
return $this->name;
}
// Setter
public function setAge(int $age): void {
$this->age = $age;
}
}// 创建对象
$person = new Person("Alice", 25);
// 调用方法
echo $person->introduce();
// 访问属性
$name = $person->getName();class Student extends Person {
private string $school;
public function __construct(string $name, int $age, string $school) {
parent::__construct($name, $age);
$this->school = $school;
}
public function getSchool(): string {
return $this->school;
}
}interface Animal {
public function cry(): string;
}
class Dog implements Animal {
public function cry(): string {
return "汪汪";
}
}$dog = new Dog();
// instanceof 检查
if ($dog instanceof Animal) {
echo "Dog implements Animal";
}
// like 关键字(鸭子类型)
if ($dog like Animal) {
echo "Dog has Animal-like structure";
}try {
$result = riskyOperation();
echo "Success: {$result}";
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
} finally {
cleanup();
}function divide(int $a, int $b): float {
if ($b == 0) {
throw new Exception("Division by zero");
}
return $a / $b;
}$single = 'Hello World';
$double = "Hello World";
$heredoc = <<<EOT
多行
字符串
EOT;$name = "Alice";
$message = "Hello, {$name}!";
$result = "Result: @{calculate(10, 20)}";$text = "Hello World";
$text->length(); // 获取长度
$text->toUpperCase(); // 转大写
$text->toLowerCase(); // 转小写
$text->trim(); // 去除空白
$text->indexOf("World"); // 查找子串
$text->substring(0, 5); // 截取子串
$text->replace("World", "Origami"); // 替换
$text->split(" "); // 分割// 索引数组
array $numbers = [1, 2, 3, 4, 5];
// 关联数组
array $person = [
"name" => "Alice",
"age" => 25
];
// 混合数组
array $mixed = [1, "hello", true, ["nested"]];$arr = [1, 2, 3, 4, 5];
// 基础操作
$arr->push(6); // 添加元素
$arr->pop(); // 移除最后一个
$arr->shift(); // 移除第一个
$arr->unshift(0); // 添加第一个
// 查找
$arr->indexOf(3); // 查找索引
$arr->includes(3); // 检查包含
$arr->find(function($n) { return $n > 3; }); // 查找元素
// 迭代
$arr->forEach(function($item) { echo $item; });
$doubled = $arr->map(function($n) { return $n * 2; });
$evens = $arr->filter(function($n) { return $n % 2 == 0; });
$sum = $arr->reduce(function($acc, $n) { return $acc + $n; }, 0);namespace myapp\utils;
function helper() {
return "Helper function";
}namespace myapp;
use myapp\utils\helper;
$result = helper();// 这是单行注释
$value = 42; // 行尾注释/*
这是多行注释
可以跨越多行
*/$title = "Hello World";
$html = <div class="container">
<h1>{$title}</h1>
<p>This is embedded HTML</p>
</div>;
echo $html;$name = "Alice";
$template = "Hello ${name}, welcome to Origami!";// 使用有意义的变量名
string $userName = "Alice";
int $userAge = 25;
// 使用类型声明
function calculateArea(float $width, float $height): float {
return $width * $height;
}
// 使用异常处理
try {
$result = riskyOperation();
} catch (Exception $e) {
Log::error("Operation failed: " . $e->getMessage());
}// 避免在循环中重复计算
int $length = $array->length();
for (int $i = 0; $i < $length; $i++) {
// 循环体
}
// 使用适当的数据结构
array $map = ["key1" => "value1", "key2" => "value2"];