PHP Notes 1
What is PHP?
PHP (recursive acronym for "PHP: Hypertext Preprocessor") is a widely-used Open Source general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.
How PHP came into being
PHP started as a quick Perl hack written by Rasmus Lerdorf in late 1994. Over the next two to three years, it evolved into what we today know as PHP/FI 2.0. PHP/FI started to get a lot of users, but things didn't start flying until Zeev Suraski and Andi Gutmans suddenly came along with a new parser in the summer of 1997, leading to PHP 3.0. PHP 3.0 defined the syntax and semantics used in both versions 3 and 4.
Why yet another language?
People often ask "why invent yet another language; don't we have enough of them out there"? It is simply a matter of "the right tool for the right job". Many Web developers found that existing tools and languages were not ideal for the specific task of embedding code in markup. Those developers collaborated to develop a server-side scripting language which they felt would be ideal for developing dynamic Web-based sites and applications.
PHP was created with these particular needs in mind. Moreover, PHP code was developed for embedment within HTML. In doing so, it was hoped that benefits such as quicker response time, improved security, and transparency to the end user would be achieved. PHP has evolved into a language, or maybe even an
environment, that has a very specific range of tasks in mind. For this, PHP is, in Stig's humble opinion, pretty close to the ideal tool.
For More Information
The following resources will further your knowledge of PHP:
The PHP Manual: http://www.php.net/manual/en/
The PHP FAQ: http://tr2.php.net/faq
PHP Notes 1 Lakshman Gupta 8:57 AM 10/8/2019
PHP Tutorial
Language Syntax
Most of PHP's syntax is borrowed from C, although there are elements borrowed from Perl, C++ and Java as
well. This article assumes that you are familiar with C's syntax. However, don't panic if you're not.
Embedding PHP Code
To give you an idea of what embedding PHP would entail, consider the following three "hello world" examples,
all of which will give the exact same output:
Example 1: HTML alone
Hello, World!
Example 2: PHP code alone
<?php print "Hello, World!"; ?>
Example 3: PHP embedded within HTML
<?php print "Hello,"; ?> World!
Web servers supporting PHP will, by default, scan a file in HTML mode. HTML code will be passed over to the
browser as usual, up until the server happens upon a PHP line of code. In examples 2 and 3 above, the "<?php" tag informs the server that PHP code is to follow. The server then switches over to PHP mode in
anticipation of a PHP command. The "?>" tag closes out the PHP mode with the server resuming its scanning in HTML mode once more.
Embedding code in this manner is, conceptually, a more fluid approach to designing a Web page because you are working within the output setting, namely an HTML page. Traditionally, you had to fragment the output (i.e. the header, body, footer etc..) and then put it into the code. Now we are inserting the code directly into the output.
"So, what's the difference?" or "Why add extra code when HTML alone would do the trick?".
Dynamic vs. Static Web pages
The "Hello, World" example we chose would certainly not require you to use PHP. That's because it is static, meaning its display will always remain the same. But what if you wanted to greet the world in any number of
ways? Say, for example, "Bonjour, World!", or "Yo, World!" and so on.
Since HTML tags are purely descriptive they cannot function as a variable. Nor can they convey even the simplest of uncertainty such as a "Bonjour" or a "Yo". You need a command language to handle variability in a
Web page. Based on either a conditional statement or direct user input, a command language can generate the
"static" HTML necessary to correctly display a Web page's content.
Let us reconsider example #3. This time we want to let the user decide how to greet the world:
Example 4: PHP embedded within HTML revisited!
<?php print $greeting, ", "; ?> World!
From the above example, $greeting is assigned a value, and together with the comma and the word "World!",
this value is sent to the browser.
Dynamic Web page design, however, is more than just about inserting variables. What if you wanted not only to
greet the world in French, but also to present the page using the colors of the French flag?
Both a Web page's structure as well as its content can be customized. This means dynamic Web page
programming can also entail on-demand Web page building. No static, here!
Variables In PHP, a variable does not require formal declaration. It will automatically be declared when a value is assigned to it. Variables are prefixed by a dollar sign: ($VariableName).
Variables do not have declared types. A variable's type does not have to be fixed, meaning it can be changed over the variable's lifetime. The table below list's PHP's variable types:
Type Description
Integer integer number
Double floating point number
bool1 Boolean (true or false), available from PHP 4.0
Array hybrid of ordered array and associative array
object2 an object with properties and methods (not discussed in this article)
PHP (recursive acronym for "PHP: Hypertext Preprocessor") is a widely-used Open Source general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.
How PHP came into being
PHP started as a quick Perl hack written by Rasmus Lerdorf in late 1994. Over the next two to three years, it evolved into what we today know as PHP/FI 2.0. PHP/FI started to get a lot of users, but things didn't start flying until Zeev Suraski and Andi Gutmans suddenly came along with a new parser in the summer of 1997, leading to PHP 3.0. PHP 3.0 defined the syntax and semantics used in both versions 3 and 4.
Why yet another language?
People often ask "why invent yet another language; don't we have enough of them out there"? It is simply a matter of "the right tool for the right job". Many Web developers found that existing tools and languages were not ideal for the specific task of embedding code in markup. Those developers collaborated to develop a server-side scripting language which they felt would be ideal for developing dynamic Web-based sites and applications.
PHP was created with these particular needs in mind. Moreover, PHP code was developed for embedment within HTML. In doing so, it was hoped that benefits such as quicker response time, improved security, and transparency to the end user would be achieved. PHP has evolved into a language, or maybe even an
environment, that has a very specific range of tasks in mind. For this, PHP is, in Stig's humble opinion, pretty close to the ideal tool.
For More Information
The following resources will further your knowledge of PHP:
The PHP Manual: http://www.php.net/manual/en/
The PHP FAQ: http://tr2.php.net/faq
PHP Notes 1 Lakshman Gupta 8:57 AM 10/8/2019
PHP Tutorial
Language Syntax
Most of PHP's syntax is borrowed from C, although there are elements borrowed from Perl, C++ and Java as
well. This article assumes that you are familiar with C's syntax. However, don't panic if you're not.
Embedding PHP Code
To give you an idea of what embedding PHP would entail, consider the following three "hello world" examples,
all of which will give the exact same output:
Example 1: HTML alone
Hello, World!
Example 2: PHP code alone
<?php print "Hello, World!"; ?>
Example 3: PHP embedded within HTML
<?php print "Hello,"; ?> World!
Web servers supporting PHP will, by default, scan a file in HTML mode. HTML code will be passed over to the
browser as usual, up until the server happens upon a PHP line of code. In examples 2 and 3 above, the "<?php" tag informs the server that PHP code is to follow. The server then switches over to PHP mode in
anticipation of a PHP command. The "?>" tag closes out the PHP mode with the server resuming its scanning in HTML mode once more.
Embedding code in this manner is, conceptually, a more fluid approach to designing a Web page because you are working within the output setting, namely an HTML page. Traditionally, you had to fragment the output (i.e. the header, body, footer etc..) and then put it into the code. Now we are inserting the code directly into the output.
"So, what's the difference?" or "Why add extra code when HTML alone would do the trick?".
Dynamic vs. Static Web pages
The "Hello, World" example we chose would certainly not require you to use PHP. That's because it is static, meaning its display will always remain the same. But what if you wanted to greet the world in any number of
ways? Say, for example, "Bonjour, World!", or "Yo, World!" and so on.
Since HTML tags are purely descriptive they cannot function as a variable. Nor can they convey even the simplest of uncertainty such as a "Bonjour" or a "Yo". You need a command language to handle variability in a
Web page. Based on either a conditional statement or direct user input, a command language can generate the
"static" HTML necessary to correctly display a Web page's content.
Let us reconsider example #3. This time we want to let the user decide how to greet the world:
Example 4: PHP embedded within HTML revisited!
<?php print $greeting, ", "; ?> World!
From the above example, $greeting is assigned a value, and together with the comma and the word "World!",
this value is sent to the browser.
Dynamic Web page design, however, is more than just about inserting variables. What if you wanted not only to
greet the world in French, but also to present the page using the colors of the French flag?
Both a Web page's structure as well as its content can be customized. This means dynamic Web page
programming can also entail on-demand Web page building. No static, here!
Variables In PHP, a variable does not require formal declaration. It will automatically be declared when a value is assigned to it. Variables are prefixed by a dollar sign: ($VariableName).
Variables do not have declared types. A variable's type does not have to be fixed, meaning it can be changed over the variable's lifetime. The table below list's PHP's variable types:
Type Description
Integer integer number
Double floating point number
bool1 Boolean (true or false), available from PHP 4.0
Array hybrid of ordered array and associative array
object2 an object with properties and methods (not discussed in this article)