A Structured Document
To create a PHP page start with a text editor.
To properly structure a page, begin and end the document with:
php Extention
If you have PHP inserted into your HTML and want the web browser
to interpret it correctly, then you must save the file with a .php extension.
Instead of index.html, it should be index.php if there is PHP code in the file.
Semicolons
Every statement in PHP ends with a semicolon.
Comments
PHP support three kinds of comment tags :
- //
This is a one line comment
-
#
This is a Unix shell-style comment. It's also a one line comment
-
/* ..... */
Use this multi line comment if you need to.
Variables
A variable is a means of storing a value, such as text string "Hello World!" or the integer value 4.
After varibles are initialized they can be reused throughout your code.
In PHP you define a variable with the following form:
If you forget that dollar sign at the beginning, it will not work.
Also, variable names are case-sensitive, so use the exact same capitalization when using a variable.
The variables
$a_number and
$A_number are different variables.
There are a few rules that you need to follow when choosing a name for your PHP variables.
- PHP variables must start with a letter or underscore "_".
- PHP variables may only be comprised of alpha-numeric characters & underscores. a-z, A-Z, 0-9, or _ .
- Variables with more than one word should be separated with underscores. $my_variable
- Variables with more than one word can also be distinguished with capitalization. $myVariable
echo
The PHP function
echo is a means of outputting text to the web browser.
String Creation Heredoc
$my_string=<<<MY_STRING
Saint Anns School<br />
129 Pierrepont Street<br />
Brooklyn, NY 11201
MY_STRING;
There are a few very important things to remember when using
heredoc:
- Use <<< and some identifier that you choose to begin the heredoc. In this example MY_STRING is the identifier.
- Repeat the identifier followed by a semicolon to end the heredoc string creation. In this example that was MY_STRING;
- The closing sequence MY_STRING; must occur on a line by itself and cannot be indented!
Another thing to note is that when you output this multi-line string to a web page,
it will NOT span multiple lines. To insert a carriage RETURN you need to use the
<br /> tags contained inside the string. Here is the output made from the code above: