Jump To Content

PHP: Basics


I am assuming that you already have PHP installed and configured on your web server. If you don't, then you need to goto http://www.php.net to find information on that. Once you have everything setup and working then we can move on.

Topics:
PHP Tags
Comments
Echo/Print
Variables

PHP Tags
Tags are how we tell the browser that we are about to execute some PHP code. Kind of like the open HTML tag() and close HTML tag(). In PHP there are 2 different tags we can use.

Canonical Tags:
Canonical are the standard PHP open and close tags. To open you use () and to close you use (). Simple, right? Now, there are some things to know about this type of tag. If you are using XML in your code then you must use this type of tag.

Example:
PHP Code Example:
<?php
your php code here
?>


Short-open Tags:
Short-open tags are very popular because they are simpler and shorter. They work like so, to open you use () and to close you use (). To use this tag you must make sure that it is enabled by doing one of the following. Compile PHP with "-enable-short-tags", enable "short_open_tags" in the php.ini or use the short_tags() function.
Example:
PHP Code Example:
<?
your php code here
?>



Commenting
In PHP we have three types of comments that we can use. None are wrong, all of them are equal so you can choose which one you like the best. Comments are important because it makes your code easier to read and if you use the comments you can go back later and know exactly what your code does. If you work in a company where many different people will be looking at your code or using, it makes it easier for them to be able to tell what it does.

C-Style Comments:
To use this style of comment you start with /* and end with */. The great thing about this style is that it can span multiple lines because everything between the start and ending tag is commented out.

Example:
PHP Code Example:
<?
/* Here is your comment */
/* Line 1
Line 2
Line 3
*/
?>


Single Line Comments:
There are two types of single line comments you can use. The first one you put a "//" at the beginning of the line and the second is a "#" at the beginning of the line.

Example:
PHP Code Example:
<?
// Here is your comment
# Here is another comment
?>



Echo and Print
In PHP we have a few different ways that we can print data to the users screen/browser. There are two functions, echo and print.

Echo:
PHP Code Example:
<?
echo "Print this to the screen.";
echo 
"Argument 1""Argument 2";
echo(
"Print this to the screen.");
?>

Print:
PHP Code Example:
<?
print "Print this to the screen";
print ("Print this to the screen");
?>

  • Your comment will be modifiable for 10 minutes after posted.

Page Author

Avatar
laurellion
Name
laurellion

From Here You Can…

Information

Most Recent Related Content

Published In…