PHP HelloWorld
·
// PHP - Server-side scripting language
/*
PHP (PHP: Hypertext Preprocessor) is a general-purpose scripting
language especially suited to web development. Originally created by
Rasmus Lerdorf in 1994, PHP is now maintained by The PHP Group. It
can be embedded into HTML and is especially suited for web development.
Key Features:
- Server-side scripting
- Cross-platform compatibility
- Compatible with many databases
- Easy to learn
- Large community
- Extensive library support
- Flexible and scalable
*/
<?php
// Variables
$language = "PHP";
$version = 8.2;
$isServerSide = true;
echo "Welcome to $language!\n";
// Data types
$number = 42;
$decimal = 3.14159;
$isInterpreted = true;
$character = 'P';
// Control structures
if ($isServerSide) {
echo "$language is server-side scripting language\n";
}
// Arrays
$features = ["Web Development", "Server-side", "Database Integration", "Flexible"];
echo "Key Features:\n";
foreach ($features as $index => $feature) {
echo ($index+1) . ". $feature\n";
}
// Associative arrays (dictionaries)
$languageCreators = [
"PHP" => "Rasmus Lerdorf",
"C" => "Dennis Ritchie",
"Java" => "Sun Microsystems"
];
echo "Language Creators:\n";
foreach ($languageCreators as $lang => $creator) {
echo "$lang: $creator\n";
}
// Functions
function greet($name) {
return "Hello, $name!";
}
function add($a, $b) {
return $a + $b;
}
echo greet("Programmer") . "\n";
echo "5 + 3 = " . add(5, 3) . "\n";
// Classes
class Animal {
protected $name;
public function __construct($name) {
$this->name = $name;
}
public function speak() {
echo $this->name . " makes a sound\n";
}
}
class Dog extends Animal {
public function speak() {
echo $this->name . " says Woof!\n";
}
}
$dog = new Dog("Buddy");
$dog->speak();
// Interfaces
interface Shape {
public function area();
}
class Circle implements Shape {
private $radius;
public function __construct($radius) {
$this->radius = $radius;
}
public function area() {
return pi() * $this->radius * $this->radius;
}
}
$circle = new Circle(5);
echo "Circle area: " . $circle->area() . "\n";
// Exception handling
try {
$result = 10 / 0;
} catch (DivisionByZeroError $e) {
echo "Cannot divide by zero!\n";
}
// SuperGlobals
echo "PHP version: " . PHP_VERSION . "\n";
// String functions
$text = "Hello World";
echo "Length: " . strlen($text) . "\n";
echo "Uppercase: " . strtoupper($text) . "\n";
// File operations
$file = fopen("sample.txt", "w");
fwrite($file, "This is a sample file created with PHP");
fclose($file);
echo "PHP demo completed!\n";
?>
更多推荐


所有评论(0)