Login
Latest Code Ticker for PHP
Daily Code Email
|
|
|
| | Submitted on: 9/15/2002 10:11:59 AM
By: John Croucher
Level: Beginner User Rating:
By 3 Users Compatibility:PHP 3.0, PHP 4.0
Users have accessed this article 2349 times. |
|
| | This tutorial provides any beginner with the basic skills required to start programming in PHP. | | | Terms of Agreement:
By using this article, you agree to the following terms...
1) You may use
this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame.
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description. |
PHP is a server side scripting language that makes it easy to create dynamic websites.
The code is similar to Java and C++ so if you know one or both of these languages you should be able to pick up PHP fairly easily.
To use PHP you must either upload your site to a server that supports PHP or set up your own server. Unless you have a fast internet connection uploading your files for testing all the time will most likely not be a very good option for you. A good option is to have your own server on your local workstation. The best way to do this would be to use Windows 2000 or XP with Internet Information Services installed. You can then download the PHP engine and install it on your computer. This will allow you to test your pages quickly. However there are other ways of doing this such as the Apache sever.
PHP is an embedded language. This means that you can embed code within a HTML document. There are a few ways in which this can be done:
<? echo ”Test Message”; ?>
This is the most common way of embedding code, but it may cause problems if your code must co-exist with XML.
Another common way to embed is:
<?PHP echo ”Test Message”; ?>
I recommend using this method for embedding for compatibility with other systems.
There are two other alternate methods but these two aren’t used very often:
<% echo ”Test Message”; %>
By default this method is turned off, and I do not recommend using it.
The final way is similar to putting JavaScript in your page:
<SCRIPT LANGUAGE=”php”>
echo ”TestMessage”;
</SCRIPT>
Every file you create that contains PHP code you must make sure the extension is the PHP extension set on your server. I usually just use .php, but some people say that you should use the PHP version .php3 or .php4. I have never had any problems but there may be some issues with other servers which is why I still mentioned it.
As I mentioned earlier PHP’s syntax comes from languages like C++. This means that you can use: // /* */ and even # for comments. Also the structure of things like for loops are similar, but variables in PHP are different.
All variables have a $ sign at the start and is followed by an alphabetic or underscore character. No data type declarations are necessary and you do not have to initialise the variables:
<?
$name;
$name=”John”;
echo “My name is $name”;
?>
OUTPUT:
My name is John
Notice how I can just put the variable name inside the output string.
Double quotes will be checked for variables and escape sequences.
If you do not want this to happen you would use single quotes:
<?
$name;
$name=”John”;
echo ‘My name is $name’;
?>
OUTPUT:
My name is $name
If you do wish to specify the data type for a variable you can use type casting:
$number = (int) = “342asd”;
This will create an integer value of 342.
The types PHP supports are as follows:
(int), (integer) – Cast to integer
(real), (double), (float) – Cast to float
(string) – Cast to string
(array) - Cast to array
(object) - Cast to object
Selection is something that is very important to know for every language. Selection in PHP is very similar to C++ and Java. We will fist start with the If statement:
if(expression){
statements
}
This is a basic If statement. It is a bit hard to understand this way because we have nothing to test for so I will expand and put some sample data in.
if($name == “John”){
echo “John is the creator of this tut”;
}
Now with this data in it will say if the value in $name is equal to the string “John” then do the code within the braces.
We can then further expand on this to do something else if the value is not true:
if($name == “John”) {
echo “John is the creator of this tut”;
} else {
echo “$name is not the creator of this tut”;
}
This code will do the same as previous but it will now output the second echo statement if it is not true.
Say the value of $name is “Bob”. It will go through and say is $name the same as “John”. No its not so we will go to else and output “Bob is not the creator of this tut”.
Getting confused? I hope not because we are going to expand on this code again and test for a second name.
if($name == “John”){
echo “John is the creator of this tut”;
} else if($name == “Bob”){
echo “I don’t know any Bob”;
} else {
echo “$name is not the creator of this tut”;
}
This code again does the same as previous but it will test for two conditions.
Say the value of $name is still “Bob”. It will look at the first condition. Is the value in $name the same as “John”? No, So go to the next if. Is the value in $name the same as “Bob”? Yes, output “I don’t know any Bob”.
You can keep expanding on this testing for as many conditions as you wish.
Just a note if you have only one line after you if statement you don’t have to have braces:
if($name == “John”)
echo “John is the creator of this tut”;
But if you want two or more statements you must have braces. I just find it easier to use braces all the time.
Also you may be wondering why I am using “==”. Well if you think to where you assign a value:
$name = “John”;
That is how you assign a value, so if you had:
if($name = “John”)
You would be trying to assign a value and you would receive an error.
Other comparisons you can do are things like &&, ||, >, <
I will explain the Boolean operators in a later tutorial.
The next selection statement is the Switch. The basic syntax is as follows:
switch(expression) {
case expression:
statements
break;
default:
statements
break;
}
Again this is a bit hard to follow so I will put some meaningful code in to help:
switch($name) {
case “John”:
echo “John is the creator of this tut”
break;
default:
statements
break;
}
This will basically do the same sort of thing as an if statement. If the value in $name is “John” then output “John is the creator of this tut”. If not then go to the next case, in this example the next case is the “default:”. This acts in a similar way to “else” in an if statement.
Also just like if statements you can expand and test for more values. To do this all you need to do is to add in more case statements.
switch($name) {
case “John”:
echo “John is the creator of this tut”
break;
case “Bob”:
echo “I don’t know Bob”
break;
default:
statements
break;
}
That concludes my first PHP Tutorial.
Check out my site for more tutorials and code.
| | | Report Bad Submission | | | Your Vote! |
See Voting Log | | Other User Comments | 9/16/2002 5:08:03 AM:TrueLyFE I already know PHP, but from a newbz
mindset, this doesn't quite work.
Why?
You just jump into code.
Try
to explain the BACKGROUND to the
language, as well as how the language
thinks - now how the code
thinks.
For those knowing other
languages such as Java or C++, it is a
decent pointer to a couplea key aspects
to the language.
| 9/17/2002 11:13:55 AM: What's the advantage/disadvantage of
PHP vs ASP pages with VBScript?
Is PHP
more of an open-source Linux thing?
| 9/18/2002 12:08:02 AM:John Croucher PHP is more of a Linux language but it
is on more Windows servers now. But I
think PHP and ASP both have advantages.
I think it just comes down to what your
system will support, what you want the
pages to be able to do and also which
one you feel more comfortable using.
| 9/18/2002 12:30:17 AM:Mehdi Hanbali perl rules, php and asp are OK, but
perl still rules :)
| 9/19/2002 7:39:30 PM:TrueLyFE I tried to code in perl but I just
didn't get it. Mehdi, try using PHP. If
you are at all in the slightest
familiar with C, then PHP will be
really quick to grasp. :D
| | Add Your Feedback! | Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.
NOTICE: The author of this article has been kind enough to share it with you. If you have a criticism, please state it politely or it will be deleted.
For feedback not related to this particular article, please click here. | | |
|