- The basics of PHP are syntax and layout in HTML pages. The first syntax standard to know is that PHP code is surrounded in blocks that indicate code. These blocks are designated with a starting "<?php" and an ending "?>". Anything within these code blocks is recognized as PHP and executed on the server. Below is an example of code that writes "Hello World" to the Web page.
<?php
echo "Hello World";
?>
Notice the semicolon at the end of the statement. This is a part of PHP code required to indicate the termination of a line of code. Without the semicolon, the compiler will throw an error during run-time. - Variables are syntax required for any programming language. Variables are denoted in PHP with a "$" prefix. Variables can be strings, integers, decimals or objects. The code below is an example of declared variables:
<?php
$aString = "My First Program";
$aNumber = 20;
?>
PHP is considered a "loose" language. While most programming languages require the programmer to define variables, PHP allows developers to assign variables without first declaring them. - One common function of PHP is processing form variables when a user submits a Web page. The PHP code processes the input and sends it to the database or displays it to the user. The code below is an example of PHP retrieving variables from the first name in a form and displaying them to the browser window:
<html>
<body>
Hello <?php echo $_POST["first_name"]; ?>.
</body>
</html>
The Basics
Variables
Form Processing
SHARE