Questions Concepts
GitHub icon

PHP

PHP - Programming language

< >

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
#9on PLDB 28Years 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 · Search · Add Language · Features · Creators · Resources · About · Blog · Acknowledgements · Queries · Stats · Sponsor · Traffic · Traffic Today · Day 305 · feedback@pldb.com · Logout