本指南将帮助您在 5 分钟内创建并运行第一个折言程序。
创建一个名为 hello.zy 的文件:
<?php
// 第一个折言程序
echo "Hello, Origami!";
// 变量声明
string $name = "World";
echo "Hello, {$name}!";
// 简单函数
function greet(string $name): string {
return "Hello, " . $name . "!";
}
$message = greet("Origami");
echo $message;./origami hello.zy输出:
Hello, Origami!
Hello, World!
Hello, Origami!
<?php
// 基本类型
int $age = 25;
string $name = "Alice";
bool $isStudent = true;
float $height = 1.75;
// 数组
array $colors = ["red", "green", "blue"];
array $person = [
"name" => "Bob",
"age" => 30
];
// 输出
echo "Name: {$name}, Age: {$age}";
echo "Colors: " + $colors->join(", ");<?php
// 条件语句
int $score = 85;
if ($score >= 90) {
echo "优秀";
} elseif ($score >= 80) {
echo "良好";
} else {
echo "需要努力";
}
// 循环
for (int $i = 1; $i <= 5; $i++) {
echo "Count: {$i}\n";
}
// foreach 循环
array $fruits = ["apple", "banana", "orange"];
foreach ($fruits as $fruit) {
echo "I like {$fruit}\n";
}<?php
// 基本函数
function add(int $a, int $b): int {
return $a + $b;
}
// 带默认值的函数
function greet(string $name = "World"): string {
return "Hello, {$name}!";
}
// 函数调用
$result = add(10, 20);
echo "Sum: " + $result + "\n";
$message = greet("Alice");
echo $message;<?php
// 类定义
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.";
}
public function getAge(): int {
return $this->age;
}
}
// 创建对象
$person = new Person("Alice", 25);
echo $person->introduce();<?php
string $text = "Hello World";
// 字符串方法
echo "Length: " + $text->length() + "\n";
echo "Uppercase: " + $text->toUpperCase() + "\n";
echo "Lowercase: " + $text->toLowerCase() + "\n";
echo "Contains 'World': " + ($text->indexOf("World") >= 0 ? "Yes" : "No") + "\n";<?php
array $numbers = [1, 2, 3, 4, 5];
// 数组方法
echo "Original: " + $numbers->join(", ") + "\n";
$doubled = $numbers->map(function($n) {
return $n * 2;
});
echo "Doubled: " + $doubled->join(", ") + "\n";
$evens = $numbers->filter(function($n) {
return $n % 2 == 0;
});
echo "Evens: " + $evens->join(", ") + "\n";<?php
function calculate(string $operation, float $a, float $b): float {
switch ($operation) {
case "add":
return $a + $b;
case "subtract":
return $a - $b;
case "multiply":
return $a * $b;
case "divide":
if ($b == 0) {
throw new Exception("Division by zero");
}
return $a / $b;
default:
throw new Exception("Unknown operation");
}
}
// 测试计算器
try {
echo "10 + 5 = " + calculate("add", 10, 5) + "\n";
echo "10 - 5 = " + calculate("subtract", 10, 5) + "\n";
echo "10 * 5 = " + calculate("multiply", 10, 5) + "\n";
echo "10 / 5 = " + calculate("divide", 10, 5) + "\n";
} catch (Exception $e) {
echo "Error: " + $e->getMessage() + "\n";
}<?php
// 创建简单的日志系统
function logMessage(string $level, string $message): void {
$timestamp = date("Y-m-d H:i:s");
$logEntry = "[{$timestamp}] [{$level}] {$message}\n";
echo $logEntry;
}
logMessage("INFO", "Application started");
logMessage("WARN", "This is a warning message");
logMessage("ERROR", "Something went wrong");验证语言功能:
./origami tests/run_tests.zy现在您已经了解了基础用法,建议:
A: 使用 Log::debug() 或 echo 输出调试信息。
A: 支持 .zy 和 .php 文件。
A: 运行 ./origami 查看命令行帮助。
A: 在 GitHub 上提交 Issue 或加入讨论群。