GNU/Linux Programmer's Solver - PHP

Hello World program in PHP. Basic structure, comments and string literals
PROGRAMMING PHP HELLO WORLD
#!/usr/bin/php -f
<?php
/* This is a multiline
   comment */
echo "Hello World\n";

echo <<<EOM
PHP supports HEREDOCS and
produce multiline strings 
easily
EOM;
?>

PHP is a classic language that originated in the world of web applications, but today it can be used as a general-purpose scripting language.

Programs in PHP usually use the .php extension.

PHP takes the best of C and Perl and combines them with a lot of sense… In other words, if you know how to program in C or Perl, then you can be programming in PHP in a few hours. As we can see in the code above, PHP defines comments the same as C, and allows us to use HEREDOCS to work with multi-line texts.

The echo statement does not add a carriage return at the end of the string. If we want it to do so, we must add the special character \n to our string.

DID YOU KNOW

PHP gets its name from the recursive acronym PHP: Hypertext Processor in the classic hacker style of old times.

We can execute this program in the usual way for scripting languages (have you noticed the Shebang! in the first line?).

  $ php -f hello.php
  $ chmod +x hello.php
  $ ./hello.php

PHP was originally developed to write Active Web Pages on the server side. In this sense, the interpreter works like other active page engines… what’s between <?php ... ?> is interpreted as language instructions and everything else is simply written to the console… or in the case of web applications, sent to the client. Keeping this in mind, our “Hello World” program can be rewritten in this way:

#!/usr/bin/php -f
<?php
/* This is a multiline
   comment */
echo "Hello World\n";
?>
PHP supports HEREDOCS and
produce multiline strings 
easily

DID YOU KNOW

PHP, along with Visual Basic (ASP Active Server Pages) and Java (JSP Java Server Pages), were and continue to be the main technologies for generating web pages on the server side.

PHP’s development began in 1994 by Rasmus Lerdorf who wrote a series of small C programs used through the CGI (Common Gateway Interface) interface with which dynamic web content could be generated. Lerdorf extended these programs to work with forms and connect to databases and called this implementation Personal Home Page/Forms Interpreter or PHP/FI.

Lerforf announced the system in the comp.infosystems.www.authroring.cgi usenet group as Personal Home Page Tools (PHP tools) Version 1.0 in order to improve the project. The project began to grow from there, and version 2.0 was released in 1997.

After that, Zeev Suraski and Andi Gutmans rewrote the parser, which led to PHP 3. It is in this version that the language is renamed PHP: Hypertext preprocessor. A few years later the core of the system was rewritten and in 1999 Zend Engine was launched and so on to this day.

I’M A NEWBIE

Zend engine is the compiler and runtime environment of PHP. In a way, we can see Zend Engine as the Java SDK that includes a compiler that generates machine code (bytecodes) that a virtual machine is then able to execute.

In 2000 PHP4 was released along with Zend Engine 1.0. The language has continued to evolve from this point to this day. As of writing this text, we are on version 7.4.

Summary

  • Interpreted language and compilable to bytecode
  • Extension: php
  • Line terminator: ;
  • Groups instructions with: { instructions }
  • Multi-line comments: /* comment */
  • Same-line comments: //
  • String delimiter: "string"
  • You can print text with echo (Does not add a carriage return)
  • Support for multi-line strings: Supported natively
  • Supports HERE-DOCS
  • Entry point: main function

Related Posts

GNU/Linux Programmer's Solver
Chapter 1 - Hello World

Hello World is the first program you write when learning a new language and there are good reasons for that

Read

PROGRAMMING HELLO WORLD

GNU/Linux Programmer's Solver - Kotlin

Hello World program in Kotlin. Basic structure, comments and string literals

Read

PROGRAMMING KOTLIN HELLO WORLD

GNU/Linux Programmer's Solver - JavaScript

Hello World program in JavaScript. Basic structure, comments and string literals

Read

PROGRAMMING JAVASCRIPT ECMASCRIPT HELLO WORLD

Return to Home Page