As PHP was originally developed as a programmable layer on top of HTML, PHP script can be placed anywhere within an existing HTML document and launched upon loading the page, as long as the following requirements are satisfied:
Simply inserting PHP code and loading the page on the macOS browser (ex. Safari) will not work, and this is why the
ocad.ckprototype.com server is provided as part of this course. One may, however, opt to turn a personal computer into a web server.
Consider the following PHP page (a simple HTML file with PHP extension):
... <body> <?php $color = "red"; echo $color; ?> </body> ...
<?php (can be shortened to
?> identify where PHP code starts and ends. All PHP scripts written between the two tags are identified, analyzed, and compiled by the web server, and its output is pushed to the web browser.
In the the above example, the programmer creates a variable (container of information) in the PHP world, and sets its value to "red". This value remains only in the backend and inaccessible to the browser, until the programmer decides to "print" the value, bringing it to the front-end level.
Each variable can be defined with a
$ sign, followed by the desired name:
... <body> <?php $hello = "Welcome"; $x = 10; $y = 5; $z = $x * $y; ?> </body> ...
As illustrated above, one can define multiple variables of different data types, and even concatenate texts or perform simple arithmetics by referring to multiple variables. The above example illustrates how one can calculate a product (multiplication) of two variables. Finally, note that all this backend code will continue to stay away from the front-end until one decides to "print" the variable.
Variables can come in different forms and sizes. Including the above examples, PHP features the following data types with specific use cases:
array("BMW", "Audi", "Kia")
There are other fundamental programming concepts yet to be explored here, and they will be discussed as the need arises in subsequent chapters.