|
|
| Author |
Message |
RCTycooner
Site Admin

Joined: 26 Feb 2005
Posts: 197
Location: Belgium -> Antwerp -> Merksem
1274.61
|
Posted:
Fri Sep 07, 2007 10:49 am |
  |
(I'm following the main lines of the php.net getting started documentation: http://www.php.net/manual/en/introduction.php --> you can often find user's notes there that might clarify things even more)
Any questions about anything, just ask ;)
What is php?
First, you'll have to understand that php is executed on the server. HTML and Javascript are executed on the client's computer (by the internet browser). This is the main difference between php and javascript. --> php code will run BEFORE html is shown by the browser.
What do I need?
You'll need a hosting that provides PHP support. (Most hostings have this)
Optionally, you'll want them to provide a database (eg. MySQL). Do note that this is not a requirement. ;)
Furthermore, you'll need an editor. Notepad will do just fine :) But when you use it, make sure the file is saved as ".php" (default will be ".txt").
Your First script
Well then, now you got something to write php and something to test it, let's make our first script:
Open your editor (eg. notepad) and write the next code:
| Code: |
<?php
echo 'Hello World!';
?>
|
Save it as a ".php" file (eg. test.php), upload it to your website and use your webbrowser to go to the file.
You will see something like this:
Now, let's examine this little script:
The tags <?php and ?> encircle the area where the script is written in php.
This is needed so you can combine php with html.
An example:
| Code: |
<html>
<head>
<title>A PHP Example</title>
</head>
<body>
We put some html up front...
<br>
<?php
echo "Followed by some php code";
?>
</body>
</html>
|
This will Give a page that looks like this:
| Code: |
<html>
<head>
<title>A PHP Example</title>
</head>
<body>
We put some html up front...
<br>
Followed by some php code
</body>
</html>
|
As you can see, you can put the php in between the html or vice-versa :)
Now, onto the next part of that piece of code:
"echo" is a function that justs prints out some data.
One thing you'll definatly need to know in php is that every code of line stops with a ";".
This means that you can put code on multiple lines in your editor and it still we be interpreted correctly by the webserver.
An example:
| Code: |
<?php
echo 'This is
an
example.';
?>
|
This will show:
| Code: |
This is an example.
|
Variables
Variables work in php just like they do in other languages. With the only difference that they can be easily reconised by their $ sign before the name.
The code below shows some examples on how you can use variables:
| Code: |
<?php
//some integers:
$a = 1;
$b = 5;
//a few things you can do with them:
$c = $a + $b;
$d = $b * $c;
echo $c;
echo "$d contains: " . $d;
//some strings:
$s = "This is a string";
$t = 'This is one too';
echo $s;
//The "." (dot) glues several strings together
echo '<br>'. $s . ' ' . $t;
$this_is_a_variable = 'You can name a variable in several ways.';
?>
|
You can type commentary between your code by these two ways:
| Code: |
<?php
// use two slashes "//" to comment a single line of text.
/*
You can start a block of comments with "/*"
But remember to end it with "*/"
*/
?>
|
|
|
|
   |
 |
|
|
|
View next topic
View previous topic
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
| |
|