To start your PHP scripts you need to use the PHP tags, these tags will tell the server that what
is inside the tags needs to be parsed by the PHP engine before it's output is displayed.
The opening and closing tags are shown below.
<?php // opening PHP tag
|
|
See the orange text in the example above? Those are comments. Comments allow you to leave yourself notes or reminders while you're coding. They're also useful in situations where you are working with multiple programmers. You can create either single line comments or multiple line comments.
To make a single line comment just type "//", all of text after that, on the same line be commented out. You can also use the "#" character to create single line comments.
To create multiple line comments type "/*" followed by your comments, then type "*/" when you want to end your commenting.
One of the most basic things you should know how to do with PHP is a simple echo. by 'an echo' I mean printing out text to the page. To do this, we'll use the built in PHP function, echo. Here's an example:
<?php
|
|
If you are using double quotes to write your text and you want print out double quotes, you need to escape them. To do this just put a "\" before your quotes. Do the same if you want to print out a single quote when you are using single quotes for the echo. I know that sounds a bit confusing, so here's an example:
<?php
|
|
It's important to remember to escape your quotes when nessecary, something as small as that can somtimes be the cause
of PHP parse errors that make you want to just pull out all of your hair. 
If you've studied algebra, then you've come across variables before.
Variables are used in PHP too, they're used to hold values. To create a variable,
just type a dollar sign($) followed by your variable name, an equal sign(=) and the value you want it to hold.
Here's an example:
<?php
|
|
Notice, I didn't use quotes when declaring the $myNumber variable. When assigning an integer, quotes may be used, but aren't required.
Now that these variables are defined, you can use them later in your scripts, simply by calling upon the variables, here's an example:
<?php
|
|
The period after '19' is used in PHP as a concatenation operator, which means that the '87', held in $myNumber will be combined, or connected, with the '19', then the result (1987) is echoed. For more information on this, see the PHP manual on String Operators.
One important thing to remember about variables is that they are case-sensitive, which means $myvar and $myVar are two completely different variables. It's also important to remember that variable names cannot start with numbers. They may only start with underscores(_) and letters. However, they may contain any combination of letters, numbers, and underscores after that.
That's it for the PHP basics tutorial, I hope it helped you.
| Discuss Tutorial: PHP Basics | 18 Comments |






It's so awesome.