Forum

 
menu_l Home Search Members Calendar Help Donatemenu_r Current time: 05-02-2024, 01:32 PM menu_l

Hello There, Guest!
(LoginRegister)

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5

Thread Contributor: RazorDOXBasics of PHP

01-08-2011, 12:10 PM,
#1
Basics of PHP
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:



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:



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:



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:


echo ”TestMessage”;


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
Code:
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:
Code:
$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:

Code:
$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:

Code:
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.

Code:
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:

Code:
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.
Code:
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:
Code:
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:

Code:
$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:

Code:
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:

Code:
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.

Code:
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;
}
Add Thank You Reply
01-13-2011, 05:27 PM,
#2
Basics of PHP
I'm slowly understand this! Big Grin
Add Thank You Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)