Top 1,000 Features Creators Resources Blog Explore Download
GitHub icon

PHP

< >

PHP, aka Personal Home Page, is a programming language created in 1995 by Rasmus Lerdorf.

Source code:
git clone https://github.com/php/php-src
#5on PLDB 29Years Old 3mRepos

Try now: Riju ยท TIO ยท Replit

PHP is a server-side scripting language designed primarily for web development but also used as a general-purpose programming language. Originally created by Rasmus Lerdorf in 1994, the PHP reference implementation is now produced by The PHP Development Team. PHP originally stood for Personal Home Page, but it now stands for the recursive acronym PHP: Hypertext Preprocessor. Read more on Wikipedia...


Example from Riju:
<?php echo "Hello, world!\n";
Example from hello-world:
<?php echo 'Hello World';
<?php // Hello world in PHP echo 'Hello World!'; ?>
Example from Linguist:
#!/usr/bin/php <?php echo "PHP\n";
Example from Wikipedia:
class Person { public $firstName; public $lastName; public function __construct(string $firstName, string $lastName = '') { // optional second argument $this->firstName = $firstName; $this->lastName = $lastName; } public function greet(): string { return 'Hello, my name is ' . $this->firstName . (($this->lastName != '') ? (' ' . $this->lastName) : '') . '.'; } public static function staticGreet(string $firstName, string $lastName) { return 'Hello, my name is ' . $firstName . ' ' . $lastName . '.'; } } $he = new Person('John', 'Smith'); $she = new Person('Sally', 'Davis'); $other = new Person('iAmine'); echo $he->greet(); // prints "Hello, my name is John Smith." echo '<br />'; echo $she->greet(); // prints "Hello, my name is Sally Davis." echo '<br />'; echo $other->greet(); // prints "Hello, my name is iAmine." echo '<br />'; echo Person::staticGreet('Jane', 'Doe'); // prints "Hello, my name is Jane Doe."
__CLASS__ __DIR__ __FILE__ __FUNCTION__ __halt_compiler() __LINE__ __METHOD__ __NAMESPACE__ __TRAIT__ abstract and array() as break callable case catch class clone const continue declare default die() do echo else elseif empty() enddeclare endfor endforeach endif endswitch endwhile eval() exit() extends final finally for foreach function global goto if implements include include_once instanceof insteadof interface isset() list() namespace new or print private protected public require require_once return static switch throw trait try unset() use var while xor yield

Language features

Feature Supported Token Example
Conditionals โœ“
Inheritance โœ“
Access Modifiers โœ“
Switch Statements โœ“
Exceptions โœ“
Constants โœ“
Classes โœ“
While Loops โœ“
MultiLine Comments โœ“ /* */
/* A comment
*/
Print() Debugging โœ“ echo
Line Comments โœ“ //
// A comment
Increment and decrement operators โœ“
Functions โœ“
Variadic Functions โœ“
function sum(...$nums)
{
    return array_sum($nums);
}
echo sum(1, 2, 3); // 6
Traits โœ“
// The template
trait TSingleton
{
    private static $_instance = null; 
    private function __construct(){} // Must have private default constructor and be aware not to open it in the class
    public static function getInstance()
    {
        if (null === self::$_instance)
        {
            self::$_instance = new self();
        }
        return self::$_instance;
    }
}
class FrontController
{
    use TSingleton;
}
// Can also be used in already extended classes
class WebSite extends SomeClass
{
    use TSingleton;
}
Operator Overloading โœ“
Multiline Strings โœ“
$xml = <<
File Imports โœ“
Assignment โœ“
$name = "John"
Ternary operators โœ“
Here Document โœ“
Constructors โœ“
Comments โœ“
Magic Getters and Setters โœ“
    public function __set($name, $value)
   {
       echo "Setting '$name' to '$value'\n";
       $this->data[$name] = $value;
   }

   public function __get($name)
   {
       echo "Getting '$name'\n";
       if (array_key_exists($name, $this->data)) {
           return $this->data[$name];
       }

       $trace = debug_backtrace();
       trigger_error(
           'Undefined property via __get(): ' . $name .
           ' in ' . $trace[0]['file'] .
           ' on line ' . $trace[0]['line'],
           E_USER_NOTICE);
       return null;
   }
Strings โœ“ '
"hello world"
Booleans โœ“
Case Insensitive Identifiers X
Semantic Indentation X

View source

- Build the next great programming language ยท About ยท Acknowledgements ยท Extensions ยท Day 622 ยท feedback@pldb.io