VisageHosting.com
 
Home
 
Resources
- PHP and MySQL (2)
- HTML and CSS (0)
- CGI and Perl (0)
- Java (0)
- JavaScript (1)
- Python (0)
- ASP (1)
- Web Hosting/Servers (14)
 
Articles
- PHP/MySQL (11)
- HTML/CSS (5)
- Other Languages (4)
- Web Hosting (2)
- Computer Tech (7)
 
Forum
 
 
Register
Login

Username:

Password:

 
 
Currently 0 registered users and 4 guests browsing.

29 articles available for viewing.

1188071 total page hits.

Welcome to our newest registered user, davidt!


PHP/MySQL -> Tutorials



Basic Syntax of PHP

- by surreal on 05/15/06, 11:34AM CDT (2936 Views)



Someone once told me that the PHP developers took all the best aspects of all the best programming languages and put them into one. If you're familiar with any other programming languages, chances are you'll find the basic syntax of PHP mind numbingly easy. This article is for those that won't :)

Lets start with the basic operators of PHP.

Filenames

For a webserver to recognise that PHP is going to be used, every PHP file must be name x.php where 'x' is the name of your file. This may seem obvious, but I can guarentee it isn't to some people!

Starting and Ending PHP Code

The next important part comes at the very beginning of your PHP script. For the webserver to recognise PHP code, <php must come before any PHP code block that you want to use. Similarly, ?> must be at the end of any PHP code block. Only PHP code inside these will be read as PHP by the webserver.

For example:


Code:
<?php

echo "HELLO WORLD!";

?>


Now, you may notice I used the PHP function "echo" in the code snippet above. echo() is a very simple PHP function that will basically print to the web page anything you include inside it. I will explain this in more detail in another article, but I will use the format of the echo() function to explain more of the PHP syntax.

Starting and Ending a Command

Similarly to HTML, any command that is "opened" must also be "closed". This is done by use of a semi-colon at the end of the PHP function, as shown in the above example. This is basically to signal to the webserver that there is no more to come from that particular command/function and it can move on to the next one.

Quotations

You may also notice from the above example the use of quotations. These are often used to tell the webserver what the function/command should actually do. In this case, they are telling the webserver to output 'HELLO WORLD'. Another important syntax rule is that quotations must always come in pairs. This means that if quotations are opened, they must also be closed. This rule also applies to parenthesis: () and single quotations: '.

Variables

Variables are a way of storing information in a condensed and easy to handle form. They can take many different values and many different forms, and can be named pretty much anything you want. The golden rule is that all variables must be prefixed with the character $ (the dollar symbol). This is for the webserver to recognise it's a variable and not a command. It's also important that each variable be named differently, as naming two variables the same will result in the second variable overwriting the value of the first variable. Below are some examples of the forms variables can take.


Code:
<?php

$var1 = 1; // $var1 will take the value of 1.

$var2 = 1 + 1; // $var2 is being used to calculate the sum of 1 + 1, and hence will take the value of 2.

$var3 = "Hello little one"; // $var3 is taking the form of text.

$var4 = $var1; // $var4 is taking the form of $var1, and hence will be equal to 1.

?>


As you can see above, an important role of variables is to perform mathematical calculations. They will be calculated just like how you'd enter calculations on a calculator, and it is very important that order of operations are taking into consideration. The syntax for PHP mathematics is shown below:

Addition: This is done by using the character +
Subtraction: This is done by using the character -
Multiplication: This is done by using the character *
Division: This is done by using the character /

There are many PHP functions that will perform a huge range of mathematical calculations. These can be found at the PHP Homepage.

Commenting

In the example above you may notice the use of two forward slashes after each line of code. These are known as "comments" and are used to make a note, usually about the code written before or after it. Essentially the webserver will ignore anything written after the // until the end of the line. For multi-line commenting, the following is used:


Code:
<?php

/* This
is a multi-
line
comment */

?>


Everything in bewteen /* and */ will be ignored by the webserver.

Good Practice

As a note of good practice, it is a good idea to separate PHP commands and functions by new lines, as shown below:


Code:
<?php

echo "Hi there!";
echo "How are you?";
echo "I'm super!";

?>


---------------------------------------

That's about it for now. I will go into more detail on some useful PHP functions in my next article!


Seen this article somewhere else? Report it for plagiarism!

Noticed an error in this article? Tell us what's wrong!

View Comments for this Article

Related Articles

- PHP Syntax (Control Structures): if, else and elseif

- Basic Syntax of a Java Program

- Prerequisites for Java Programming

- Compiling and Executing Your Java Program

- PHP Syntax (Control Structures): while, do-while and for Loops
Rating

5.00/5

1 Votes

Want to rate this article? Register a free account or Login if you already have one.


Comments

- There are currently no comments for this article.

Want to add a comment? Register a free account or Login if you already have one.



All content on this site is property of www.juxtaposing.com. Do not copy anything on this site without prior written permission of the site owner.