Welcome to the first tutorial on
Tutorials-Live! This tutorial will guide you through the basics of PHP, and relate them to the syntax of VisualBasic. I will show you the similarities and differences and give you a kick start on PHP if you're coming from basic.
Why VB?
Well, VB is one of the most popular programming languages out there in the world today, with PHP. VB is more widely used than PHP, possibly because it's easier to pick up for some and designing forms also gets you to flex your creativity. If you've never used VB, it is a programming language as well as a design tool used for making applications in a moderately easy to navigate environment. It has a simple to understand syntax, much easier to understand than PHP's for a beginner and is often a gateway into other languages.
Getting Started... Basic Basics
Everyone should know about the classic "Hello World" examples in programming. They basically just tell you how to write the words "Hello World" for the user to see. You can do this in basic like so:
Code:
Form1.Print "Hello World"
Form1 is the name of the Form in VB, and print is the function that will write what follows in that form.
Code:
'This is a comment
Rem This is also a comment
As you can see, a single quote starts a comment, and is valid until the line breaks. Rem also starts a comment and is short for a reminder. Those of you familiar with ASP will recognize this style of commenting.
Code:
' If/Else If/Else Statements
If Event Then
' Do This
End If
If Event Then
' Do This
Else If Other Event Then
' Do This
Else
' Do This
End If
That is the syntax for event statements in VB. Instead of PHP's bracket type of operation, VB uses a word bases approach, which suits beginners well, and often better then a bracket and curly bracket approach.
In PHP now
For the Hello World example, PHP works like this.
Code:
<?
echo("Hello World");
?>
And if you would like to comment your code...
Code:
<?
# one line comment
// another one liner
/* starts the comment, and then ends it. (can go on forever) */
?>[/php]
PHP Code blocks are contained with a <? starting tag, and and ?> ending tag.
[code]<?
if($event) {
// Do this
}
if($event) {
// Do this
}
elseif($other) {
// Do this
}
else {
// Otherwise, this.
}
?>
That is the PHP event statement syntax. It looks quite different from VB, doesn't it? The next section is all about variables!
Both Sides of Variables
Variables in VB are quite different from PHP. In VB you can't just pull a variable out of no where like in PHP, and VB has some special things you have to do.
Code:
Option Explicit
Dim Variable as Integer
In VB, that will create a new variable name Variable as an integer. You have to choose what kind of variable you create. Some common types are:
Code:
Integer .......... Number
Double ........... Larger Number
Boolean .......... True or False
String ........... Words
Note how when using variables in VB you have to have "Option Explicit" at the top of your code. (Don't ask, just do.)
In PHP, you can pull a variable out of nowhere. You start a variable with a dollar sign ($) and end it with a semi-colon (;), like this:
Code:
<?
$var = "value";
?>
So now, when $var is called, it will be replaced with "value". For example:
Code:
<?
echo $var;
// Output: value
?>
You do not have to tell PHP what kind of var you're using.
Code:
<?
$var = true;
$var = "lalala";
$var = 10;
// all valid...
?>
In Closing
If you were a PHP/VB only before, I hope this could have been of some use to you. Both languages are great things to know and to put on a resume, as well as broadening your mind as a programmer. Your PHP will be different if you know VB and vise versa.
-
Connor (darkesT)